How to build a Chatbot with Telegram and Dialogflow (previously – including a Python Webhook

The title pretty much tells you every thing I’m going to cover here. Therefore to make things easier,

If you would like to dive straight into Webhook code, go to my Github Repo here.
If you would like to test the bot out, you can either Talk to the Bot on Telegram or Talk to the Bot on (As of October 2017, was renamed as Dialogflow)

The outcome of this tutorial is to build a bot that can be integrated with all kinds of messaging platforms easily (e.g Facebook Messenger), therefore Telegram-specific APIs are not used. Read on!

1. Creating a bot instance on Telegram

Thanks to Telegram’s great documentation, I will only need to show you a few basic steps. Go to Telegram and talk to the BotFather. Here are some screenshots to help you understand what happens when you speak to the father.

This slideshow requires JavaScript.

Not too difficult, is it? However, this bot has absolutely NO functions. No matter what you say to it, it doesn’t reply. Hence, you’ll have to either write your own code using Telegram’s Bot API or use a platform such as, which makes building and integrating so much simpler and faster!

2. Building your agent

2a. Creating an agent

Create an account, create an agent. This part needs no explanation..

Creating your agent

2b. Integrating with your Telegram instance

After creating your agent, integrate with your Telegram instance by using the the “Integrations” option on the left navbar. Turn on the Telegram option, and use the Telegram Token given to you by the Botfather when you first created your bot. Then, deploy your bot on Heroku with just one click. That’ll bring you to Heroku, and you can fill in the blanks as you please.

2c. Enable Small Talk

Well, you’d want your bot to be able to answer simple questions like “Hi ” and “How are you?” ain’t it? Therefore, you should use the free Small Talk plugin provided by, and customise it as you please. There are lots more Prebuilt Agents you could add in, and they are fully customizable.


2d. Create Intents

You can think of Intents as “User intentions when speaking to a bot”. In this case, it will be to validate an NRIC number, which is how the Singaporean government uniquely verify all citizens. It consists of a starting alphabet, followed by 7 numbers and ending with an alphabet which is a checksum calculate using the previous numbers. For example, “S1234567D” is a valid NRIC number.

Create intentNRIC validator intent

As you have noticed above, “@sys.any” is an entity type defined by For example, if the entity you are trying to capture is a date, it would be “”. You can create entities for your bot to recognize as well, as seen right under “Intents” if the left navbar. Here, what I am trying to do is to capture the parameter “NRIC”, to process the validity of an NRIC number. I wished there was a “@sys.alphanumeric” entity though. **Hint Hint**

So, I caught the NRIC parameter by the user. Now what? I would need to have some customised code to check the validity of an NRIC right? does not provide that, and hence we are using a webhook.

2e. Creating your Webhook

Well, I took a look at the webhook example, and combined it with Sam Liew’s NRIC validation logic. My “validateNRIC” webhook code is public on Github, so I will not be explaining the intricacies of the Python code here.
However, you will have to

  • Host your webhook code on a site such as Heroku
  • Integrate it by using the “Fulfillment” option on
  • Go to the specific “Intent” that you want the webhook to fulfill, and click “Fulfill with Webhook”

The images below give you a brief overview. Wouldn’t be too hard. If it is, feel free to reach out to me 🙂

This slideshow requires JavaScript.

As you can see from the slideshow above, you can test your bot and see the JSON replies on the right side of the

3. Conclusion

Your chatbot should then work like this on Telegram!

Telegram success.PNG

Extremely easy, isn’t it? Moving on from this, you can add more intents, and train your bot to understand variable user queries by using the “Training” option of the left navbar. Essentially, it helps you sieve through conversation history and allow you to tag user queries to intents.

This is a very simple chatbot example, but I hope you got what you needed from this tutorial. If you see any areas for improvement or anything you feel I’ve missed out on, do let me know. Feel free to direct any questions to me in the comment box below as well. 🙂


3 thoughts on “How to build a Chatbot with Telegram and Dialogflow (previously – including a Python Webhook

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s