Retrieve all chat ids using Telegram bot - 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.

Related

How does telethon judge whether the chat is a group or a user? for bot

What I need is the type of this chat message, whether it is a personal conversation or a group conversation, not the interlocutor (I don't know if I say this, can you understand what I mean
https://docs.telethon.dev/en/stable/modules/custom.html?highlight=group#module-telethon.tl.custom.dialog
async for dialog in client.iter_dialogs():
if dialog.is_channel:
....
I looked through the documentation to find this, and after using it, it shows "telethon.errors.rpcerrorlist.BotMethodInvalidError: The API access for bot users is restricted. The method you tried to invoke cannot be executed as a bot (caused by GetDialogsRequest)
"
#lonami,
Is there any other way?
iter_dialogs method is only for user clients not for bot clients and you can use
await client.get_entity(<id or username here>) which gives your info about whether the id belongs to user or a chat or a bot.

How To Get Telegram Bot ID?

Anyone Who Add In My Group, I Want To Greet Them With My Bot! But After Some Time I Want To Delete That Bot Messages Too... For That I Need Bot Chat ID. So From Where I Can Get It?? The Bot Is Created With BotFather!
To delete messages, you only need the chat_id of the chat the message was sent in and the message_id of the message. See deleteMessage, you don't need the user id of your bot. If you need that for some other reason, you can get the via getMe: The return value is a User object that has the id field.

How to get authenticated by Telegram bot in Group chats?

Please kindly someone explain me how can the Telegram bot could understand who is sending the command in group chats and respond it with the the unique answer which is just for that user.
Surely in this case security issues should be considered and a user must not send command as another user.
I guess I can use username to send along with command.
Any suggestions...
The Message Object contains two objects apart from other objects:
Chat, message['chat'] which represents the Chat from which the message is coming. In your case the group.
User, message['from'] which represents the user that sent the message/command.
So it's easy to differentiate which user sent the message. And in case of Private chats, both the Chat object and the User Object are same.

Telegram bot questions

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.

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