# Telegram bot in Python In this article we will look at how to deploy a telegram bot in python. ## Instructions for uploading via push to git Let's take a ready-made example [echo bot](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot.py) from the creator of the library for working with telegram python-telegram-bot . ```{eval-rst} .. admonition:: Clue :class: hint To get acquainted with the principle of working with git, we recommend `this article `_, which will help you understand how to create a git repository and make changes to it. ``` ### Preliminary preparation - In telegram, through the search, find the “BotFather” bot and ask it to create a new telegram bot. It will ask you to enter the name and username for the bot, after which it will issue a token. - Copy the issued token and paste it into the echo bot example file in place of the word TOKEN in the main() function - Write a YAML file or Dockerfile. For yaml, we have a generator that supports a limited set of environments; in other cases, it is recommended to use [Dockerfile](https://docs.amverum.ru/books/amverum/page/docker). - Create a file with dependencies requirements.txt - Link the repository to Amverum - Make a push to master ### Creating a YAML file Details on how to manually create a configuration file are described in [documentation](../../applications/configuration/config-file.md) or our GUI. ![python_config](../../img/python-tgbot.png) - Select the Python environment and version. - Specify the version and path to the requirements.txt file. It is very important to indicate all the packages used in the project in this file so that the cloud can download them via pip. - We indicate the path to the file containing the entry point to the program (the file that you specify to the Python interpreter when you launch the application). In our case, this is the echobot.py file (if your file is not in the project root folder, then you need to specify the path relative to the project root. If my file was in the src folder, we would indicate src/echobot.py in this field) - If during operation your bot collects some data from the user that should be saved to disk, then it should be placed in the data folder. Otherwise, when you restart the project, all data will be lost! - You can specify any port, since in our case it does not play any role (but in most cases we recommend using port 80, it is the one that is open in Amverum). - Click on the Generate YAML button, after which the amverum.yml file starts downloading. - Place the downloaded file in the root of our project. ### Creating a dependency file - Create a file `requirements.txt`, in which write the line: `python-telegram-bot` ```{eval-rst} .. admonition:: Attention :class: attention Your project may have other dependencies that differ from the dependencies in this example - they all need to be registered correctly. ``` ```{eval-rst} .. admonition:: Clue :class: hint When deploying telegram bots, a common mistake is the incorrect name of "telebot" in `requirements.txt`. Telebot in `requirements.txt` should be named `pyTelegramBotAPI`, not `telebot`. ``` - Call `pip install -r requirements.txt` to install this package. Why not just install the package via `pip install python-telegram-bot`? This is also possible, but the requirements.txt file will still be needed for deployment in cloud, so it’s better to create it right away. - You can check that everything is working by running the bot locally via python3 echobot.py and testing the created bot in telegram (link to the bot can be found at BotFather). - Place your `requirements.txt` at the root of the repository. ### Linking a git repository and pushing to master There are two options: 1. Link to an existing repository (folder). - Initialize the git repository. How to install [git](../../applications/git.rst) if it is not already installed is described [here](https://git-scm.com/book/ru/v2/%D0%92%D0%B2%D0%B5%D0%B4%D0%B5%D0%BD%D0%B8%D0%B5-%D0%A3%D1%81%D1%82%D0%B0%D0%BD%D0%BE%D0%B2%D0%BA%D0%B0-Git). - In the root of our project we give the command: `git init` (if git is already initialized in your project, then this is not necessary) - We link our local Git repository to the remote repository through the command that is indicated on the project page in amverum: ```shell git remote add amverum https://git.amverum.ru//` ``` - Check that all changes are committed by calling `git status`. If there is a list of files there, then you need to commit them by running: ```shell git add . git commit -m "Some informative msg" ``` - Submitting changes to the Amverum repository ```shell git push amverum master ``` When prompted for a user and password, provide your Amverum account user name and password. 2. Use a dedicated Amverum repository (rather than linking your own). - Let's clone an empty repository: ```shell git clone https://git.amverum.ru// ``` - We check that all changes are committed by calling `git status`. If there is a list of files there, then you need to commit them by running: ```shell git add . git commit -m "Some informative msg" ``` - Submitting changes to the Amverum repository ```shell git push amverum master ``` When prompted for a user and password, provide your Amverum account user name and password. ### Application Deployment After the project is pushed into the system, [build](../../applications/build.md) will begin and on the project page the status will change to "Building in progress". As soon as the project is completed, it will move to the [launch](../../applications/run.md) stage and the status will change to “Running” deployment" and then changes to "Successfully deployed". If for some reason the project does not deploy, you can refer to the build logs and application logs for debugging (they may be delayed by 5-10 minutes). If the Project is stuck in the "Building" status for a long time, and the build logs are not displayed, then it is worth checking again the correctness of the `amverum.yaml` file and `requirements.txt` file. ```{eval-rst} .. admonition:: Clue :class: hint If logs are written in print, to display them you need to set the `PYTHONUNBUFFERED` environment variable to 1. ``` ```{eval-rst} .. admonition:: Important :class: warning Save database files and other changeable data to permanent storage to avoid losing them when updating the project when the code folder is “rolled back” to the state of the repository update. The data folder in the project root and the /data directory are different directories. You can check that the save is going to /data by going to the “data” folder on the “Repository” page. ``` ```{eval-rst} .. admonition:: Important :class: warning To avoid the 502 error, change host 127.0.0.1 (or similar localhost) to 0.0.0.0 in your code, and specify in the configuration the port that your application listens to (example - 8080). ``` ## If you are unable to deploy the project Write the symptoms you observe to support@amverum.ru indicating your username and project name, we will try to help you.