Telegram bot questions - telegram-bot

User1 has a Telegram account and created the Telegram bot Bot1.
User2 has a Telegram account.
How User 2 can work with Bot1? i.e. in teh same way as User1 does it: send commands etc.
How Bot1 can send messages to User2? I.e. report about smth etc

You mean work with BotFather? No.
Each user can access to its own bots using BotFather and you can't access other bots from another accounts. But if you mean work with tokens, then yes you can work with bots that you have their tokens.
Getting last updates from telegram: https://core.telegram.org/bots/api#getupdates
NodeJS library: https://github.com/yagop/node-telegram-bot-api

Okay let's break your questions into parts:
How User 2 can work with Bot1?
When you say work with its kind of relative. There are two assumptions I can
come up with:
(I). It means that User2 wants to send commands from his
telegram app on his mobile device to bot1(bot1 was made by User1), in this
case anyone can send messages (any form of text) and commands (like
/showusers) to any bot even though they were not its creator. Also just a
side note this is from the telegram bot docs
Bots can't initiate conversations with users. A user must either add them to a group or send them a message first. People can use telegram.me/ links or username search to find your bot.
(II). On the other hand work with can mean that I can program how bot1
interacts with its users, e.g if a user sends /ping to the bot reply with a
message pong. In this case only User1 can work with bot1 because he has the
bot token. However if User1 gives his Bot token(its like your credit card
details) to User2 then User2 can program that bot to do anything he wants.
So to answer your question, if you mean my first assumption then User2 only needs telegram installed and the name of Bot1 but if you mean assumption 2 then User2 needs the bot token from User1 to work with it.
How Bot1 can send messages to User2? Every user has a unique chatid that telegram sends to you the first time someone clicks the start button of your bot(actually they send the chatid every time a person uses your bot). All you need to do is to store this chatid in a database or something and then when you want to send the person a message you indicate the chat id. Read about the sendMessage method made available by the API to better understand how to use it.

Related

Telegram Bots: Can I now start a chat or not?

https://core.telegram.org/bots clearly says:
Bots can't initiate conversations with users. A user must either add them to a group or send them a message first.
But if I visit https://telegram.org/blog/login it says
Telegram bots are a powerful communication tool, but until today they couldn't start a conversation. Even if you wanted them to reach out to you, you had to chat them up first.
With the new login widget, moving from interacting with a website to a conversation on Telegram becomes completely frictionless.
So it is somehow possible to start a conversation via the login widget. But how do I do it?
I get a user id. Is there an api endpoint to initiate a chat with the user?
The only way to begin conversing with a Telegram bot is to start the conversation with them yourself. There's no way for the bot to start a conversation with a user themselves.
As you said, telegram clearly says there is no way for bot to start a conversation, the only thing bots can do is sending messages to an old conversation (I mean a user have to start the conversation).

Is there any way to transfer Telegram file IDs between different bots?

Many different media types on Telegram have a file_id and file_unique_id property, such as Stickers, Audio, and Document.
The file_id property is unique to each Telegram bot, so one Telegram bot will not report the same file_id as another Telegram bot will, even if it's the same piece of media.
I want to have two Telegram bots communicate with each other, but they are currently unable to do so because the file_id property is meaningless when one bot tries to talk to the other.
The file_unique_id property is consistent between the bots, but it does not seem like you can do much of anything with that identifier.
Is there any way to pass a reference to a file stored on Telegram's servers from one bot to another, without having to re-upload the file on both bots?
Bot can't interact with each other. But there's a workaround,
we can use channels as a medium (with only BotAPI)
Steps,
Add two bots as admin in channel
Broadcast the messages from Bot A to channel
Now, the Bot B will get these as channel posts and your new fileid
for the same file which will be unique for Bot B (obviously)
You have officially transferred all fileid to Bot B
There aren't any official methods to share fileIds between bots. In fact, you can't even get 2 bots talk to each other, also you won't get bot updates in groups. So bot to bot communication is not possible at all at this moment.
But what you can do is to use Mtproto api and sign-in to telegram as a normal user (with phone number). And follow these steps using the logged in account:
Start both of your bots.
Forward messages you receive from bot 1 to bot 2.
This way you'll be able to access any files in bot 1 in bot 2.
You can use Telethon to write a script that does the job for you, listening to updates coming from bot 1 & forwarding them to bot 2.
Also using normal bot api you should forward received messages to your logged-in account.
The only way is to use a shared channel. Both bots have admin access. However, you may encounter error 429 while transferring the file.
The solution is to send the files to the channel at longer intervals.

Telegram Super Group Moderation

I'm looking for a telegram bot that will allow the admins or group moderators to first approve a message before the message is released to the greater group. Admins or moderators should be able to either approve or decline a message sent by a user of a group that way to keep control of messages and content of the group ?
Telegram is not made use of this, I think this is impossible.
You can try allow any message in group, and if admin approved, bot forward it to channel.
What about scrabbling the message posted by the users so when user sends message to group other users can't read it but only admins can read it and have to type password to open and read message ?

Retrieve all chat ids using Telegram bot

the main question is how do I get the chat ids for all the conversations ever held with the bot?
Imagine that during the execution of the bot there is a conversation with the user A.
Now I stop the bot process and start it again.
How do I get the chat id of that past chat with the user A?
I understand you get the chat id when the user sends you a message, and you use that id to reply, but what if user A no longer sends messages to the bot during the current execution? how to get the past conversation id?
Is the only one option to store the ids and retrieve them when the second execution starts?
UPDATE:
Looks like the current solution is to store the chat id somewhere safe, as answered by #Tick Tock.
Your question is unclear to me but as I understand from your question I wrote something to you hope be helpful. You can retrieve chat_ids and use it to send something to that chat. I would give a sample code but before let me explain something.
In Telegram Bot API there is two definitions: chat_id and from_id.
1-When we are in private chat with some one chat_id and from_id are equal.
2-When our bot is a group member, then chat_id is id of that group and is different from that person id(from_id) may be send something to group(and maybe our bot receive it too-when privacy_mode is off)
I assume your bot is in private chat:
when user sends anything to your bot, then Telegram gives that message to your BOT( calls your script), this sample code sends "Hello chat_id" to that user.(in PHP)
define('BOT_TOKEN','12345:abcde');//replace with your bot token
$command_prefix_url='https://api.telegram.org/bot' . BOT_TOKEN ;
$update = json_decode(file_get_contents('php://input')); //retrieves data sent by telegram
$chat_id=$update->message->chat->id; //retrives `chat_id`
$rep = json_decode(file_get_contents($command_prefix_url . '/SendMessage?chat_id=' .
$chat_id . '&text=' . urldecode('Hello '.(string)$chat_id))); //send something to that `chat_id` (sender)
UPDATED: (due to edition in question)
First, chat_id is unique and always permanent for that user(in private chats)
even if your bot's user leaves your bot and rejoin again.
I don't hear or read anything up to now that Telegram have been provided a method to tell your bot WHOLE its users chat_id , so the best way to know about your users is to save their chat_id and other info (that you gather along the time from user from messages reciceve from) in a database.
If you save at least their chat_id in a simple database then you have a list of your bot's subscribed users. And since chat_id is permanent you can send anything you want to your users.
AND, as I understand from your question, IF you do not have a database but user A is your bot's subscribed user, AS I KNOW, you should wait until she/he send a single message to you, then you grab her/him chat_id and add it to your database. NOW you can send her/him every time anything.

How can I send message to specific chatID?

I have chatId and want send messages while execute some tasks from java code. I found this explain on telegram "Bots can't initiate conversations with users. A user must either add them to a group or send them a message first. People can use telegram.me/ links or username search to find your bot." But my task works good when I use https request directly from browser. My bot perfectly answer for requests but how I can do this without user request?
I can not truly understand your case!
Anyway, your bot can send a message to a chat ID Only if the user witch blongs to the specified chat ID added your bot (send start to a bot).
and if it happened then send your message with
/sendMessage