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

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

Related

Is it possible to get all my chat ids in telegram bot api?

I'm writing a bot that will send the same message for many people I've ever communicated in telegram. So I need to get all my old chat_id's.
All I found is just to store chat_id when I receive a new message as here Retrieve all chat ids using Telegram bot
But it doesn't fit me. If is it possible, please tell me, I'd really apreciate it!
It's not possible, as there is no such method listed in the Bot API Docs.

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.

Detect stopping of Telegram bot

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.

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.

Cannot delete bot message in telegram

I sent a message using telegram bot api. The bot I'm writing is kind of private channel bot, not inline.
So I've just sent a message, 48 hours has not lasted.
I tried to use this: https://api.telegram.org/botTOKEN/deleteMessage?chat_id=CID&message_id=MID
As a result I got "Bad Request: message can't be deleted"
Why so?
In my case I'm asking user for a price. If the price is not valid, I send a notification, asking user to re-enter the price. So, there might be many service messages, asking user to correct the price. After he entered correct value I want to erase these service messages. I can't think of any other solution except removing these messages, but I couldn't manage to do this.

Resources