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. The port to bind to is assigned by Heroku as the PORT environment variable.
So is there a way to deploy a telegram bot using webhooks on Heroku? I slightly modified this example code from the github repo of pyTelegramBotAPI:
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). 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
. My Procfile is web: python main.py
.
What should i do?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…