We are always in touch
Ukraine: +380674822702
email@singularika.com

How to create a Telegram chatbot

You can create a Telegram chatbot using tools or develop from scratch in any programming language which allows you sending POST requests.

Creating an account

The first step is to create an account and get a token.

To do so, you have to find a Telegram account @BotFather which is described as «one bot to rule them all». Type /start and it will provide the list of available commands. Use /newbot to create your bot, choose its name and username (there is a limitation, the username has to end with the word Bot.

Get the token

Also, you can edit your bot with @BotFather: change its name, profile image, description, “about” info and commands list.

Chatbot functionality development

The next step is to train your bot to perform necessary functions.
We recommend beginning with the chat flow of your bot.
Which features, menu buttons and commands will it have?
You can compose a diagram in any flowchart program, for example Draw.io, or just on a sheet of paper.
If the bot is very simple, like our demo bot, you can skip planning the interaction process and start developing it at once.

The first way is to do it without programming languages.

How to create a Telegram chatbot without coding?

In this case, we use one of the following tools:

@Chatfuel bot that represents the platform (by the way, it is recommended on Telegram official website)

Manybot bot

Flow.XO development platform

Using @Chatfuel bot

Add @Chatfuel bot on Telegram.

It will suggest you what to do next: we type /addbot, after that our guide redirects us to BotFather to create an account (we have accomplished it in advance). Now copy the token and send it to @Chatfuel.

After this you will discover Chatfuel admin panel in your own bot which has been without form and void before. Now you can manage your bot:

add and edit menu, add content to each menu item and set up its behavior (the way it will deliver content), ask a question, send a message to your audience and even create automated posting from various channels (including YouTube, Instagram, Facebook). The abilities of admin panel are represented by command list.

What are disadvantages of this way?

1. There will be Chatfuel branding in your chatbot, and when a person first interacts with it, the advertisement with suggestion to create a bot on this platform will be displayed to your users/

2. Some functions of the constructor are not intuitive and are not described in guidelines. If you have questions, it would be not very easy to find answers.

3. There is no way to add Inline Buttons to your bot, there are only the Сustom Keyboards. Though maybe there is an ability to build buttons of this type and it is simply very hard (or impossible) to find HOW to do it (see point 2). So, dear readers, if you happen to locate this feature, share with us, please!

@Manybot operation principle is similar.

The development of a Telegram bot on Flow.XO platform takes more time, and the interface of this tool is far not so friendly as in the described helpful bots. You have to build chat flow (dialogues) or choose from the templates. By the way, if you are on the free plan, the number of flows is limited. The dialogues are triggered by a defined message. And sometimes the trigger inexplicably fails to work.

The second way: developing a chatbot from scratch (we provide php example)

How to build a Telegram chatbot in php?

Usin the token, we connect the webhook to the bot sending GET request to Telegram API:

https://api.telegram.org/bot<your_bot_token>/setWebhook?url=<webhook_url>

<your_bot_token> is a token provided by BotFather

<webhook_url> is your webhook url (it has to be ssl)

If we have connected the webhook correctly we will receive the following answer:

After we connect the webhook all the updates from chatbot (incoming messages. etc.) will be sent to it in the format of a POST request in JSON format. Here is an example of an incoming message with the text  “Hello bot!”:

This JSON object contains data on the user who has sent a message to the bot and the content of the received message.

So what does our script subscribed to the updates?

It receives events from the bot and selects an answer. Here is an example:

//we write to the string_input variable everything we get to our script and we transform these data to an object
$string_input = file_get_contents(‘php://input’);

//we get from the object all necessaty information, that is chat_id, message content and user name

$id = $input->message->chat->id;

$message = $input->message->text;

$first_name = $input->message->chat->first_name;

When the user presses “START” button beginning the interaction, our bot gets a report about an incoming message with text “/start.” We handle this case and provide an answer (in this case, it is a message with text and two buttons)

Let’s describe what happens when a user presses these buttons:

To send text messages (with buttons or without them) we use a function that will send POST request to Telegram API using curl.

url – https://api.telegram.org/bot<your_bot_token>/sendMessage
id – chat_id
message – the text of the message
buttons – a two-dimensional array with buttons, in this case they are [[“First button”],[“Second button”]] (null by defaultl)

What can you do with this bot now?

Unlike Facebook Messenger and Viber, there is no chatbot registration procedure in Telegram. Your bot is available to everybody and at once. There is even no test mode. It can be found in search and added in a group. You can share it using a link (https://telegram.me/ +bot username, for instance, for our @Xyzqwerty_bot it will be https://telegram.me/Xyzqwerty_bot)
Also, you can add it to Store Bot catalog (storebot.me).

The authors:

Vadim Sharafin, php-developer, dialog systems specialist of Singularika

Valeriia Maliarenko, Singularika copywriter

Contact us!

We respond within 2 hours