Telegram Bots are ready now.
If we use the analogy of web browser and websites, the telegram client applications are like the browser clients.
The Telegram Chatrooms are like websites.
Suppose we have some information we only want to restrict to certain users, on the websites, we will have authentication.
How do we achieve the same effect on the Telegram Bots?
I was told that I can use deep linking. See description here
I will reproduce it below:
- Create a bot with a suitable username, e.g. @ExampleComBot
- Set up a webhook for incoming messages
- Generate a random string of a sufficient length, e.g. $memcache_key = “vCH1vGWJxfSeofSAs0K5PA”
- Put the value 123 with the key $memcache_key into Memcache for 3600 seconds (one hour)
- Show our user the button https://telegram.me/ExampleComBot?start=vCH1vGWJxfSeofSAs0K5PA
- Configure the webhook processor to query Memcached with the parameter that is passed in incoming messages beginning with /start.
If the key exists, record the chat_id passed to the webhook as
telegram_chat_id for the user 123. Remove the key from Memcache.- Now when we want to send a notification to the user 123, check if they have the field telegram_chat_id. If yes, use the sendMessage
method in the Bot API to send them a message in Telegram.
I know how to do step 1.
I want to understand the rest.
This is the image I have in mind when I try to decipher step 2.
So the various telegram clients communicate with the Telegram Server when talking to ExampleBot on their applications. The communication is two-way.
Step 2 suggests that the Telegram Server will update the ExampleBot Server via a webhook. A webhook is just a URL.
So far, am I correct?
What’s the next step towards using this for authentication?
4
Answers
You are correct so far.
However, your requirements are a bit vague. Let’s look at it in another way.
If you want to send restricted information to special users, you should ask user to start a direct chat with your bot or simply use users chat_id in groupchat to start sending message to them.
Please note that you will have access to user chat_id only when the user communicates with the bot in “privacy mode” which is default mode for bots.
Update: I created a GitHub repository with a very simple PHP application to illustrate the concept explained below:
https://github.com/pevdh/telegram-auth-example
Whether you use a webhook or not is irrelevant.
The "deep linking" explained:
Now when your bot receives another message, it can query message.chat.id in the database to check if the message is from this specific user. (And handle accordingly)
Some code (using pyTelegramBotAPI):
Note: the unique_code will not be shown as ‘/start unique_code’, only ‘/start’, in the Telegram client, but your bot will still receive ‘/start unique_code’.
I have just implemented an authentication solution using deep linking for Django.
This solution generates authentication tokens to associate Telegram chats and Django users. When some telegram user wants to access to restricted area it receives a telegram message with a link to login into the web. One logged web site provides a link to start a new authenticated chat using deep linking.
Also there is a demo for a polls example which only the logged in user can’t vote through a telegram.
As of February 2018 you can use Telegram Login Widget to authorize people on your website through Telegram.