我不知道如何在 pyTelegramBotApi(远程机器人)上设置 webhook

问题描述 投票:0回答:1

我已经完成了,就像这个官方示例一样:https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/webhook_examples/webhook_aiohttp_echo_bot.py。不过,似乎没有什么效果。也不会压坏。

WEBHOOK_URL 格式为 http://adress.io。 WEBHOOK_PATH = '/'.

import telebot
    from aiohttp import web

    from config import *
    from messages import *

    bot = telebot.TeleBot(TOKEN)


    app = web.Application()
    bot.remove_webhook()
    bot.set_webhook(url=WEBHOOK_URL + WEBHOOK_PATH)

    async def handle(request):
        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)

    # main loop
    if __name__ == '__main__':
        @bot.message_handler(content_types=['text'])
        def reply(message):
            if message.text == "/start":
                bot.send_message(message.from_user.id, MESSAGE_START)
            elif message.text == "/help":
                bot.send_message(message.from_user.id, MESSAGE_HELP)
            elif message.text == "/seeagreement":
                bot.send_message(message.from_user.id, MESSAGE_AGREEMENT)

        web.run_app(
            app,
            host=WEBHOOK_IP,
            port=WEBHOOK_PORT,
        )
telegram-bot aiohttp telegram-webhook py-telegram-bot-api
1个回答
1
投票

我是 Telegram API 和 telebot 的新手,但我看到这个问题没有任何答案,所以我将把我认为可以帮助你找到答案的内容(或者对于任何面临这个问题的人)。

我决定将我的机器人部署到 Heroku 并完成了它想要的一切,但我收到了一个奇怪的错误,即“TeleBot”没有“message_handler”属性,但没有发现 webhook 错误。所以我只想解释一下我做了什么。

我使用了 CPython 代码而不是您使用的 aiohttp 。你的代码经过一些修改就会变成这样(修改与源代码不完全相同,所以看一下):

import telebot
from http.server import BaseHTTPRequestHandler, HTTPServer

from config import *
from messages import *

bot = telebot.TeleBot(TOKEN)

WEBHOOK_HOST = '<ip/host where the bot is running>'
WEBHOOK_PORT = int(os.environ.get('PORT', 5000))
WEBHOOK_LISTEN = '0.0.0.0'

WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % (TOKEN)

async def handle(request):
    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)

# main loop
if __name__ == '__main__':
    @bot.message_handler(content_types=['text'])
    def reply(message):
        if message.text == "/start":
            bot.send_message(message.from_user.id, MESSAGE_START)
        elif message.text == "/help":
            bot.send_message(message.from_user.id, MESSAGE_HELP)
        elif message.text == "/seeagreement":
            bot.send_message(message.from_user.id, MESSAGE_AGREEMENT)

    httpd = HTTPServer((WEBHOOK_LISTEN, WEBHOOK_PORT), WebhookHandler)
    httpd.serve_forever()

对我来说效果很好。

PS:由于这个错误,我将切换到 Telegram pkg(主要 API)(在我住的地方,我无法使用任何外国主机,而且这里的主机太贵了。我只会坚持使用 Heroku看看是否有效)

© www.soinside.com 2019 - 2024. All rights reserved.