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

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.

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.

Telegram Bot sometimes requires User Authentication

When I access to my telegram-bot, after some time, it requires user authentication with the text:
"Stand by...
Hi there!
Before We Can Continue We Need To Verify That You're a REAL User"
When this message appears it seems session is closed, and the user can't receive Bot notifications, until user authenticates.
I can't understand why this authentication is required if I am storing each user telegram_id.
Your bot token might have exposed on git.
You need to change the token.
Instead of hard coding it in the code, pass it as an environment variable.
Just had the same issue!
I guess, someone scraped my bot-token from public repository and tried to steal my account. NEVER leave your bot-token in public rep!!!
Detailed description for others to find this thread:
Bot sent, what it was programmed to do and then
"Hi There!
Before We Can Continue We Need To Verify That You're A REAL User"
Telegram sent me login code -> I entered it (yeah, I'm dumb)
Bot printed
"Please reply with your 2FA (Two Factor Authentication / Two Step Verification) code"
then
"Timeout has been reached , pleaase try again."
(I like the pleAAse part, where you understand it was totally fake)
Telegram said it was
Device: Android
Location: Bulgaria (IP = 185.95.157.122)
I discovered what was wrong!
Lev Vasilyev answer made me think if somebody was able to get my bot_token. So, I changed it. And, the authentication message no longer appeared.
It´s important to get into telegram allowed devices, and delete not authorized accesses.
My bot has ""Stand by... Hi there! Before We Can Continue We Need To Verify That You're a REAL User" too.
When I proceed and send authentication code and sms from Telegram was received I had been authorized as Samsung Galaxy s20 5g from Seychelles ip adress which was not mine... This was very strange. If anybody had that experience please post your answer too. Maybe somebody had stolen my bot ...Sorry if my English not very correct and nice. I hope you understand.
I had the same problem, changed the Token and resolved the problem. I needed set the webhook again to work.
All replies are correct:
your token has been stolen and used by hackers, once you enter your credentials - you will see unknown sessions in your telegram.
Change token via BotFather and do not publish it in public places.
If you have already put your cred's to the telegram bot - you need to change the password immediatelly.

Push notifications by username

I have been looking for ways to send notifications to specific users and what I found was that I need the device token to do that.
I have tried Firebase and Ionic Cloud Service to do some pushs and it worked fine, but I'm wondering if it's possible to register a service with a key -> value, for exemple, register with the username and the token. If so, how can I do it?
And what is the best service to do it?
Thank you in advance for the help.
P.S.: I'm not asking for code, just the theory.
From you question [for example, register with the username and the token. If so, how can I do it?] I understand following.
You mean to say, there is a mobile app, which user will sign up to use and you want to send the notification to registered user i.e. get send push notification by username.
To solve this, you can follow the steps mentioned below.
On app launch when you get FCM registration token, save it to some intermediate location such as local storages along with device-id, mobile details etc..
Create a backend API which can save username and registration token in DB.
When a user signs up or signs in, then fetch the registration token from local storages, post username, token to backend API to save it. You can make backend API bit intelligent to handle multiple devices of the single user, distinguishable by device-id, mobile details.
Then while sending API from the backend, you can fetch all registration ids of a single user and send the notification to that users using all tokens of that user in FCM API. Use registration ids as JSON array in "registration_id" field. FCM Document - link.

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

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 😉

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