In this tutorial, we'll create a client for this API in the form of a Telegram bot.
Telegram
Table of content
This guide uses Python. You can find the NodeJS versionhere. Ina previous tutorial, we created and hosted an API on Code Capsules. In this tutorial, we’ll create a client for this API in the form of a Telegram bot. This will allow us to pull temperature, weather and exchange rate data on the go by messaging our bot in the Telegram app. We’ll also learn how to host this bot onCode Capsules, soit can be used by others. Along the way, we’ll learn some key concepts about hosting bots securely and efficiently. Let’s get started! To create a Telegram bot, we’ll need: Telegram bots appear as contacts on the Telegram interface. Users interact with Telegram bots by messaging them with commands – these are words preceded by a forward slash, e.g. For example, when we send the command Let’s create a Telegram bot. To create a Telegram bot, we need to downloadTelegramand create a user account. You can use Telegram from either your PC or your phone, or both. Once you have a Telegram account, you can register a new bot by sending a message to BotFather, a bot managed by Telegram themselves. Search for “BotFather” and initiate a chat. From the chat interface, follow these steps: Once you’ve chosen a username, the BotFather will reply with anauthorization token. This is a string that enables your bot to send requests to the Telegram Bot API, similar to the authorization tokens we used to retrieve weather and exchange rate data in the personal API tutorial. Make sure to save this token somewhere safe and private.Requirements
About Telegram Bots
/weather
, or/currency
. Commands sent to the bot’s account on Telegram will be passed to the bot’s backend code (in our case, this will be the code we host on Code Capsules)./weather
to our bot later in this article, the bot will reply with the weather data from our personal API.Registering a Bot Account and Talking to the BotFather
/newbot
.
To see if your bot was successfully created, search for the bot’s username. You should see the bot and be able to start a conversation with it. Currently, our bot won’t reply to anything you send it, as it doesn’t have any backend code yet. Let’s change that.
Planning and Setup
We’re going to implement two commands for our bot.
- When we send the command
/weather
, our bot will reply with the weather data from the API we created. - When we send the command
/currency
, our bot will reply with the exchange rates from USD to CAD, EUR, and ZAR.
Creating a virtual environment and installing requirements
First, we need to create a local directory. Give it the same name as our bot. Then, from this directory, open a terminal and create a Pythonvirtual environmentby entering the following command:
virtualenv env
Enter the virtual environment using the appropriate command for your system:
- Linux/MacOSX:
source env/bin/activate
- Windows:
env\Scripts\activate.bat
The virtual environment will help manage our dependencies for when we host the bot on Code Capsules.
To interact with the Telegram Bot API, we need to install thepython-telegram-botlibrary, a Python wrapper for theTelegram Bot API. We’ll also use the Python libraryrequests
to retrieve data from the weather and currency exchange rate API. To install these requirements, enter the following in your terminal:
pip install python-telegram-bot requests
Retrieving Data from the API
Now we can start coding. Create a file namedbot.py
in the same directory where we activated the virtual environment. In this file, enter the following code, replacingYOUR-URL-HERE
with the URL pointing to the weather and exchange rate API hosted on Code Capsules.
import requestsurl = 'YOUR-URL-HERE/GET'data = requests.get(url) # requests data from APIdata = data.json() # converts return data to json# Retrieve values from APIcurr_temp = data['curr_temp']cad_rate = data['usd_rates']['CAD']eur_rate = data['usd_rates']['EUR']zar_rate = data['usd_rates']['ZAR']def return_weather(): print('Hello. The current temperature in Cape Town is: '+str(curr_temp)+" celsius.")def return_rates(): print("Hello. Today, USD conversion rates are as follows: USD->CAD = "+str(cad_rate)+ ", USD->EUR = "+str(eur_rate)+", USD->ZAR = "+str(zar_rate))return_weather()return_rates()
Here we request the currency and weather data from the API and parse the temperature and conversion rates. Then we print out the data usingreturn_weather()
andreturn_rates()
.
Try it out! Run the program to ensure everything works, then continue.
Creating the Bot
Now we get to create the actual bot. At the top of the bot.py
file, add this line:
from telegram.ext import Updater, CommandHandler
From thepython-telegram-bot
library, we import two classes:Updater
andCommandHandler
. We’ll talk about these classes soon.
We don’t need to print our data anymore – instead, we’ll return a string to our bot, so the bot can display it on Telegram. Replacedef return_weather()
anddef return_rates()
with the following:
def return_weather(): return 'Hello. The current temperature in Cape Town is: '+str(curr_temp)+" celsius."def return_rates(): return "Hello. Today, USD conversion rates are as follows: USD->CAD = "+str(cad_rate)+", USD->EUR = "+str(eur_rate)+", USD->ZAR = "+str(zar_rate)
Now, replace thereturn_weather()
andreturn_rates()
function calls with the code below:
def main(): TOKEN = "YOUR-BOT-TOKEN-HERE" updater = Updater(token=TOKEN, use_context=True) dispatcher = updater.dispatcher weather_handler = CommandHandler("weather", weather) currency_handler = CommandHandler("currency", currency) start_handler = CommandHandler("start", start) dispatcher.add_handler(weather_handler) dispatcher.add_handler(currency_handler) dispatcher.add_handler(start_handler) updater.start_polling()if __name__ == '__main__': main()
At the top of our newmain
method, which will be called when this file is run, we instantiateupdater
, an instance of the Telegram library’sUpdater
class. This object will retrieve commands sent to our bot and pass them to an instance of theDispatcher
class. We’ve assigned thisDispatcher
instance to the variabledispatcher
for further use.
Next, we create three differentCommandHandler
classes, one for each command that can be sent to our bot:/start
,/weather
and/currency
. We pass two arguments into each instantiation: the command text (without the preceding/
), and a function to call. For example, when a user enters the command/weather
, theweather()
function will be called.
Let’s define that function, and the other two. Just abovedef main()
, enter the following three function definitions.
def weather(update, context): context.bot.send_message(chat_id=update.effective_chat.id, text=return_weather())def currency(update, context): context.bot.send_message(chat_id=update.effective_chat.id, text=return_rates())def start(update, context): context.bot.send_message(chat_id=update.effective_chat.id, text="Hi! I respond to /weather and /currency. Try them!")
Each of these functions calls thepython-telegram-bot
functionsend_message()
with the ID of the current chat and the appropriate text, either returned from one of our other functions or specified as a string. Theupdate
andcontext
arguments are supplied automatically by the dispatcher.
Back in ourmain()
function, we usedispatch.add_handler
to add all three handlers to our dispatcher.
Finally,updater.start_polling()
will beginpollingfor updates from Telegram. This means our code will regularly ask Telegram’s servers if any commands have been sent to it. Upon receiving commands, the appropriate handler will be invoked. In thenext section, we’ll discuss the pitfalls of polling and consider an alternative.
The codebot.py
file should now look like the code below. Once again, make sure to replaceYOUR-URL-HERE
with the URL of the API you created in the API tutorial.
from telegram.ext import Updater, CommandHandlerimport requestsurl = 'YOUR-URL-HERE/GET'data = requests.get(url) # requests data from APIdata = data.json() # converts return data to json# Retrieve values from APIcurr_temp = data['curr_temp']cad_rate = data['usd_rates']['CAD']eur_rate = data['usd_rates']['EUR']zar_rate = data['usd_rates']['ZAR']def return_weather(): return'Hello. The current temperature in Cape Town is: '+str(curr_temp)+" celsius."def return_rates(): return "Hello. Today, USD conversion rates are as follows: USD->CAD = "+str(cad_rate)+ ", USD->EUR = "+str(eur_rate)+", USD->ZAR = "+str(zar_rate)def weather(update, context): context.bot.send_message(chat_id=update.effective_chat.id, text=return_weather())def currency(update, context): context.bot.send_message(chat_id=update.effective_chat.id, text=return_rates())def start(update, context): context.bot.send_message(chat_id=update.effective_chat.id, text='Hi! I respond to /weather and /currency. Try these!')def main(): TOKEN = "YOUR-BOT-TOKEN-HERE" updater = Updater(token=TOKEN, use_context=True) dispatcher = updater.dispatcher weather_handler = CommandHandler('weather', weather) currency_handler = CommandHandler('currency',currency) start_handler = CommandHandler('start',start) dispatcher.add_handler(weather_handler) dispatcher.add_handler(currency_handler) dispatcher.add_handler(start_handler) updater.start_polling()if __name__ == '__main__': main()
Below is a conversation with a bot created using this program. Runbot.py
and try it out yourself.
We won’t be able to send messages to our bot if this program isn’t running, so hosting it on Code Capsules will allow us to interact with the bot without having to keep this code permanently running on our development PC.
While we could deploy our bot to Code Capsules in its current state, there is a downside to our current implementation that we should remedy first.
Polling versus Webhooks
There are two ways for ourbot.py
file to receive commands sent to it on Telegram. Currently, the code polls Telegram constantly, regardless of whether the bot is in use. If we hosted this current version on Code Capsules, we would be wasting bandwidth, as the vast majority of polls would return nothing.
Instead of polling Telegram for changes, we can create awebhook. This will allow us to receive commands as they are sent by Telegram users, without having to continuously ask Telegram servers for them.
We’ll set up a webhook by telling Telegram to send commands sent to our bot account to our bot’s Code Capsules URL. Our dispatcher will then process the command using the appropriate handler and send back the requested information.
Creating a webhook
To set up the webhook, replace the lineupdater.start_polling()
in themain
function with the code below:
PORT = int(os.environ.get('PORT', '443')) HOOK_URL = 'YOUR-CODECAPSULES-URL-HERE' + '/' + TOKEN updater.start_webhook(listen='0.0.0.0', port=PORT, url_path=TOKEN, webhook_url=HOOK_URL) updater.idle()
Here we start a webhook that will listen on our Code Capsules URL at TCP port 443 and with the path of our token. Thus, Telegram will relay commands sent to our bot to the following URL:
https://YOUR-CODECAPSULES-SUBDOMAIN.codecapsules.io:443/TOKEN
If you’ve completed some of our other backend tutorials, you will be familiar with setting up web servers that receiveGET
andPOST
requests to different routes. You can think of a webhook as a simple HTTP server that is intended to be used by bots and automated services rather than humans.
Preparing For Deployment
Before we push our code to GitHub and deploy it on Code Capsules, we need to make one small code change and create some files.
Creating an API key environment variable
Because we’ll push our code to GitHub, we need to hide our bot’s authentication key. If we don’t, anyone could use our authentication key and take control of our bot.
Replace this line
TOKEN = "YOUR-BOT-TOKEN-HERE"
with the below
import osTOKEN = os.getenv('BOTAPIKEY')
os.getenv('BOTAPIKEY')
will look for anenvironment variablewith the name “BOTAPIKEY”. When we host our bot on Code Capsules, we’ll set this environment variable to the key we received from the BotFather.
With that done, we must now create some files before we can push our code to GitHub and deploy it on Code Capsules.
Creating a Procfile and requirements.txt
Code Capsules requires a couple of files to deploy our application:Procfile
andrequirements.txt
. The first one tells Code Capsules how to run our application, and the second one tells it which libraries it needs to install.
To create theProcfile
:
- Navigate to the directory containing the
bot.py
file and enter the virtual environment. - Create a file named
Procfile
(with no file extension). - Open
Procfile
, enterweb: python3 bot.py
, and save the file.
In the same directory, open a terminal and activate the virtual environment. Then enterpip3 freeze > requirements.txt
to generate a list of requirements for our Code Capsules server.
Now we can push our code to GitHub. Create a GitHub repository and send therequirements.txt
,Procfile
, andbot.py
files to the repository.
Deploying the Bot to Code Capsules
With all the files sent to GitHub, let’s deploy the bot to Code Capsules:
- Log in to Code Capsules and create a Team and Space as necessary.
- Link Code Capsules to the GitHub repository createdpreviously.
- Enter your Code Capsules Space.
- Create a new Capsule, selecting the “Backend” capsule type.
- Select the GitHub repository containing the bot – leave “Repo subpath” empty and click “Next”.
- Leave the “Run Command” blank and click “Create Capsule”.
We haven’t supplied our webhook a URL yet, and we still need to create an environment variable for our bot’s authorization token. To create an environment variable:
- Navigate to your Capsule.
- Click the “Config” tab.
- Add an environment variable with the name “BOTAPIKEY” and give it your bot’s API key as a value. Make sure to hit the “Update Capsule” button after adding the variable.
Next, let’s supply our webhook with the correct domain.
- Navigate to the “Overview” tab.
- Copy the domain found under “Domains”.
- Open the
bot.py
file and find the lineHOOK_URL = 'YOUR-CODECAPSULES-URL-HERE' + '/' + TOKEN
. - Replace “YOUR-CODECAPSULES_URL” with the domain just copied.
- Commit and push these changes to GitHub.
After pushing these changes, the Capsule will rebuild. Once this is done, the bot is ready. Give it a try!
Further Reading
We’ve covered a lot above, from creating a Telegram bot to the differences between webhooks and polling.
If you’re interested in learning more about what you can do with Telegram bots, check outTelegram’s bot developer introduction. If you have some ideas but require a more profound understanding of the python-telegram-bot
library, browse theirGitHub repository.
You can find a thorough explanation of webhooksin this blog post.