Telegram Bot Development 101

Jayesh Bairagi
GAMMASTACK
Published in
6 min readSep 11, 2019

Telegram bots are essentially third-party apps that operate within the Telegram application. By sending messages and commands, telegram users can communicate with the bots. The bots can be used to integrate with other services which can help in adding content from external services. The bots can be used to accept payments, create interactive games using the HTTPS requests provided by the telegram directly. A bot can work as an intelligent agent sending relevant material to users as quickly as it gets released. ImageBot, WikiBot, GmailBot, MusicBot, GitHubBot, YouTubeBot are few of the examples of telegram applications available for you to explore. Users can communicate in any conversation directly from the text input field with these bots via inline queries.

In this tutorial, I will be walking you through the steps to create a telegram bot application.

By the end of this tutorial, you will be able to create a telegram bot application that will be able to respond to commands like /start and also behave in a specific way when certain predefined messages are received as input from the user. We will be using @BotFather to create a new bot.

Registering a new bot using BotFather

In order to register a new bot using BotFather follow the below steps:

  • Search and contact BotFather in the Telegram application
  • Send the command /newBot to the BotFather using the text input field
  • BotFather will now ask to enter a name for the created bot, enter the preferred name like Gammastack
  • Now, the BotFather will ask to create a username for your bot. You can enter something like GammastackContactBot

Alright, we have successfully registered the bot

  • BotFather will send you a token which will be used later for the bot development

Note: Save this token.

Setting the description of the bot

In order to set the description of the bot:

  • Send a command /setdescription to the BotFather using the text input field
  • If you have multiple bots created then, select the bot for which you are writing the description

We are done with the basic registration part of our Telegram bot. Now, we can start working on the node project that will help us in accessing the bot APIs

Node project setup

To set up the node project use npm init and follow the instructions. We will be using node-telegram-bot-api npm package:

  • Install node-telegram-bot-api package for bot development npm install --save node-telegram-bot-api
  • Create an ‘index.js’ file
  • Create a file config.json and save the previously generated token as
{
"token": <YOUR_TELEGRAM_TOKEN>
}
  • Now, in the index.js we will require the node-telegram-bot-api
const TelegramBot = require('node-telegram-bot-api');
  • Now, get the config parameters for the telegram application using
const config = require('./config.json')
  • Now, initialize the telegram bot
const bot = new TelegramBot(config.token, {polling: true});

Note: We are setting polling to true to fetch new updates from our bot.

Implementing the command and message functionalities

Till this point, we are now done with the initialization part for our bot. We are ready to now work on the section where our bot will respond to the commands that the user will enter in order to interact with our bot.

  • We will create a command /start. We will be using the sendMessage function of the telegram bot instance to send the response to the user sending the command. We have mentioned msg.chat.id in order to specify the user who will receive the response. The other parameter to the sendMessage function will contain the response message. We can use msg.chat.first_name to get the first name of the user and msg.chat.last_name to get the last name of the user.
bot.onText(/\/start/, msg => {
bot.sendMessage(
msg.chat.id,
`Hi, ${msg.chat.first_name} ${msg.chat.last_name}.
Welcome to Gammastack's contact telegram bot.
Enter 'HR' to get HR's contact number
Enter 'Sales' to get sales team contact number
Enter 'SalesMail' to get sales team email id
Enter 'Mail' to get company's email id`
)
})

Now, we can start working on the section wherein the bot will respond to the messages that the user will send.

  • To get messages that user sends our bot
bot.on('message', (msg) => {
// Different messages and their respective responses will be
handled here
});
  • Let’s create the message HR and its response:
const hr = 'HR'
if (msg.text === hr) {
bot.sendMessage(msg.chat.id, 'The contact number of HR is
+917312974574')
}
  • Let’s create the message Sales and its response:
const sales = 'Sales'
if (msg.text === sales) {
bot.sendMessage(msg.chat.id, 'The contact number of sales team is
+13022311373')
}
  • Let’s create the message SalesMail and its response:
var salesMail = 'SalesMail'
if (msg.text === salesMail) {
bot.sendMessage(msg.chat.id, 'The email id to get in contact with
the sales team is sales@gammastack.com')
}
  • Let’s create the message Mail and its response:
var mail = 'Mail'
if (msg.text === mail) {
bot.sendMessage(msg.chat.id, 'The email id to get in contact with
the company is contact@gammastack.com')
}

Now, we are done with the setup of our node APIs.

The final structure of our telegram application bot’s index.js should look like:

To start the node APIs for receiving the inputs and sending responses, run node index.js in the terminal.

Now, we are ready to proceed with the final test our bot onto the telegram application.

Search your bot using the search box and select the bot from the results.

  • To test the command /start enter /start in the text input field and press enter and it should show you the responses which we have defined above
  • You can enter the message HR in the text input field and you should get The contact number of HR is +91732974574 as response.
  • You can enter the message Sales in the text input field and you should get The contact number of sales team is +13022311373 as response.
  • You can enter the message SalesMail in the text input field and you should get The email id to get in contact with the sales team is sales@gammastack.com as response.
  • You can enter the message Mail in the text input field and you should get The mail id to get in contact with the company is contact@gammastack.com as response.

That’s it, congratulations you have successfully created your own telegram bot. Feel free to add more features to it.

Conclusion:-

In this article, we have discussed the essential concepts that will come up frequently while developing a telegram bot. Now that you have a basic understanding of how a telegram bot can be developed and its dependencies, we are ready to dive deeper to look into the advanced parts. We will continue with the same application we have built here and apply the advance concepts as we proceed.

Thanks for reading. Hopefully, this guide has been useful to you and will help you to understand fundamental concepts for developing a telegram bot.

You can find the next part here, where we will be going through the advanced parts of the telegram bot development.

If you are looking for Telegram Bot and Telegram Open Network Blockchain Network Development, then GammaStack is a one-stop solution for you. The bots we develop have interactive features such as keyboard functionality, in-app location sharing, cryptocurrency and fiat payment support, etc. Get more details at https://bit.ly/2lPSGeN

--

--