Create Simple Telegram Bot Using NestJS
Tags: javascript, typescript, blogspiritbro1english
Published: 2020-07-13 01:04:46

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
```

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](https://imgur.com/download/cGYXyf0)

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
```

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

{% gist https://gist.github.com/spiritbro1/78153b2fd930cfad7181b008fec6dab1 %}

now you will get a view like this

![as](https://imgur.com/download/erYsXpt)

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](https://imgur.com/download/bc3SRds)

so this one is the sqlite file for saving our data

![a](https://imgur.com/download/pH2YPKs)

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

{% gist https://gist.github.com/spiritbro1/65eb8178876d417e73ab1eb258f3fa0f %}

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:

{% github https://github.com/spiritbro1/ormtelegram %}