Build your first telegram Bot with Nodejs

Build your first telegram Bot with Nodejs

What is telegram?

Telegram is one of top social media apps/platforms that offers security with encryption and on a completely free, fast, and distributed system.

What is a chatbot?

A bot is a software program that operates on the Internet and performs repetitive tasks.

What is a Telegram Bot?

Telegram Bots are accounts operated by software – not people – and they'll often have AI features. ... They can do anything – teach, play, search, broadcast, remind, connect, integrate with other services, or even pass commands to the Internet of Things.

Getting started

Step 1: Getting Permission From BotFather

Sign In to your telegram account and search for BotFather himself and start a conversation with him.

Screenshot_20201127-223612.png

Screenshot_20201127-223700.png

We’ll now invoke the process of creating a new bot with:

/newbot

Screenshot_20201127-223908.png

Give it a name and a username:

Once that process is complete, you’ll get an API Token to use, copy it somewhere, we’ll be using it soon in our application.

Screenshot_20201127-230658.png

Step 2: Set up your project directory

Now that we have Node.js and npm installed and our Telegram account is ready, we can start with the project. Open up your preferred terminal and run these commands:

Create a directory

mkdir movie-finder-telegram-bot

Move into the directory

cd movie-finder-telegram-bot

Now we have a directory for our Telegram bot, but we need a package.json, this tells npm information about our project. To do this, (in the same terminal window) we need to run this:

npm init

This command will tell npm to initialize a pre-made package.json in our project directory. Just hit enter at all of the prompts, we can worry about those later.

Step 3: Install necessary packages

For this project, we will only need one npm package. An npm package is essentially a piece of code (“package”) in the npm registry that we can download with a simple command, npm install.

We’re going to use a package called 'node-telegram-bot-api', we can install it like so:

npm install node-telegram-bot-api

Step 4: Write some code!

Now that we have our bot token, we can use it to interface with the Telegram Bot API.

Create an app.js file in your project directory, then open it in your favourite text editor. Here’s some simple “Welcome message” boilerplate code I’ve written to assist with this step:

Essentially, what this code does is include the library we downloaded from npm earlier, then instantiated a new TelegramBot object with our token and told it to poll for updates from Telegram. Then it will listen for the “text” event (which, in this library, is a simple text message) and then send a message to the chat the original message was saved saying “Hey there”.

Now, run the app.js file with this command:

node app.js

Send your bot a message. It should reply with “Hey there”. If it does, awesome! If it doesn’t, ask me.

gett.png

Now let's create a command that displays details of a movie using omdbapi.com API, You need to go omdbapi.com and get your API KEY..

For us to start the implementation we need to install a dependency called express

npm install express

after install let's write some code to get the movie details from OMDBAPI and display to users.

Send your bot a message with /movie doctor strange. It should reply with the below image If it does, awesome! If it doesn’t, ask me.

Screenshot_20201128-002811.png

Hopefully, this article can be useful for readers. If given the opportunity, I will explain about Node.js again or anyone has a request? Please criticize and advise, do not forget to leave a comment and give Clap 👏👏