To configure the service, you need to:
Credentials to access the server have been sent to your registration email.
Use any SSH client convenient for you (cmd, PuTTY, xShell, etc…).
Use the root connection data for the server.
In this example, the standard Windows CMD will be used.
To connect, enter the command
ssh root@IP-server
Accept the key
Enter the password.
Note that in Linux systems, password characters are not displayed during input.
When the greeting is displayed, the connection is established.
Use FileZilla for file exchange between your local PC and the server.
Host – sftp://IP_server
Login – root
Password – from the server.
Port – 22
Log in to Telegram and enter in the search - BotFather (https://telegram.me/BotFather),
Use the command - /newbot
Enter the bot identifier.
Enter the bot name without special characters and ending with _bot
Save the token of the new bot.
It's time to use all the tools to launch the bot.
Upload your script to the container folder.
For example, use a Python script.
To run the script in a Docker container, you need to create a Docker image and container for your application.
Create a directory for your project – telegrambottemplate, and place the PHP script in it (for example, telegram_bot.py).
import telebot
# Your bot token, which you will receive from BotFather on Telegram
BOT_TOKEN = 'YOUR_BOT_TOKEN'
# Bot initialization
bot = telebot.TeleBot(BOT_TOKEN)
# /start command handler
@bot.message_handler(commands=['start'])
def handle_start(message):
bot.send_message(message.chat.id, "Hello, I am your Telegram bot!")
# Text message handler
@bot.message_handler(func=lambda message: True)
def handle_text(message):
bot.send_message(message.chat.id, f"You wrote: {message.text}")
if __name__ == "__main__":
bot.polling(none_stop=True)
Create a Dockerfile to build a Docker image. Here's a simple Dockerfile example:
# Use the official Python image
FROM python:3.8
# Install the necessary libraries
RUN pip install pyTelegramBotAPI
# Copy the bot script to the container
COPY telegram_bot.py /app/telegram_bot.py
# Specify the command to run the bot
CMD ["python", "/app/telegram_bot.py"]
Now you can build the Docker image using the docker build command. Go to the directory with the Dockerfile and execute the following command.
cd /root/telegrambottemplate
docker build -t my-telegram-bot .
Where my-telegram-bot is the name of your Docker image.
Run the Docker container using the created image by executing the command:
docker run -d my-telegram-bot
As a result of this command, a container will be launched in which your PHP script will be executed. The container will run in the background (flag -d).
Now your Telegram bot will work inside the Docker container. You can set up a webhook and provide external access to the container if necessary.
Check the container status with the command:
docker ps -a
Output
And the message "UP" indicates normal container operation.
Now is the time to check the bot's operation in Telegram.
Enter your bot's name in Telegram or follow the link from the last GodFather message and click the START button.
The bot receives commands and operates according to an algorithm.
You can use and modify existing solutions on the server to create a bot.
Install new, required packages for your project.
Thanks to Docker containerization, you can run multiple projects on one server.
It is also a good platform for testing applications without harming the main system.
If the container gives an error when starting, you can view the container log.
To do this, execute the command
docker logs ID
where ID is the container number.
When changing parameters in the script, the container should be stopped, removed, rebuilt with new data, and restarted.
To do this, execute the commands:
docker stop ID
docker rm ID
cd /root/telegrambottemplate
docker build -t my-telegram-bot .
docker run -d my-telegram-bot
More details:
https://docs.docker.com/