Building A Telegram Bot With NodeJS

How to build a Telegram Bot With NodeJS & Ngrok for local development

Why Telegram and Why A Bot

https://telegram.org

Getting Permission From BotFather

Telegram Messenger For IOS
Telegram Web Version
Searching For BotFather on Telegram
Just had to show this graphic of BotFather
Starting a conversation with BotFather
/newbot
Creating A New Telegram Bot With BotFather
Newly Created Telegram Bot API Token

Using Postman To Configure Bot

# HTTP request format
# https://api.telegram.org/bot<token>/METHOD_NAME
# our first method /getMe
https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/getMe
Telegram Bot API Method /getMe
Directly Messaging Our New Bot
API results of our recent interaction /getUpdates

Using Ngrok To Tunnel / Forward Connection

https://ngrok.com
Ngrok Installation
./ngrok http 80
Ngrok running locally
https://da6d9f1d.ngrok.io
POST request to set Telegram Bot Webhook /setWebhook
ngrok http inspect
Messaging our Telegram Bot again
Results in Ngrok from our message

Building NodeJS API

# configure as needed
npm init;
# install dependencies
npm i express axios body-parser --save;
# create main file
touch index.js;
// Dependencies
const express = require('express');
const app = express();
const axios = require('axios');
const bodyParser = require('body-parser');
const port = 80;
const url = 'https://api.telegram.org/bot';
const apiToken = '{api-token-given-by-BotFather}';
// Configurations
app.use(bodyParser.json());
// Endpoints
app.post('/', (req, res) => {
console.log(req.body);
res.send(req.body);
});
// Listening
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
node index.js;# [Expected Output]
# Listening on port 80
Sending another Test Message to our Bot
Ngrok results of successful POST request to our NodeJS API
// Dependencies
const express = require('express');
const app = express();
const axios = require('axios');
const bodyParser = require('body-parser');
const port = 80;
const url = 'https://api.telegram.org/bot';
const apiToken = '{api-token-given-by-BotFather}';
// Configurations
app.use(bodyParser.json());
// Endpoints
app.post('/', (req, res) => {
// console.log(req.body);
const chatId = req.body.message.chat.id;
const sentMessage = req.body.message.text;
// Regex for hello
if (sentMessage.match(/hello/gi)) {
axios.post(`${url}${apiToken}/sendMessage`,
{
chat_id: chatId,
text: 'hello back 👋'
})
.then((response) => {
res.status(200).send(response);
}).catch((error) => {
res.send(error);
});
} else {
// if no hello present, just respond with 200
res.status(200).send({});
}
});
// Listening
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
Notice our bot replying back when the word “hello” is present in our message.

Next Steps

Web Application / Full Stack JavaScript Developer & Aspiring DevOps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store