简体   繁体   English

使用 pyTelegramBotAPI 将电报机器人部署到 Heroku 时使用开放端口

[英]Use an open port when deploying telegram bot using pyTelegramBotAPI to Heroku

While trying to host a Telegram bot written with pyTelegramBotAPI library and aiohttp webhooks, i have encountered a problem: Telegram only supports webhooks on open ports 80, 88, 443 and 8443. Meanwhile, Heroku docs say: Each web process simply binds to a port, and listens for requests coming in on that port.在尝试托管使用 pyTelegramBotAPI 库和 aiohttp webhook 编写的 Telegram 机器人时,我遇到了一个问题:Telegram 仅支持开放端口 80、88、443 和 8443 上的 webhook。同时,Heroku 文档说:每个 Z2567A5EC9705EB7AC2C984033E01 进程简单地绑定到端口, 并监听来自该端口的请求。 The port to bind to is assigned by Heroku as the PORT environment variable.要绑定的端口由 Heroku 作为 PORT 环境变量分配。 So is there a way to deploy a telegram bot using webhooks on Heroku?那么有没有办法在 Heroku 上使用 webhook 部署电报机器人? I slightly modified this example code from the github repo of pyTelegramBotAPI:我从 pyTelegramBotAPI 的 github repo 稍微修改了这个示例代码:

import os
import ssl
import requests
import telebot
from aiohttp import web

WEBHOOK_HOST = 'pdf-tg-bot.herokuapp.com'
WEBHOOK_PORT = os.getenv('PORT', default=8443)  # 443, 80, 88 or 8443 (port need to be 'open')
WEBHOOK_LISTEN = '0.0.0.0'

WEBHOOK_SSL_CERT = './webhook_cert.pem'  # Path to the ssl certificate
WEBHOOK_SSL_PRIV = './webhook_pkey.pem'  # Path to the ssl private key

WEBHOOK_URL_BASE = "https://{}:{}".format(WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/{}/".format(TOKEN)

app = web.Application()
bot = telebot.TeleBot(TOKEN, parse_mode=None)

@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
    bot.reply_to(message, "Run /new to create a new document.")

# some other message handlers

# Build ssl context
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.load_cert_chain(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV)

# Start aiohttp server
web.run_app(
    app,
    host=WEBHOOK_LISTEN,
    port=WEBHOOK_PORT,
    ssl_context=context,
)

Of course, Heroku binds the web app to $PORT and it runs on https://0.0.0.0:(whatever port Heroku gave the app). Of course, Heroku binds the web app to $PORT and it runs on https://0.0.0.0:(whatever port Heroku gave the app). But this doesn't work with Telegram!但这不适用于电报! If I try to bind it manually to port 8443 with WEBHOOK_PORT = 8443 , I get, as expected, a Heroku error Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch .如果我尝试使用WEBHOOK_PORT = 8443手动将其绑定到端口 8443 ,我会如预期的那样得到 Heroku 错误Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch My Procfile is web: python main.py .我的 Procfile 是web: python main.py What should i do?我应该怎么办?

There's an error in the example.示例中有错误。 You don't need to specify a port in the webhook URL.您无需在 webhook URL 中指定端口。 The port provided by Heroku is for the application to bind to, not for external access to the server. Heroku提供的端口是供应用程序绑定的,不是供外部访问服务器的。

Change this改变这个

WEBHOOK_URL_BASE = "https://{}:{}".format(WEBHOOK_HOST, WEBHOOK_PORT)

to

WEBHOOK_URL_BASE = "https://{}".format(WEBHOOK_HOST)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Heroku 上部署时电报机器人应用程序出现问题 - Issues with telegram bot app when deploying on Heroku 带有 pyTelegramBotAPI 的电报测验机器人 - Telegram Quiz Bot with pyTelegramBotAPI 在heroku上部署电报bot [Procfile] - Deploying telegram bot on heroku [Procfile] requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='api.telegram.org', port=443): # python telegram bot using pytelegrambotapi and Tor - requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='api.telegram.org', port=443): # python telegram bot using pytelegrambotapi and Tor 使用电报机器人 pytelegrambotapi 从群组回复用户的消息 - Reply to user's message from group using telegram bot pytelegrambotapi 通过 pyTelegramBotAPI 在电报机器人中获取照片 - Get photo in telegram bot through pyTelegramBotAPI 将 Discord bot 部署到 heroku 时状态为 0 - Status 0 when deploying Discord bot to heroku 电报机器人(pyTelegramBotAPI)不处理新用户加入组 - Telegram bot (pyTelegramBotAPI) does not handle new user joining group 无法使用 pytelegrambotapi 向 Telegram 频道发送消息 - Unable to send a message to a Telegram channel using pytelegrambotapi 在 PyTelegramBotAPI 上运行的 Telegram Bot 错误地获取转发的消息 - Telegram Bot running on PyTelegramBotAPI is incorrectly getting forwarded messages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM