Building a chat bot using Nest.js and Telegram – Best Telegram

Building a chat bot using Nest.js and Telegram

Introduction

What we’ll be building

Prerequisites

  • Nest.js: a progressive framework for building efficient and scalable server-side applications; built to take the advantage of modern JavaScript but still preserves compatibility with pure JavaScript.
  • Node-telegram-bot-api: a Node.js module to interact with the official Telegram Bot API.

Setting up the project

$ git clone https://github.com/nestjs/typescript-starter.git nest-telegram-chat-bot
// change directory
cd nest-telegram-chat-bot
// install dependencies
npm install

Installing server dependencies

$ npm install --save node-telegram-bot-api

Creating a Telegram bot

Making request

https://api.telegram.org/bot<YOUR_ACCESS_TOKEN>/getMe
{"ok":true,"result":{"id":591836832,"is_bot":true,"first_name":"new-nest-bot","username":"nest_demo_bot"}}

Initialize the application controller

// ./src/app.controller.tsimport { BotService } from './bot/bot.service';
import { Get, Controller, Res, HttpStatus } from '@nestjs/common';

@Controller()
export class AppController {
constructor(private botService:BotService) {}

@Get()
getBotDialog(@Res() res) {
this.botService.botMessage();
res.status(HttpStatus.OK).send("Bot service started");
}
}

Configure the bot service

// ./src/bot/bot.service.tsimport { Component} from '@nestjs/common';

@Component()
export class BotService {

botMessage() {
process.env.NTBA_FIX_319 = "1";
const TelegramBot = require('node-telegram-bot-api');

const token = 'YOUR_ACCESS_TOKEN';

const bot = new TelegramBot(token, { polling: true });

bot.on('message', (msg) => {
let Hi = "hi";
if (msg.text.toString().toLowerCase().indexOf(Hi) === 0) {
bot.sendMessage(msg.from.id, "Hello " + msg.from.first_name + " what would you like to know about me ?");
}
}
}
  1. Webhook: A dedicated URL or can also be referred to as a web callback.
  2. Long Polling: This allows us to run our application locally without the need for a dedicated server or external address.

Register the component

// ./src/app.module.tsimport { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { BotService } from 'bot/bot.service';

@Module({
imports: [],
controllers: [AppController],
components: [BotService],
})
export class AppModule {}

Running the application

$ npm start

Update the service

// ./src/bot/bot.service.tsimport { Component, OnModuleInit } from '@nestjs/common';

@Component()
export class BotService implements OnModuleInit {

onModuleInit() {
this.botMessage();
}

botMessage() {
...
}
}

Conclusion

Ten articles before and after

The Very First AMA of PopulStay Telegram Community Was Held Successfully on Nov. 30, 2018 – Best Telegram

剛在 Telegram 上開了一個 UI/UX 設計師聊天群組. 點擊加入 ?.. – Best Telegram

Telegram 世界裡的商業「性」行為. 互聯網之所以發達總是因為色情,究竟 Telegram… – Best Telegram

Сбросить кэш FB, VK и Telegram. SMM инструменты – Best Telegram

Telegram CLI Cheatsheet. Telegram CLI is basically full-blown… – Best Telegram

Bee Token Hits 50k on Telegram! Community Update Jan 21, 2018 – Best Telegram

Telegram and The Future of Mobile Messaging – Best Telegram

$10,000 Anypad Telegram Referral Competition – Best Telegram

Telegram Basics. Explaining some end-user concepts of… – Best Telegram

How to Create Telegram Bot in Python – Best Telegram