I started my journey into the world of IT just three weeks ago. Seriously, three weeks ago, I didn't even understand HTML syntax, and my acquaintance with programming languages ended with a school course on Pascal from ten years ago. However, I decided to go to an IT camp where it would be nice for kids to create a bot. I figured it couldn't be that hard.
This marked the beginning of a long journey during which I:
- deployed a cloud server with Ubuntu,
- registered on GitHub,
- learned the basic syntax of JavaScript,
- read a ton of articles in English and Russian,
- finally created a bot,
- and wrote this article, finally.
The final result looked something like this:

I'll say right away, this article is for beginners—to simply understand how to do basic things from the absolute starting point.
And also—for advanced programmers—just to make them laugh a little.
1. How to write code in JS?
I understood that I should at least figure out the language's syntax first. I chose JavaScript simply because my next step was to create an application in ReactNative. I started with Codecademy and was very impressed. The first 7 days are free. Real projects. I recommend it. It took about 25 hours to complete. In reality, not everything from it was applicable. Here’s roughly what the course structure looks like and the first module in detail.

2. How to register a bot?
In the beginning, I was greatly helped by this blog by someone named Archakov. He explains the very beginning. But the main thing they have is an instruction for registering a bot. I couldn't write it better, and since this is the easiest part, I'll just summarize. You need to create a bot and get its API. This is done through another bot—@BotFather. Find him on Telegram, message him, follow the simple steps, and get (save!) the API key (a set of numbers and letters). I found it useful later.

3. What does the bot's code look like?
After studying articles for a long time, I realized that it makes sense to use some library (third-party code in module format) to avoid struggling with Telegram's API and creating large chunks of code from scratch. I found the framework , which had to be somehow connected to something via npm or yarn. That’s roughly how I understood the deployment of the bot at the time. Feel free to laugh here. I won’t be offended. The most helpful were the examples at the bottom of the page when I later created 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 available everywhere, but to run it, you need to install the Node Package Manager. The problem was that I was programming on a PixelBook with ChromeOS. I’ll skip a large block about how I learned Linux—it’s unnecessary for most people. If you have Windows or a MacBook, you already have a console.
In short, I installed Linux via Crostini.
However, during the process, I realized that for the bot to work constantly (not just when my computer is on), I needed a cloud server. I chose I deposited 100 rubles and bought the cheapest Ubuntu server (see the image).

4. How to prepare the server to launch the bot
After that, I realized that I needed to create a folder on the server to place the code file. For this, in the console (you can run it directly on the site by clicking the 'Open Console' button), I typed
mkdir botbot — это стало название моей папки. После этого я установил npm и Node.js, что позволит запускать мне потом код из файликов с разрешением *.js
sudo apt update
sudo apt install nodejs
sudo apt install npmI highly recommend setting up a connection to the server through your console at this stage. Here’s This will allow you to work with the server directly through your computer's console.
5. How to write the code for your first bot.
And now, the simplest thing for me. Any program is just lines of text. You can type them anywhere, save them with the correct extension, and that’s it. You’re amazing. I used , but honestly, you can just write in a standard notepad. The main thing is to save the file with the correct extension afterward. It’s like writing text in Word and saving it.
I created a new file and pasted the code from the telegraf page example into it, saving it as index.js (it's not necessary to name the file that, but it's customary). It’s important to replace BOT_TOKEN with your API key from the second point.
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 needed to somehow upload this code to the server and run it. This became a challenge for me. In the end, after much struggle, I realized it would be easier to create a file on github that allows me to update the code using a command in the console. I registered an account on and created , where I uploaded the file. After that, I needed to figure out how to set up file uploads from my (public!) account to the server in the bot folder (if you accidentally exited, just type cd bot).
7. How to upload files to the server via github part 2
I needed to install a program on the server that would upload files from git. I installed git on the server by typing in the console
apt-get install gitAfter that, I needed to set up file uploads. For this, I typed in the command line
git clone git://github.com/b0tank/bot.git bot
В итоге все из проекта загрузилось на сервер. Ошибкой на данном этапе было, что я, по сути, сделал вторую папку внутри уже существующей папки bot. Адрес до файла выглядел как */bot/bot/index.js
I decided to ignore this issue.
And to load the telegraf library, which we request in the first line of the code, type the command in the console.
npm install telegraf8. How to run the bot
To do this, while in the folder with the file (to switch folders in the console, execute the command in the format cd bot To ensure that you are in the right place, you can type a command that will display all files and folders visible in the console ls -a
To start it, I typed in the console
node index.js
If there are no errors — all is well, the bot is running. Look for it on Telegram. If there is an error — apply your knowledge from step 1.
9. How to run the bot in the background
You will quickly realize that the bot only works when you are sitting at the console. To solve this problem, I used the command
screenAfter that, a screen will appear with some text. That means everything is fine. You are on a virtual server in the cloud. To understand better how this all works — . Just go into your folder and type the command to start the bot
node index.js10. How the bot works and how to expand its functionality
What can our bot from the example do? It can
bot.start((ctx) => ctx.reply('Welcome!'))say 'Welcome!' at the moment of starting (try changing the text)
bot.help((ctx) => ctx.reply('Send me a sticker'))send the message 'Send me a sticker' in response to the standard command /help
bot.on('sticker', (ctx) => ctx.reply(''))send approval in response to a sticker
bot.hears('hi', (ctx) => ctx.reply('Hey there')) reply 'Hey there' if someone writes 'hi'
bot.launch()

If you look at the code in , you'll quickly realize that I haven't strayed far from this functionality. What is actively used is the function ctx.replyWithPhoto It allows sending a specified photo or gif in response to a particular text.
A significant part of the code was written by children aged 11-13, to whom I gave access to the bot. They introduced their user cases. I think it's easy to identify which part was made by them.
For instance, when the message 'jake' is sent, a gif featuring a well-known character from the Adventure Time cartoon is received.

To further develop the bot, you need to connect the keyboard; look for examples, for instance,
11. How to update the code and restart the bot
Don't forget that you need to update the code not only on github but also on the server. It's simple to do: stop the bot (press ctrl+c),
— enter in the console while in the target folder, git pull
— restart the bot again with the command node index.js
END
Many things described in this file will be super obvious to advanced programmers. However, when I myself tried to jump across the gap to the world of bots in one go, I really missed a guide like this. A guide that doesn't skip over things that are obvious and simple for any IT specialist.
In the future, I plan to post about how to create your first application in ReactNative in a similar style, so stay tuned!
Source: habr.com
