Guide: how to make a simple bot for Telegram in JS for a beginner in programming

I started diving into the IT world only three weeks ago. Seriously, three weeks ago I didn't even understand HTML syntax, and acquaintance with programming languages ​​ended with a 10-year-old Pascal syllabus. However, I decided to go to an IT camp, for whose children it would be nice to make a bot. I figured it wasn't that hard.

This started a long journey, in which I:

  • deployed a cloud server with Ubuntu,
  • registered on GitHub,
  • learned basic JavaScript syntax,
  • read a ton of articles in English and Russian,
  • finally made a bot
  • finally wrote this post.

The end result looked like this:

Guide: how to make a simple bot for Telegram in JS for a beginner in programming

I will say right away that this is an article for beginners - just to understand how to do elementary things from the very beginning.

And also - for advanced programmers - just to make them laugh a little.

1. How to write code in JS?

I understood that it was worth at least to understand the syntax of the language first. The choice fell on JavaScript, simply because the next step for me was to create an application in ReactNative. I started with of course on Codecademy and was very enthusiastic. The first 7 days are free. Real projects. I recommend. The passage took about 25 hours. In fact, not all of it was useful. This is how the course structure looks like and the first block in detail.

Guide: how to make a simple bot for Telegram in JS for a beginner in programming

2. How to register a bot?

Helped me a lot in the beginning this article from the blog of a certain Archakov. He chews on the very beginning. But the main thing that is there is the instructions for registering the bot. I won't write better, and since this is the easiest part, I'll just write the gist. You need to create a bot and get its API. This is done through another bot - @BotFather. Find him in telegram, write to him, follow the simple path and get (save!) API key (this is a set of numbers and letters). It came in handy for me later.

Guide: how to make a simple bot for Telegram in JS for a beginner in programming

3. What does the bot code look like?

After a long study of the articles, I realized that it is worth using some kind of library (third-party code in module format) so as not to suffer from studying the telegram API and creating large pieces of code from scratch. I found a framework telegraph, which needed to be somehow connected to something using npm or yarn. This is how I understood then what the deployment of the bot consists of. Laugh here. I will not be offended. The examples at the bottom of the page helped me the most during the subsequent creation of the bot:

Guide: how to make a simple bot for Telegram in JS for a beginner in programming

3. How to create your own cloud server for 100 rubles

After a lot of searching, I figured out that the 'npm' command from the picture above refers to the command line. The command line is everywhere, but in order to be able to execute it, you need to install the NodePackageManager. The problem was that I was programming on a PixelBook with ChromeOS. I'll skip a big block here about how I got to know Linux - for most, this is empty and unnecessary. If you have Windows or a MacBook, you already have a console.

In a nutshell, I installed Linux through Crostini.

However, in the process, I realized that for the bot to work all the time (and not just when my computer is turned on), I need a cloud server. I chose vscale.io I threw 100 rubles, bought the cheapest Ubuntu server (see picture).

Guide: how to make a simple bot for Telegram in JS for a beginner in programming

4. How to prepare the server to run the bot

After that, I realized that I needed to make some folder on the server, in which I would put the file with the text of the code. To do this, in the console (run directly on the site through the "Open Console" button), I drove

mkdir bot

bot - this became the name of my folder. After that, I installed npm and Node.js, which will allow me to later run code from *.js files

sudo apt update
sudo apt install nodejs
sudo apt install npm

I highly recommend at this stage to set up a connection to the server through your console. Here instruction This will allow you to work with the server directly through the console of your computer.

5. How to code the first bot.

Now this is just a revelation for me. Any program is just lines of text. You can drive them anywhere, save with the desired extension and that's it. You are beautiful. I used Atom, but really, you can just write in a standard notepad. The main thing is to save the file later in the desired extension. It's like writing text in Word and saving it.

I made a new file, in which I inserted the code from the example on the telegraf page and saved it to the index.js file (it's not necessary to name the file like that at all, but it's accepted). Important - instead of BOT_TOKEN, insert your API key from the second paragraph.

const Telegraf = require('telegraf')

const bot = new Telegraf(process.env.BOT_TOKEN)
bot.start((ctx) => ctx.reply('Welcome!'))
bot.help((ctx) => ctx.reply('Send me a sticker'))
bot.on('sticker', (ctx) => ctx.reply(''))
bot.hears('hi', (ctx) => ctx.reply('Hey there'))
bot.launch()

6. How to push the code to the server via github

Now I had to somehow upload this code to the server and run it. For me it became a challenge. In the end, after much ordeal, I realized that it would be easier to create a file on github that allows you to update the code using a command in the console. I registered an account with github and did new projectwhere I uploaded the file. After that, I needed to figure out how to set up uploading files from my (open!) account to the server in the bot folder (if you suddenly left it, just write cd bot).

7. How to upload files to the server via github part 2

I needed to put a program on the server that would download files from git. I installed git on the server by typing

apt-get install git

After that, I needed to set up file uploads. To do this, I drove into the command line

git clone git://github.com/b0tank/bot.git bot

As a result, everything from the project was uploaded to the server. The mistake at this stage was that I essentially made a second folder inside the already existing bot folder. The address to the file looked like */bot/bot/index.js

I decided to ignore this problem.

And to load the telegraf library, which we request in the first line of code, type the command into the console.

npm install telegraf

8. How to start the bot

To do this, while in the folder with the file (to move from folder to folder through the console, run the format command cd bot To make sure that you are where you need to, you can drive in a command that will display in the console all the files and folders that are there ls -a

To start, I entered into the console

node index.js

If there is no error, everything is fine, the bot is working. Look for him on telegram. If there is an error, apply your knowledge from point 1.

9. How to run the bot in the background

You will quickly realize that the bot only works when you yourself are sitting in the console. To solve this problem I used the command

screen

After that, a screen with some text will appear. This means everything is fine. You are on a virtual server on a cloud server. To better understand how it all works - here is the article. Just go to your folder and type in the command to launch the bot

node index.js

10. How the bot works and how to expand its functionality

What can our bot from the example do? He can

bot.start((ctx) => ctx.reply('Welcome!'))

say "Welcome!" at the moment of start (try to change the text)

bot.help((ctx) => ctx.reply('Send me a sticker'))

in response to the standard /help command, send the message "Send me a sticker"

bot.on('sticker', (ctx) => ctx.reply(''))

send approval in response to a sticker

bot.hears('hi', (ctx) => ctx.reply('Hey there'))

answer "Hey there" if they write 'hi'
bot.launch()

Guide: how to make a simple bot for Telegram in JS for a beginner in programming

If you look at the code at github, then you will quickly realize that I have not gone very far from this functionality. What is actively used is the function ctx.replyWithPhoto It allows you to send a given photo or gif in response to certain text.

A significant part of the code was written by children aged 11-13, to whom I gave access to the bot. They entered their user-cases. I think it's easy to tell which part was done by them.

For example, a GIF with a famous character from the Adventure Time cartoon comes to the message β€œjake”.

Guide: how to make a simple bot for Telegram in JS for a beginner in programming

To develop the bot further, you need to connect the keyboard, see examples, for example, hence

11. How to update the code and restart the bot

Do not forget that you need to update the code not only on github, but also on the server. It's easy to do this - stop the bot (press ctrl + c),

- enter into the console, being in the target folder, git pull
- restart the bot with the command node index.js

END

Many of the things described in this file will be super obvious to advanced programmers. However, when I myself tried to jump over the abyss to the world of bots in one fell swoop, I really missed such a guide. A guide that does not miss obvious and simple things for any IT specialist.

In the future, I plan a post on how to make my first application on ReactNative in the same style, subscribe!

Source: habr.com

Add a comment