Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Dmitry Kolomatskiy edited this pageMay 26, 2023 ·17 revisions

Introduction

You will need a VPS (or dedicated server) first. Check out the list atWhere to host Telegram Bots if you don't have one already.

First login

Your login details should contain

  • The IP address of your server
  • Your username (usuallyroot)
  • Your password

You should also receive some information on the type of server you got, including Operating System, RAM, storage and monthly traffic.

Once you received that information, you can connect to the server via SSH.

Note that although the explanations below mention entering your password, it is strongly recommended to authenticate using aSSH public key instead of the password. You can readthis explanation on how to generate keys and send the public key to the server, or check your VPS provider's documentation.

Linux

Run the following command in the terminal and replace<user> with your username and<ip> with your servers IP address:

ssh <user>@<ip>

Confirm that you want to trust the host and enter the password if you are asked to do so.

Windows

InstallpuTTY and start it.

In the fieldHost Name (or IP address) enter the IP address of your server. As the connection type, selectSSH and setPort to22. You can save these settings my entering a name in the field belowSaved Sessions and clickingSave. Then, clickOpen and enter your username and password when asked to do so.

Setup

First, install the Python package managerpip.

apt-get update && apt-get install python3-pip

Now, install thepython-telegram-bot library:

pip3 install python-telegram-bot

Finally, confirm the installation:

python3 -c "import telegram;print(telegram.__version__)"

Of course, this is the easy way; the correct way (especially if you have multiple Python projects on your server) is to install avirtual environment (likepipenv orpoetry) first.

Upload your bot's files

Now you can upload your bot to the server. There are multiple ways to do that, one way is to useFileZilla. Install it (if you're on Linux, chances are you already have it) and start it. Open theServer Manager and create a new server with the button on the left. Give it a nice name, then go to the right and fill in the fields:

  • Server: Your servers IP address
  • Port: 22
  • Protocol: SFTP
  • Connection type: Normal
  • Username: Your username
  • Password: Your password

Now, clickConnect. You will probably see the/root directory (your user's home directory) on the right and your local files on the left. Create a directory for your bot and upload all the files needed by your bot into that directory.

Start your bot

To run your bot, connect to your server again via SSH (or go back to the connection) andcd into the directory you created. You could now immediately start the bot, but then it would stop working once you disconnect from the server. There are again several ways to make sure that doesn't happen:

  1. Use a terminal multiplexer likescreen ortmux.
  2. Run your bot as a systemd service.

Running your bot in a terminal multiplexer

screen is called a "terminal multiplexer". It createsvirtual terminals that you can attach to and detach from and that can run processes without you being logged in.

Create a newscreen and attach to it:

screen -S mybot

Start the bot:

python3 bot.py

Detach from thescreen by holdingCTRL and pressingA, thenD. You can now disconnect from the server by typingexit if you want.

To re-attach to thescreen after you logged back in:

screen -r mybot

or

screen -d -r mybot

A known alternative toscreen istmux.

Running your bot as a systemd service

Choosing this option will mean that your Python program will be running in the background. Another upside to this option is that you can set it up to start at system boot, which means that if you reboot your server (e.g. to apply system upgrades), your bot will start automatically afterwards.

  1. Installpython-dotenv.
  2. Create the.env file and put your bot-related environment variables in there (you didn't put your bot token right into your code, hopefully!). If you're using Git, make sure.env is in.gitignore so it doesn't get checked in.
  3. Follow e.g.this tutorial to run your Python script as a service. If you're using a virtual environment (seethis external article for details), you have to locate the Python executable first. To do that, activate the virtual environment on the remote server and enterwhich python in your bash console. You will get a path to the Python instance you will need to use when configuring systemd.

Steps 1 and 2 are optional if you're not using any virtual environment, but it's better to go through them anyway because if you set the environment variables manually, they will be lost after system reboot.

What to read next?

If you plan on hosting multiple bots on your server, it's recommended to usevirtualenv. It allows you to install and upgrade Python modules viapip for one project, without worrying how it affects other projects on the server. Readthis external article for more information.

Learn about how to use a webhook for your bot inthis article.

You might also read the article onPerformance Optimizations if you didn't read it yet.

Wiki ofpython-telegram-bot © Copyright 2015-2025 – Licensed byCreative Commons

Must read

  1. Introduction to the API
  2. Tutorial: Your first bot
  3. FAQ
  4. How to ask good questions
  5. How to write an MWE

Concepts & Important Elements

  1. Architecture Overview
  2. Builder Pattern forApplication
  3. Types of Handlers
  4. Working with Files and Media
  5. Exceptions, Warnings and Logging
  6. Concurrency in PTB

Notable Features

  1. Advanced Filters
  2. Storing data
  3. Making your bot persistent
  4. Adding Defaults
  5. Job Queue
  6. Arbitrarycallback_data
  7. Avoiding flood limits
  8. Webhooks
  9. Bot API Forward Compatiblity

Code Resources

  1. Frequently requested design patterns
  2. Code snippets
  3. Performance Optimizations
  4. Telegram Passport
  5. Bots built with PTB
  6. Automated Bot Tests

Examples explained

  1. InlineKeyboard Example

Networking

  1. Working Behind a Proxy
  2. Handling network errors

Other resources

  1. Where to host Telegram Bots
  2. How to host your bot
  3. Local API Server
  4. Type Checking with PTB
  5. Press
  6. Notes on GAE
  7. Related Projects
  8. Emoji

Transition Guides

Administration

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp