Step by Step Guide to Start a Telegram Bot

Tek Loon
4 min readSep 11, 2016

I bumped into an article by Joshua Allen article, My Dead Girlfriend’s Bot. I was touched and decided to spend one of my off day to build my telegram bot.

Then, I asked Mr. Google everything about bot and chat bots. Then, I start dig deeper until I found telegram bot is the easiest for me to start with.

Step 1 : Create your own telegram bot

Download Telegram from App Store or Play Store. Create an account for Telegram. After the account created, search For Bot Father, Press on it and click start.

Type /newbot to create a newbot. Followed by choose a name & username for your bot. Then one token will be generated you are ready to go. Copy down your token and move to step 2.

Step 2: Create your project folder & setting up project dependencies.

Create your project in any folder. Open your folder directory with Terminal (MAC) or Command Prompt (Windows). Run command npm init. It will ask for name, version, description and etc. Just press enter to skip through it.

A package.json file will be created after this step

Next, we are going to setup the project dependencies. We’re going to need 2 dependencies, Express and Telegram Node Bot. Execute the command below right after the npm init command.

npm install express --savenpm install --save telegram-node-bot

Step 3: Hands get dirty part (Coding part)!!

Express library was used to ensure the app set & listen to specific port. This setup is to resolve the app crash issue I faced when I tried to run this bot server app in Heroku. You can comment below if you wish to know more about Heroku.

var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 5000));app.get('/', function(request, response) {

});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});

Now, it’s time to get into the Telegram Setup. I created the most simple use case for the Telegram Node Bot.

Use case: Reply to ‘hi’, ‘hey’, ‘hello’. Simply reply ‘Hey, how are you?’ to all this greeting.

const Telegram = require('telegram-node-bot')
const TelegramBaseController = Telegram.TelegramBaseController
const tg = new Telegram.Telegram('YOUR_TOKEN_GET_IN_STEP_ONE')
class GreetingController extends TelegramBaseController {
/**
* @param {Scope} $
*/
greetingHandler($) {
$.sendMessage('Hey, how are you?')
}
get routes() {
return {
'hey': 'greetingHandler',
'hi': 'greetingHandler',
'hello': 'greetingHandler',
}
}
}
tg.router
.when(['hey', 'hi', 'hello'], new GreetingController())

Step 4: Run & Test part !!

Now let’s test the app to see whether it work as expected. Please type the command below to start the bot app. Then find your bot in Telegram by using global search. In my scenario, I name my app as KissMyAss (Don’t ask me why I use this name, it just cross my mind during that moment). Be aware. It is CASE_SENSITIVE. Thus, auto reply not working for Hi with capital letter

node index.js
Be aware. It is CASE_SENSITIVE. Thus, auto reply not working for Hi with capital letter

Step 5: Deploy to Heroku part !!

Now we have our simple telegram bot and and a server app on how to make response to the user. However, we need a server to run this server app.

If you have your own server, that’s the best scenario. If you’re looking for free server, Heroku is one of the most popular choice.

However, for free account, Heroku have 30 minutes inactivity limit, which means your server app will auto stop after 30 minutes of inactivity and you will have to manually restart the app.

Before you upload your server app to Heroku, please make changes on your package.json file. Replace the line with the line provided below.

"engines": {
"node": "6.5.0",
"npm": "3.10.3"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},

Now, you can upload your app to Heroku.

Follow this guide. Download the heroku CLI for your OS. Ensure you are in your project directory and run the following command. Always refer back to the guide if you encounter any issues for the uploading part. And of course, you’re welcomed to ask as well. Comment on the article and I will help as much as I can.

heroku create
git push heroku master
heroku ps:scale web=1
heroku open

Resources for this project:

Express Official Documentation:

Telegram Node Bot

Heroku Deployment

--

--

Tek Loon

Coder and Writer. If you enjoy my stories— support me by https://www.buymeacoffee.com/tekloon so I can keep writing articles for the community.