如何使用“ Python Telegram bot”在Pythonanywhere上设置Webhook图书馆

发布于 2025-01-22 07:23:08 字数 314 浏览 4 评论 0原文

谁能帮我吗?我正在将现成的机器人部署到pythonanywhere,当地我的网络怪人正在工作,但是在pyany的地方,我在getwebhookinfo上遇到了404个错误。 我在这里发布了我的代码和屏幕截图: https://github.com/github.com/python-telegram bot/ python-telegram-bot/讨论/2952

can anyone help me? I am deploying my ready bot to pythonanywhere, locally my webhook was working but on pyanywhere I am getting 404 error on getWebhookInfo.
I posted my codes and screenshots Here:
https://github.com/python-telegram-bot/python-telegram-bot/discussions/2952

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぽ尐不点ル 2025-01-29 07:23:08

它对我有用:

# telegram.py
def build():

    application = Application.builder().updater(None).persistence(persistence).token("TOKEN").build()
    ....
    return application

async def run(application, data):
    async with application:
        await application.start()
        await application.update_queue.put(
                Update.de_json(data=data, bot=application.bot)
            )
        await application.stop()


# views.py
def webhook(request, *args, **kw):
    data = json.loads(request.body)
    application = telegram.build()
    asyncio.run(telegram.run(application, data))
    return HttpResponse('Ok')

为了确定持久性,请参阅此示例: https://docs.python-telegram-bot.org/en/v20.0a4/examples.persistentConversationbot.html

我为修复“线程安全”创建了一个“构建”函数。 (应用程序只能运行一个循环)

“运行”功能仅用于帮助我使用应用程序开始 /更新 /停止。 (您可以移动异步视图)。

It works for me:

# telegram.py
def build():

    application = Application.builder().updater(None).persistence(persistence).token("TOKEN").build()
    ....
    return application

async def run(application, data):
    async with application:
        await application.start()
        await application.update_queue.put(
                Update.de_json(data=data, bot=application.bot)
            )
        await application.stop()


# views.py
def webhook(request, *args, **kw):
    data = json.loads(request.body)
    application = telegram.build()
    asyncio.run(telegram.run(application, data))
    return HttpResponse('Ok')

For undestanding the persistence see this example: https://docs.python-telegram-bot.org/en/v20.0a4/examples.persistentconversationbot.html

I create a "build" function for fix "Thread Safe". (Application can be run only one loop)

The "run" function is only for help me with application start / update / stop. (You can be move for the async view).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文