Telegram bot, session lifetime - telegram

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:

Related

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.

One bot managing many discussions with one user

Is it possible to create a bot who manage more than one conversation with one user?
I need to create a bot able to make the link between a random chat and telegram.
For example, if I don't wont to use Facebook Messenger anymore, creating a bot who send me every new Facebook message from my account to my Telegram number. The bot would create a new conversation for each unique Facebook user sending me message. Or, if it's not possible, a bot who create a new temporary bot for each new user conversation and give me his #name.
Is it possible with the Telegram Bot API?
According to the latest telegram bot api you can make more than one bot, but one thing that is crucial is the bot's API token that is made manually through botFather. You can make a single bot and make other bots inherit its features and every time a new user sends you a message assign a bot to him/her. Up to here, it's possible but for API token all you can do is to make as much bots as your Facebook friends count and store their api tokens in a DB and pass one of the api tokens to the bot that stands for a user.
This is what I think can solve your problem but the api token part is a bit strange 😀 although you can send a notification from the main bot to your own account when the number of unsigned api tokens get less than 5.
As you know the number of Facebook friends can change and any of them at any time can text you so there must be enough tokens because every time one of them texts you for the first time a bot must be created with a pre-made api token and inherit the features from the main bot. From that moment that token and that bot stand for the person who tried to message you.
Also I think there must be a limit to the number of the bots that you
can make using one telegram account which can be solved using
different accounts.
What you want is possible, as you know every single telegram user has a unique chatID in telegram so if your friends or users text to your bot directly you can use a code like below to answer their messages from your own telegram account and make the bot send it to the related user:
if(update.Message.Text != null)
{
var req = new SendMessage(Your_Own_ChatID, "This message is sent from the user with chat ID: "+update.Message.Chat.Id.ToString()+"\n In order to reply it type the user's chat ID and add one comma then type in your text and tap on send.\nThe message:"+ update.Message.Text);
await bot.MakeRequestAsync(req);
continue;
}
else if (update.Message.Text.Contains(",") && update.Message.Chat.Id == Your_Own_ChatID)
{
string sender_chatID = update.Message.Text.Split(',').First();
string Message = update.Message.Text.Split(',').Last();
var req = new SendMessage(long.Parse(sender_chatID), Message);
await bot.MakeRequestAsync(req);
continue;
}
But if your users send the messages from Facebook to you and you want the bot to send them to your telegram account and answer them again through the bot using Facebook API or something else, all that must change is:
Instead of sender's Chat ID you must save his/her Facebook username and reply it with the FB username instead of telegram Chat ID.
Note that this code is just a sample and of course separating the Chat ID and the reply is a simple solution, but my recommendation is to use inline or custom keyboards to answer the message and that way you don't even need to type down the user's ChatID or FB account.
Also you can use telegram's reply feature and check if the reply is null and find the Chat ID(or FB account or ...) of the sender of the message your trying to reply and send the reply directly to that user.
That is achievable using the method that is used in this project.
[https://github.com/idoco/intergram][1]
Essentially, you create a random userid for each user. In order to reply to that specific user, you have to use the reply to message method in your telegram client. Ie... right click on the message to reply to it.

Remove user mention in bot message

Is there a posibility to remove the mention of an user in an bot's answer? I developed a telegram bot with the microsoft bot builder framework and only sending the raw message like an echo bot but in group chat there is always this #username metion before every message my bot sends to the group chat. I added my bot via bot father to telegrams bot list.
At the moment this is not a setting the developer can change. Tracking the issue here: https://github.com/Microsoft/BotBuilder/issues/483

How to echo message at telegram /start?

I'd like to set up a authentication through telegram using it's deep linking api.
In order to authenticate, in my app I ask users to click on a link like:
https://telegram.me/myloginbot?start=somesecretkey
If I understand the docs correctly, I should expect the bot to echo back somesecretky to my server.
Now, this step of the docs is unclear to me:
Configure the webhook processor to query Memcached with the parameter that is passed in incoming messages beginning with /start
If I understand correctly, I need to configure myloginbot so that when the user clicks start button on the bot's page, the bot echos back to my server a url containing somesecretkey and some user info. But I don't know how to do so.
In this answer, it is suggested that:
Let the bot retrieve the username by querying the database or key-value storage for unique_code.
But I don't know how can I make the bot query the (presumably remote) database.
So really appreciate your hints.
My understanding to deep linking is this:
You have a database of users. Each user has an ID. Suppose you want your Telegram bot to communicate with user 123. But you don't know his Telegram chat_id (which the bot needs in order to send messages to him). How do you "entice" him to talk to the bot, thus revealing his chat_id? You put a link on a web page.
But the link has to be "personalized". You want each user to press on a slightly different link, in order to distinguish them. One way to do that is to embed user ID in the link. However, user IDs are not something you want to expose, so you generate a (temporary) key associated with each user ID, and embed that key in the link. For example, user 123 has the key abcde. His personalized link will be:
https://telegram.me/myloginbot?start=abcde
Someone clicks on the link, and is led to a conversation with your bot. At the same time (or when he presses the START button), your bot will receive a message:
/start abcde
On receiving that message, the bot sees that abcde is associated with user 123. Telegram chat_id can also be extracted from the message. Now, the bot knows user 123's chat_id, and can send him messages afterwards.
To experiment with deep linking, you need a bot that can handle /start messages, supported by a "datastore" that remembers the key-ID associations. When Telegram docs say "memcache", they just mean something that stores the key-ID associations. For an experiment, it may be as simple as a dictionary, or an associative array. In real life, it may be Memcached (the memory caching software), or a database table.
If you use Python, I recommend taking a look at telepot, a Python framework for Telegram Bot API. It does not do deep linking per se, but it does help you in receiving messages for a bot, and other bot operations in general. I also have an example there demonstrating how to output a personalized link, set up a webhook, and parse the incoming /start command with the key.

Resources