How to echo message at telegram /start? - telegram

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.

Related

how to read/receive telegram channel messages in my telegram bot?

i'm trying to create a bot which can read/receive all messages in a specific channel and send them to me .
my problem here is that i can't find a way to access those messages in my bot
important thing is:
i'm not admin or creator of that channel
i don't want to ask the creator to add my bot as administrator
i've searched in google but i wasn't able to find a solution
and i'm also sure that it is possible to do it, because there are already some junction bots with exact same performance .
any references or suggestions are appreciated .
As already pointed out in the Telegram group of python-telegram-bot, bots can't receive messages from channels where they are not an admin. If you want something like this, you'll have to use a user bot, e.g. a program that controls your private Telegram account (in contrast to controlling a bot account). See here for some info on user bots.
Note that also the provider that you linked seems to be using userbots behind the scenes. More precisely, the docs on the so called "DirectConnection" state that you need to enter a phone number (associated to a telegram account) and then "Follow the instructions" which probably just means to enter the verification code that Telegram sends you. Phone number + verification code is exactly what is needed to control your private Telegram account.
Disclaimer: I'm currently the maintainer of python-telegram-bot.

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.

Telegram URL schema: which url to trigger the client to open the "share/send to" dialogue

I am working on an telegram bot, and looking for the exact URL schema to popup the "Share / Select Group / Send to" dialogue in the telegram client.
I have seen this link in #pollbot, for which I took following screenshots.
A bit more details:
What I want is to give end-user an easy way to add the bot to their group, just like what PollBot does: When a poll being created, PollBot will return a special like to the user. Any by clicking that link, the user could easily add PollBot to a group. So I'm looking for the exact URL schema of that link.
I then check that link PollBot sent to me in telegram web client, and found out it's a tg protocol url: tg://resolve?domain=PollBot&startgroup=5148bed5f90678b93246464b3e132052. So I tried to resend this url via bot.sendMessage. But it turned out Telegram api server won't parse the tg://resolve url.
So, how could the bot manage to send such a link to the user?
The link there works with deeplinking, please read about it here:
https://core.telegram.org/bots#deep-linking
the url has the following scheme:
https://telegram.me/<YourBotUsername>?startgroup=<Payload>
the startgroup part (parameter) of the url tells telegram to open the 'add to group' dialog.
when you choose a group there (and you can add bot's to it) the bot will be added to the group and /start PAYLOAD will be send to that group.
The Payload is what you defined as value of the startgroup parameter, so your bot can assosiate it with something and send the matching reply.
please mind, that the Payload can only contain certain characters and don't mind that the telegram clients might internaly convert that link to an tg:// url.
you can also use start instead of startgroup to open a private chat.

How to copy telegram usernames to Spreadsheet

Create a Script or bot to copy the list of usernames from my telegram channel to google spreadsheet.Can i use the bot api to do the job.
First of all, regarding your question - you can't.
But you can try some other things:
You can create a bot that will track the service messages of the channel, and will record the joining/leaving of users. If you have a new channel or with small amount of people - you could do that, and complete the few users already in the channel manually.
You can use Telegram Web to open the list of users of your channel, and then use some javascript to extract the names from the list.
You could also simulate clicks on each user to get more details about it, also automatically.
Another point, is that not everybody has a username. So the only certain thing you can get for sure is their name, which would be at least 1 character.

Resources