Telegram is one of the most widely used messaging app in the world. Apart from being used for social conversations, It provides a great number of features for the tech community to use. One of the cool feature of Telegram is creating your own Telegram bot and let other user use it. You can program the bot in any language and setup the connection and your bot is live for other to use.
In this article, we will setup our Telegram bot with Rasa ( A conversational AI framework which allows you to create conversational AI models for users to interact with.
If you are new to Rasa, I strongly suggest you go through How to Create a ChatBot using Rasa and Python — A Step-by-Step Guide (Simple). Rasa provides a way for us to connect different social applications to our chatbot model.
For connecting Telegram bot and Rasa together, we would require few things
- Rasa boilerplate code – You can get from here How to Create a ChatBot using Rasa and Python — A Step-by-Step Guide (Simple)
- Telegram Bot – We will create the bot inside Telegram App
- Ngrok – Converts http network into https network for easy interaction with Telegram. Download Ngrok from here
Telegram Bot
For creating Telegram bot, You only require a Telegram App. Follow the instructions and we will create the bot in less than 5 minutes
- Search for BotFather on Telegram App and open it and select /newbot option from the menu or just type /newbot in the text area.
- Follow the instructions and at the end of it you’ll get two things that we would require in order for Rasa to interact with your Telegram bot. Access_Token and Bot_name that you have given.
Start NGROK
Since Telegram only allows secure endpoints (https) to work with their services, So Ngrok comes in play here. It masks our local http server into a secure https server.
Rasa server by default runs on port 5005, So to put our local server on secure https server do the following steps.
Open Ngrok and type the following
G:\>ngrok.exe http 5005
Code language: Python (python)
Ngrok will then provide you two masked URLs one will be on http and other would be on https. It will look something like this.
Single session for non-premium user is for 2 hours but if it expires you can generate a new one by following the above steps again.
Update Rasa code to interact with Telegram bot
We have credentials.yml
file in our project directory in which we have to make our changes for our model to interact with Telegram bot.
- Open the
credentials.yml
and add these lines in your file, Yourcredentials.yml
file should look like this nowCode language: Python (python)# This file contains the credentials for the voice & chat platforms # which your bot is using. # https://rasa.com/docs/rasa/messaging-and-voice-channels rest: # # you don't need to provide anything here - this channel doesn't # # require any credentials telegram: access_token: "REPLACE_WITH_YOUR_GENERATED_TOKEN_HERE" verify: "REPLACE_WITH_YOUR_BOT_NAME" webhook_url: "REPLACE_WITH_NGROK_HTTPS_URL/webhooks/telegram/webhook" #facebook: # verify: "<verify>" # secret: "<your secret>" # page-access-token: "<your page access token>" #slack: # slack_token: "<your slack token>" # slack_channel: "<the slack channel>" # slack_signing_secret: "<your slack signing secret>" #socketio: # user_message_evt: <event name for user message> # bot_message_evt: <event name for bot messages> # session_persistence: <true/false> #mattermost: # url: "https://<mattermost instance>/api/v4" # token: "<bot token>" # webhook_url: "<callback URL>" # This entry is needed if you are using Rasa X. The entry represents credentials # for the Rasa X "channel", i.e. Talk to your bot and Share with guest testers. rasa: url: "http://localhost:5002/api"
- Once you add these lines, Save and close the file.
Launch Rasa Server and Test
We have made all the required changes. Let’s start our Rasa server by typing the command inside your python environment terminal.
Code language: Python (python)(rasa2) G:\rasa projects\Rasa-boilerplate>rasa run
If everything is fine you’ll get something like this on your terminal.
Open your Telegram App and type Hi in your bot and you’ll see that you are able to interact with your Rasa model on Telegram bot now.
Tha’s it folks for this article, You have a working Telegram bot which you can share with your friends and family to test with.
You can clone the code from Github repo Integrate Telegram with Rasa