Detect stopping of Telegram bot - telegram

Does Telegram bot receive any message when the user stops the bot?
When the user starts communication with a bot, he/she sends "/start" command/message to the bot. I would like to know when the user decided to leave.

If you are using InlineKeyboardMarkup I would suggest to use timeout parameter in MessageLoop, so you would know when the user stops sending callback queries.
Something like this
ERROR:root:on_close() called due to IdleTerminate: 1 #1 sec termination
Otherwise there is no method to know that the user has stopped interacting with the bot.

Related

Is there a way to track how many times users have started your Telegram bot from a specific message?

I currently have a Telegram bot as an admin for my Telegram channel. Is there a way for me to track how many times my message (from the bot) has been clicked? Specifically, I want to know how many times has a user started my bot from specifically clicking my message in the channel.
To illustrate: I will be sending out this in my Telegram channel (via my bot) see image 1
and I will like to know how many times this message has successfully led to a user starting my bot.
It will be more useful if there are methods to obtain this answer using bot api.

Telegram bot, session lifetime

I know that a telegram bot can't start conversation with the user. So user should click "/start" first. But for how long bot can respond to the user after the last message? Do any limitations exist like 24/48 hours?
Do any limitations exist like 24/48 hours?
No. After a user starts a conversation with a bot, the bot can send messages to that user until one of the following actions occur:
User 'Stop and blocks' the bot (See picture below)
User account gets deleted
For example, using the Desktop version, the user can 'Stop and block' a bot by pressing the following button:

How telegram bot knows where to respond?

I am using telethon and I have a question that how telegram knows where to send the request?
I have created a bot on telegram. When the user sends a message to the bot then how telegram knows where to redirect that message. I run my program in my local and also on a server. But in the code, we don't mention anything about the server. But the program is working fine on it also.
Need to understand the flow.
Telethon uses what’s called MTProto to communicate with Telegram servers. It connects just like an actual client would (like TDesktop or Webogram, for instance). Once the connection is made, instead of using a phone number, the authentication is made with the bot token. Then, everything else is standard procedure: Ask Telegram for any new message and Telegram responds with all the messages people sent to the bot or to groups your bot is in (if you have that enabled through BotFather). To cut down on traffic, if there’s an open connection, Telegram may send updates automatically.
That’s the general process flow. For more in-depth explanation of how MTProto works, all the technical information is available on their website. The portion about updates is here: https://core.telegram.org/api/updates
Every handler have an arg, i named it here event , e.g:
#client.on(events.NewMessage)
asynce def handler(event):
This event have all information in it, text of message, message id and etc.

Make Telegram bot to read only /commands as administrator

I am using Telegram bot API with AWS Lambda and Zappa.
Everything was working fine. Until I realised that bot's webhook is being called everytime whenever a message is sent by anyone in the group.
I want to limit this webhook requests, as Lambda allows for only 1 million calls which is sufficient if bot is invoked, but only when /commands are called.
As I have to pin and edit messages using bot, that's why administrator permission is must.
I just want my bot to respond only to /commands along with administrator rights.
Please help me in tackling this issue.
I am assuming you added your bot to a Telegram group. In this case, the first thing that you need is group id. This should help: https://github.com/GabrielRF/telegram-id#web-group-id
And then you need to write a function that identifies the status of a chat member. You can do this using getChatMember method in Telegram Bots API.
If the chat member's status is "creator" or "administrator" then the response is sent to that user.
There are 2 things you need to do.
setPrivacy for your bot - go to BotFather chat and use the /setPrivacy command which gives you the ability to set:
'Enable' - your bot will only receive messages that either start with
the '/' symbol or mention the bot by username. (Should be default)
'Disable' - your bot
will receive all messages that people send to groups. Current status
is: DISABLED.
When setting the commands for the bot, set the BotCommandScopeAllChatAdministrators scope which should limit the use of the command only to the admins of the chat.

how to check if a user started a bot in telegram or not?

I'm developing a bot in telegram , here is my problem:
I have some posts in my channel which some inline_keyboards are attached to them. when a user press the button , next events happen in a bot and some messages show to a user. if user has been started the bot before , there is no problem , if not I have error. how should I check the user is member of my bot or not to handle this problem???
try to use sendMessage() API and send a message to user. After that telegram will response a status tell you whether success or not.
if started bot receive receive is ok,
if not started bot receive is nothing.
{"ok":true,"result":{"message_id":9999,"from":{"id":00000000,"is_bot":true,"first_name":"mybot","username":"mybot"}}}
like that, good luck.
There is no way to check if bot can talk to user or not without send message.
You can use url with https://t.me/Bot?start=some_data format, and you will receive /start some_data, please try this link yourself.
The problem with this approach is that if a lot of users do the same, telegram starts to throttle your callbacks :( ... it would be so much easier if you could just read that information somewhere...

Resources