You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Heroku部署我的Telegram Python Bot?附Bot代码片段

Deploying Your Telegram Python Bot to Heroku

Got it, let's walk through deploying your Telegram bot to Heroku step by step—we'll cover all the required files, configuration tweaks, and deployment commands you need to get your bot up and running.

1. Prepare Your Project Files

First, you'll need to add a few critical files to your project directory so Heroku knows how to run your bot:

a. requirements.txt

This file lists all Python dependencies your bot needs. Based on your code snippet, here's what to include (use the latest stable versions that work for you):

pyTelegramBotAPI==4.14.0
requests==2.31.0
pyzbar==0.1.9
Pillow==10.1.0
python-dotenv==1.0.0

Note: sqlite3 is part of Python's standard library, so you don't need to add it here.

b. Procfile

Tell Heroku how to start your bot. Replace your_bot_script.py with the actual filename of your bot code (e.g., bot.py):

worker: python your_bot_script.py

c. runtime.txt

Specify the Python version Heroku should use (pick a version supported by Heroku—check their official documentation for current supported versions):

python-3.11.6

d. Aptfile

Since pyzbar requires the system library libzbar0, create this file to tell Heroku to install it:

libzbar0

e. Update Your Bot Code for Environment Variables

Heroku doesn't support local settings.py files for sensitive data like your bot token. Instead, use environment variables:

  1. Remove the line from settings import TOKEN
  2. Replace it with code to fetch the token from environment variables:
import os
from dotenv import load_dotenv

# Load variables from .env for local development (optional but helpful)
load_dotenv()

TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')

Tip: For local testing, create a .env file with TELEGRAM_BOT_TOKEN=your_actual_token (add .env to .gitignore so you don't commit it).

2. Set Up Heroku

a. Install Heroku CLI

Download and install the Heroku CLI via their official distribution channels.

b. Log In to Heroku

Run this command in your terminal and follow the prompts:

heroku login

c. Create a Heroku App

Choose a unique name for your app (or let Heroku generate one):

heroku create your-unique-bot-name

3. Configure Environment Variables on Heroku

Set your Telegram bot token as an environment variable so your bot can access it:

heroku config:set TELEGRAM_BOT_TOKEN=your_actual_telegram_token

Alternatively, you can add this via the Heroku Dashboard: Go to your app → Settings → Config Vars → Click "Reveal Config Vars" and add the key-value pair.

4. Deploy Your Code

a. Initialize Git (if not already done)

If your project isn't tracked by Git yet:

git init
git add .
git commit -m "Initial bot deployment"

b. Push to Heroku

Link your local Git repo to Heroku and push the code:

heroku git:remote -a your-unique-bot-name
git push heroku main

5. Start the Bot Process

Heroku won't automatically start your worker process, so you need to scale it up:

heroku ps:scale worker=1

6. Check the Bot's Status

To make sure everything is running smoothly, view the logs in real-time:

heroku logs --tail

You should see logs indicating your bot is started and listening for commands.

Important Notes

  • SQLite Limitation: Heroku's filesystem is ephemeral—any data stored in SQLite will be lost when your app restarts. For persistent storage, consider using Heroku Postgres instead.
  • Sleeping Dynos: Free Heroku dynos sleep after 30 minutes of inactivity. If you need your bot to run 24/7, you'll need to upgrade to a paid plan or use a service to ping your dyno periodically.

内容的提问来源于stack exchange,提问作者user6083685

火山引擎 最新活动