我正在尝试按照此模板 PyTelegramBotApi webhook_aiohttp 将 webhook 添加到我的代码中。 我的问题是我无法使用 ngrok 从 telegram 接收任何更新。为了使用 webhook 应该做什么?
python -m SimpleHTTPServer 8443
ngrok http 8443
然后输出显示:
======== 在 http://localhost:8443 上运行 ======== (按 CTRL+C 退出)
我的项目结构:
数据库
处理程序
config.py
main.py
from telebot import TeleBot
from membership_bot import config
from membership_bot.handlers.buy import buyHandlerTest, buyHandler
from membership_bot.handlers.join_group import joinGroupHandler
from membership_bot.handlers.start import startHandler, backHomeHandler
from membership_bot.handlers.shipping_handlers import checkout, got_payment
from membership_bot.handlers.account import accountHandler
bot = TeleBot(config.BOT_TOKEN)
bot.register_message_handler(startHandler, commands=['start'], chat_types=['private'], pass_bot=True)
bot.register_callback_query_handler(joinGroupHandler, func=lambda message: message.data == "join_group", pass_bot=True)
bot.register_callback_query_handler(accountHandler, func=lambda message: message.data == "account", pass_bot=True)
bot.register_callback_query_handler(backHomeHandler, func=lambda message: message.data == "back_home", pass_bot=True)
bot.register_callback_query_handler(buyHandler, func=lambda message: message.data.startswith('Buy'), pass_bot=True)
bot.register_pre_checkout_query_handler(checkout, func=lambda query: True, pass_bot=True)
bot.register_message_handler(got_payment, content_types=['successful_payment'], pass_bot=True)
bot.infinity_polling()
我用aio编辑的模板https:
import logging
from aiohttp import web
import telebot
API_TOKEN = 'MyToken'
WEBHOOK_HOST = '123.eu.ngrok.io'
WEBHOOK_PORT = 8443 # 443, 80, 88 or 8443 (port need to be 'open')
WEBHOOK_LISTEN = 'localhost'
WEBHOOK_URL_BASE = "https://{}:{}".format(WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/{}/".format(API_TOKEN)
logger = telebot.logger
telebot.logger.setLevel(logging.INFO)
bot = telebot.TeleBot(API_TOKEN)
app = web.Application()
# Process webhook calls
async def handle(request):
logging.info('Called handle requests........')
if request.match_info.get('token') == bot.token:
request_body_dict = await request.json()
update = telebot.types.Update.de_json(request_body_dict)
bot.process_new_updates([update])
return web.Response()
else:
return web.Response(status=403)
app.router.add_post('/{token}/', handle)
# Handle '/start' and '/help'
@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
logging.info('Called start command........')
bot.reply_to(message,
("Hi there, I am EchoBot.\n"
"I am here to echo your kind words back to you."))
# Handle all other messages
@bot.message_handler(func=lambda message: True, content_types=['text'])
def echo_message(message):
bot.reply_to(message, message.text)
# Remove webhook, it fails sometimes the set if there is a previous webhook
logging.info('Removing webhook........')
bot.remove_webhook()
logging.info('Webhook removed........')
# Set webhook
logging.info('Setting webhook........')
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH)
logging.info('Webhook set........')
# Start aiohttp server
web.run_app(
app,
host=WEBHOOK_LISTEN,
port=WEBHOOK_PORT
)
为了确保您的机器人正常运行并避免将“http://localhost:8443”暴露给“123.eu.ngrok.io”,建议从
WEBHOOK_URL_BASE
中删除该端口。以下是建议的调整:
# To address the exposure issue from 'http://localhost:8443' to '123.eu.ngrok.io',
# it is recommended to eliminate the port from the 'WEBHOOK_URL_BASE' like so:
WEBHOOK_URL_BASE = "https://{}".format(WEBHOOK_HOST)
# By making this change, your bot's webhook URL will be configured to use 'https',