DEV Community

spiritupbro
spiritupbro

Posted on • Edited on

Create Simple Telegram Bot Using NestJS

Hello guys so yesterday i was learning how to create a telegram bot using nestjs. What is nestjs? nestjs is a framework for nodejs using typescript as their main language much like deno today but its actually a framework for nodejs.

What we create today?

We want to create a telegram bot that can listen to any event of sql update, delete and insert. So whenever we add user, update user and delete user telegram bot will listen to that event and show it in our telegram app.

So, first of all lets create our nestjs app from scratch using this command:

npx nest new project-name
Enter fullscreen mode Exit fullscreen mode

you can fill out project-name with whatever project name you want then after that you will get a directory that look something like this:

d

we will work on directory src only so just ignore any files for now.

so in the src folder you will probably get a really simple file like app modules and all of that. Let me tell you what is all of that so basically nestjs has its idea of creating app much like angular typescript so instead of creating OOP design system. They create it using dependency injection design so instead of creating new constructor every single time why dont we reuse the already create constructor to our use case so basically thats the idea of dependency injection so if you want more detail of that please google yourself coz this is my understanding of dependency injection so now, lets say we want to create a page using handlebars like express js first we create folder views outside of src folder and inside of that views we add index.hbs file.

now add dependency

yarn add @nestjs/platform-express hbs
Enter fullscreen mode Exit fullscreen mode

this for making the nestjs can parse the index.hbs file into html

now in src/main.ts change the inside of the file like this

now you will get a view like this

as

you can basically edit the view whatever you want now is the most important part lets create a telegram library wrapper using telegrafjs and also lets create a typeorm module using sqlite. create a folder and file like this one

s

so this one is the sqlite file for saving our data

a

and this one is the inside of the src folder you can see there is a lib folder for telegram wrapper and you can see module folder for creating all our module for used later we create a telegram module for instantiating the telegram and users module to save our data using typeorm lets see inside telegram module first

so basically all module basically will look something like this you got providers, controllers, imports, and exports. Providers basically is for importing a service or a library because we use our own telegram wrapper we add TelegramLib in it so we can use the telegram wrapper that we create earlier in telegram module. Controllers is actually a route to where our application go much like express router. Imports is for importing other module because we dont use any other module for telegram module we dont import anything and exports is for exporting our service so that other module can use it also.

So lets now take a look at our telegram.service.ts

so basically this one actually start listening to every text that we send to the bot in telegram app whatever we send to telegram app he will send back id and username so its useful for sending back from our bot the event of typeorm later coz telegram bot can only send to particular user if he knows the telegram id of the user.

you can see that in the constructor we add private telegramLib:TelegramLib thats basically the dependency injection we dont need to instantiate every object because of the inject in nestjs we can just use it and also this class implement OnModuleInit basically for starting the class the first time it instantiated

you can take a look the whole application yourself here:

challenge

cara menjalankan

  • follow bot telegram berikut https://t.me/ormtelegram_bot anda akan mendapat id dan username simpan teks tersebut untuk digunakan nanti

  • klik button add telegram user di pojok kanan layar, tambahkan username yang anda dapatkan dari bot, password boleh diisi atau tidak, lalu pada form telegramUser isikan id yang anda dapatkan dari bot

  • klik simpan maka update dari simpanan anda tadi akan masuk ke telegram bot @ormtelegram_bot




Top comments (0)