📜 ⬆️ ⬇️

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

I started to dive into the IT world only three weeks ago. Seriously, three weeks ago I didn’t even understand the syntax of HTML, and familiarity with programming languages ​​ended with a school program in Pascal 10 years ago. However, I decided to go to the IT camp, for whose children it would be nice to make a bot. I decided that it was hardly so difficult.

From this began a long way in which I:


The final result looked like this:
')


I will say right away, this is an article for beginners - just to understand how to do basic things from zero point itself.

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

1. How to write JS code?

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



2. How to register a bot?

In the beginning, this article from the blog of a certain Archakov helped me a lot. He chews the very beginning. But the main thing that is there is the instructions for registering the bot. I will not write better, and since this is the easiest part, I will just write the point. You need to create a bot and get its API. This is done through another bot - @BotFather. Find it in the telegram, write to it, go the easy way and get (save!) The API key (this is a set of numbers and letters). He came in handy later.



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 the format of a module), so as not to suffer from learning the telegraph API and creating from scratch large pieces of code. I found a telegraf framework that I needed to somehow connect to something with npm or yarn. Something like this, I understood then what a bot deployment consists of. Laugh here I will not be upset. The following examples helped me most with the subsequent creation of the bot:



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

After a long search, I realized that the 'npm' command from the image above refers to the command line. The command line is everywhere, but in order to execute it, you need to install the NodePackageManager. The problem was that I was programming on PixelBook with ChromeOS. I will skip here a big block about how I learned Linux - for most it is empty and unnecessary. If you have a Windows or MacBook, you already have a console.

In a nutshell, I put Linux through Crostini.

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



4. How to prepare the server to run the bot

After that, I realized that I had to do some kind of 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 via the "Open console" button) I hammered

mkdir bot 

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

 sudo apt update sudo apt install nodejs sudo apt install npm 

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

5. How to write the code of the first bot.

And now just a discovery for me. Any program is just a string of text. They can drive anywhere, save with the desired extension and all. You are beautiful. I used Atom , but really, you can just write in standard notebook. The main thing - then save the file in the desired extension. This is how to write text in Word and save.

I made a new file in which I inserted the code from the example on the telegraf page and saved it in the index.js file (in general, it is not necessary to call the file this way, but this is customary). Important - instead of BOT_TOKEN, insert your API key from the second item.

 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 upload code to the server via github

Now I had to put this code on the server somehow and run it. For me it has become a challenge. In the end, after long ordeals, I realized that it would be easier to create a file on github, which allows you to update the code using the command in the console. I registered my github account and made a new project where I uploaded the file. After that, I needed to understand how to set up uploading files from my account (open!) To the server in the bot folder (if suddenly you left it - just write cd bot).

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

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

 apt-get install git 

After that, I needed to set up file uploads. For this, I hammered 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, in fact, made the second folder inside the already existing bot folder. The address to the file looked like * / bot / bot / index.js

I decided to neglect this problem.

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

 npm install telegraf 

8. How to run the bot

To do this, being in the folder with the file (to go from folder to folder via the console - run the cd bot format cd bot to make sure that you are where you need to drive a command that displays all the files and folders in the console that are there ls -a

For launch, I entered into the console

 node index.js 

If there is no error, all is well, the bot is working. Look for him in the telegraph. If there is an error - apply your knowledge from 1 point.

9. How to run the bot in the background

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

 screen 

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

 node index.js 

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

What can our bot from the example? He can

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

say “Welcome!” at the time of launch (try changing 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('')) 

in response to the sticker send approval

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

Answer “Hey there” if they write to him “hi”
bot.launch ()



If you look at the code on github , you will quickly realize that I have not gone very far from this functionality. What is actively used is the ctx.replyWithPhoto 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 11-13 years old, whom I gave access to the bot. They entered their user case. I think it is easy to determine which part was made by them.

For example, a gif comes to the message “jake” with a famous character from the cartoon Adventure Time.



To develop the bot further, connect the keyboard you need to look at examples, for example, from here

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. To do this is easy - stop the bot (press ctrl + c),

- we enter into the console, being in the target folder, git pull
- re-launch the bot with the node index.js

END

Many of the things described in this file will be super obvious to advanced programmers. However, when I myself was trying 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 the obvious and simple things for any IT specialist.

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

Source: https://habr.com/ru/post/447006/


All Articles