I have always been fascinated by how chat bots work!
With the trend of the telegram phone app being popular among students, Chat Bots have become a new medium for application interaction.
From digitizing social games like "werewolf" to informational services like getting the latest news update, chat bots have made its way to take those roles, and it has become a source of leisure and information for frequent telegram users.
So here I am, sharing that learning journey on creating my very first Telegram Bot.
Requirements on creating a telegram chat bot:
Simple NGINX service knowledge
A VPS cloud
Telegram API Key
Python telepot library
I have used the following git project by
Adil Khashtamov to setup the telegram bot. The codes are available at https://github.com/adilkhash/planetpython_telegrambot
Step 1 (obtain telegram bot secret key)
To obtain the bot secret key, you will have to use your existing telegram account to start a conversation with
@BotFather. This bot will walk you through on creating a new bot. Start by typing
/start followed by
/newbot. BotFather will ask for a name for your bot and a username for your bot. For example, you can name your bot as
MyFirstApp, and you can set the username as
MyFirstAppBot. Note down the username of your bot, as you will only be able to search your bot in telegram with the username, in which with reference to the example
Step 2 (Understanding telepot, and your first interaction)
First, install telepot.
pip install telepot
Or simply google the correct method for the version of python in which you are using. Once telepot is installed, ensure that your teelgram bot secret key from Step 1 is working. You can do this doing a basic information grabbing method.
import telepot token = 'your telegram secret key' TelegramBot = telepot.Bot(token) print TelegramBot.getMe()
Once done you should get a response with your Bot name and bot username with an ID.
Next, open up your telegram app and start interacting with your Bot. Search your bot by adding a
@ followed by the bot username. Once you have a dialog, send a string the the bot. Let's use
/start as an example.
Once you have sent a message, alter your python script by adding the following line
You should get an update on what messages are being sent out with various ID information such as update ID and chat ID. This ID uniquely identifies the chat. Thus, it is important to know how chats are being identified for future debugging purpose
Time to ignore the codes above, and code some real bot features.
Step 3 (NGINX, Django, and all the Environment Variables)
According to google, "NGINX is a web server which can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache." In Short, NGINX is a web server SERVICE which allows application to be hosted. Our telegram bot functions will be hosted on a server running NGINX. You may use your own VPS Cloud server to run this, or spin up a local VM on your machine bridged to the local network which you are in, along with Network Address Translation rules to ensure traffic from the internet going to your public IP will reach your VM. For simplicity, I have used a server from Digital Ocean, a paid VPS which allows me to host my telegram bot functions.
Adil Khashtamov site on configuring and setting the server up: https://khashtamov.com/en/how-to-deploy-telegram-bot-django/
Step 4 (Getting it running, inserting your code)
If you can get Step 3 running (using Adil's codes), you are one step closer on creating your own bot! (Successfully implementing step 3 means you are able to run this telegram bot functions from your bot. If you are able to achieve this, you are almost there. All that is left right now is replacing his logics with yours and call them!)
Assuming your codes are running right in Step 3, all you need to do is change the codes at
view.py. This is the file in which functions will be inserted.
To understand how the code works, the important information required for the 'reply' function for the bot to reply the use is this following piece of code in
.. .. chat_id = payload['message']['chat']['id'] cmd = payload['message'].get('text') .. ..
Therefore, to visualize the underlying mechanism of how the bot replies, it is something like this:
1. User starts conversation 2. Bot recieves various information including chat_id and message 3. Bot uses the chat_id to identify which 'chat' to reply to 4. Bot sends whatever that is required.
So assuming you just want your bot to reply "Hello World", all you had to add was
TelegramBot.sendMessage(chat_id, 'Hello World') right after the bot receives the chat_id and sent message.
To summarize the "reply" portion, it is important to place the
TelegramBot.sendMessage(chat_id, 'Reply message here') code right after your functions. For instance, you want to pull data off certain API. You will first need to pull the data before placing the code. Take that piece of code as a 'return' function for a telegram bot.
Lastly, once your code is ready, configure
supervisord, according to the link in: https://khashtamov.com/en/how-to-deploy-telegram-bot-django/
Once your services are up, test your bot!
*NOTE: if any changes were made to the codes, you are required to restart the service by using