Reputation: 105
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.
Upvotes: 3
Views: 8338
Reputation: 5949
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.
Upvotes: 10