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_id
s 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.