Can I use chat_id to send message to clients in Telegram bot after a period of time? - telegram

I want to say to clients to start my chat bot and send me username and password, then I store chat_id of them, and use it whenever I want to send a message to one of them.
Is it possible? or chat_id will be expire?

If the chat_id is the same as the Telegram user_id for that client, then yes you can.
SendMessage:
chat_id [Required] [Integer] Unique identifier for the target chat
text [Required] [String] Text of the message to be sent

If you want to identify each user with a unique constant you can use user_id.
user_id: User identifier in Telegram.
chat_id: Unique identifier for the target chat.
user_id and chat_id wil never expire or change on Telegram. But a user may use your bot from a private chat or from a group. You may get two different values from the same user. So i suggest you to use user_id to do that.

When a user register on telegram, server choose a unique chat_id for that user! it means the server do this automatically. thus if the user send /start message to your bot for the first time, this chat_id will store on bot database (if you code webhook which demonstrates users statastics)
The answer is if the user doesnt blocked your bot you can successfully send him/her a message. on the other hand if the user had delete accounted no ways suggest for send message to new chat id!
i hope you got it 😉

Related

How to send a Telegram message to a username using bot api or telegram api?

Hi I am trying to build a website login system where users are able to log in using their telegram. I would like to achieve this workflow:
User selects to login/sign up via telegram.
User enters their username (#ExampleJoe).
A 6-digit verification code is sent to them via my Telegram bot (#ExampleBot).
The user enters the received 6-digit code into my website and is allowed to enter.
I have issues with steps 2-3 where my bot can send them a direct message (not in a group chat) with only their username. I am aware that sendMessage method in the Telegram bot API requires a chat_id and that a user_id is also suitable. However, I do not know how to get the user_id with just the user's username.
That's not possible with the Bot API. You'd have to use a userbot for that. Note however, that TG provides a native login widget that you can use instead.

How to confirm if a phone number belongs to a user_id in telegram bot

I'm working on a project where the user enters their phone number on a website. And the conformation is done through a telegram bot, which the user initiates a conversation with first. The user sends the confirmation code displayed on the site to the bot.
So here is my question, is there any way that I can compare the phone number the user provided with the confirmation code message the user sent to the bot? Or is there any way that I can get the user_id from the phone number, the user provided, so that I can compare it with the message sent by the user?
You can send Keyboard with request_contact=True that will ask user to share phone number (documentation).
Then your bot can get an Update with it (example how to do that) and compare with desired phone number.

Delete/Edite a client message in private Chat with Telegram Bots

In private chat with a bot (a user and a Bot), is it possible to edit/delete user messages? I am creating a Telegram Bot for registration. As a registration step, the users insert their password, and for security considerations, I want to replace it with stars or delete it. Any idea?
It is as simple as calling deleteMessage with chat_id and message_id
https://core.telegram.org/bots/api#deletemessage
EDIT:
As #mohamad-mehdi-rajaei mentioned in his comment, this method seems to be just to delete bot sent message, not user sent message.
The only solution I can imaging is to provide a numeric password inline keyboard to user and ask him/her to enter password by pushing your numeric inline keyboard buttons. In this way nothing be logged in client, and you manage user input as callback data in server side.
Bad thing with this approach is that user became limited to numeric password.
Bot API 4.2 changelog (updated April 14, 2019):
The method deleteMessage can now be used to delete messages sent by a
user to the bot in private chats within 48 hours.
Since the method signature is not changed, any wrapper/framework (like python-telegram-bot) support this operation by now.
This seems to provide s a solution - https://github.com/yagop/node-telegram-bot-api/issues/328 .
Basically, there's a deleteMessage endpoint you can use - https://core.telegram.org/method/messages.deleteMessages, passing it the message ID. So when you get the message (with it's ID), just delete it.

Adding registration tokens in an empty device group throws bad request in Firebase

We have one device group per user for our mobile app. When a new user sign up, a new device group with registration token is created via create operation from app server and its notification key is stored with user data.
When a user sign out of the app, its registration token is removed from the notification key via remove operation as we don't want to send personal notification for that user to that device anymore. This step returns same notification key.
When a user sign in again, we try to add the registration token via add operation using the stored notification key. This action returns 400 (Bad Request) status from Firebase server.
Is this expected behavior? If so, how to handle the above use-case?
Notification key for a device group is removed as soon as the token count reaches zero.
I ended up using topics instead of device groups for this use case.

As a telegram bot, what is the best way to authenticate the owner of a telegram channel?

Is there a way for a telegram bot to know who is the administrator of a channel?
One way to do this is to send a verification code to channel admin and ask the admin to confirm the code. I couldnt find any API that allows sending a message to channel admin, though.
I can authenticate a user account just by asking user to send a message to my bot, but is there a way to have a channel sending a message to a bot?
i think this is not required!
when you have a1255412542_bot and ask the user to add this bot as administrator, is it possible to a channel admin to add this bot as admin?!!
you can:
ask the channel name,
send a message to the channel to ensure that the bot is not the admin of the channel,
ask the user to add your bot as admin immediately! then press ok button on your bot,
then you send a text message (or sendChatAction for hidden test) to the channel and if successful, finished!
You can get the list of admins of any channel using this method:
https://core.telegram.org/bots/api#getchatadministrators
Check the permissions of that user since it returns a json array of username of the admins and their permissions

Resources