Create a Telegram Bot With Jovo
Let’s build a Jovo-based Telegram bot
Telegram is a popular online messaging app, like WhatsApp and Facebook Messenger. One thing about Telegram that has caught my attention is how easy it is to set up a bot and get it running with some simple commands with the guidance of the BotFather. As Telegram describes it:
BotFather is the one bot to rule them all. It will help you create new bots and change settings for existing ones.
So, how do we create our Telegram bot? Let’s get started!
Create a Telegram Account
Before you create your account, you need to download the Telegram app. Follow these links to download it from the store of your preference:
Once installed open your browser and go to https://web.telegram.org/. Enter your country and phone number then click “Next”.
It will ask you to confirm your number. Click “OK”. The code will be sent to your phone — copy and paste it to confirm it.
Once you confirm the code, you’re ready to start using Telegram. On an Android phone the screen looks like this:
Create a Telegram bot
As you saw in the image above, I already have a chat with the BotFather bot because I have previously created a bot. The COVID-19 Tracker bot. You can find the BotFather bot by clicking in the search button at the top right of the screen and type: “botfather.” It will be the first result.
Click on it and start setting up your bot:
- Click on the “START” button at the bottom of the screen. You will get a bunch of commands as instructions to talk to BotFather.
- Type “/newbot” to start setting up your first bot.
- Type a name for your bot.
- Finally, type the username of your bot.
Copy the token from the last step — we will use it in the next section.
Connect Your Bot to Dialogflow
Dialogflow is one of the most popular natural language understanding engines. You can connect bots from platforms like Google Assistant, Facebook Messenger, Slack, Twitter, Skype, Telegram, and more.
The first thing we need to do is to create a new Agent for our bot. On the left panel, click on the “Create new agent” menu and give a name to your agent. Then click on “CREATE”.
Now, go to the “Integrations” menu, look for the “Telegram” platform and click on it. In the dialog that will pop up, enter the token you got when you set up the bot in BotFather and click “START”. Then click “CLOSE”.
Run a Jovo Project to Manage the Conversation
Jovo is a cross-platform framework that you can use to build and run voice experiences that work across devices and platforms, including Amazon Alexa, Google Assistant, mobile phones, Raspberry Pi, and more. It also easily connects to bot platforms like Facebook Messenger, Slack, Telegram and more.
Now that we have our bot ready to talk to, and the Dialogflow Agent to understand the conversation, we need to catch the intents spoken by the user and return a response. I have prepared a simple TelegramJovoHelloWorld project that you can use as a template for your bot. Go ahead and clone the repository.
If you are new to Jovo, I recommend you to check their HelloWorld project for beginners.
The following assumes you’re familiar with a Jovo project structure. The first thing we need to do, is create the Dialogflow Agent zip file to restore in Dialogflow. For that, run the following command:jovo deploy
.
This command will only create the zip file of the agent in the following path of the project: /platforms/googleAction/dialogflow_agent.zip.
Go to the Dialogflow agent, click on “Settings”, click on the “Export and Import” tab, and then click on “RESTORE FROM ZIP”.
Upload your zip file, type “RESTORE” in the text box to confirm the action and then click the “RESTORE” button.
The final step to connect Dialogflow to our Jovo project is to add a fulfillment URL. In your computer, go to your terminal window and run the following command:
npm install / yarn installjovo run -w
If you don’t have the Jovo-CLI installed you can run:
npm install -g jovo-cli
When your server is running, you’ll see the Jovo URL for your local machine in the terminal window:
Copy this URL then, in the Dialogflow Agent, on the left panel, go to Fulfillment, enable the Webhook setting, and paste the URL of your server. At the bottom of the page click on the “SAVE” button.
Final Words
At this point you should be able to talk to your Telegram bot and ask: “How are you?” It will reply, “Thanks for asking. I’m doing great, how about you?”
You can host your Jovo project in your own server, or if it’s easier for you, you can save it in Google Cloud or AWS. Once you finish your project you can share it with others with the Telegram URL for your bot. For example:
This is the URL for the bot I created in this post. If you test it out it won’t always work as I don’t have my server running in my machine all the time!