如何在Google Cloud功能上使用电报Webhook? [英] How to use telegram webhook on google cloud functions?

查看:199
本文介绍了如何在Google Cloud功能上使用电报Webhook?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在google cloud功能上使用python中的webhooks设置了电报机器人.根据互联网上的一些示例代码,我将其作为一个简单的echo bot进行工作,但是其结构与我在使用长轮询之前编写的bot完全不同:

I have set up a telegram bot using webhooks in python on google cloud functions. Based on some sample code from the internet I got it to work as a simple echo-bot however the structure is very different to the bots I coded before using long polling:

# main.py
import os
import telegram

def webhook(request):
    bot = telegram.Bot(token=os.environ["TELEGRAM_TOKEN"])
    if request.method == "POST":
        update = telegram.Update.de_json(request.get_json(force=True), bot)
        chat_id = update.message.chat.id
        # Reply with the same message
        bot.sendMessage(chat_id=chat_id, text=update.message.text)
    return "ok"

我不明白如何为此添加更多的处理程序或其他函数,尤其是因为云函数仅需要我命名一个函数以从脚本中运行(在本例中为webhook函数)

I do not understand how to add any more handlers or different functions to this, especially because cloud functions needs me to name only one function to run from the script (in this case the webhook function).

如何将以上逻辑转换为下面更熟悉的逻辑:

How can I convert the above logic to the one I am more familiar with below:

import os

TOKEN = "TOKEN"
PORT = int(os.environ.get('PORT', '8443'))
updater = Updater(TOKEN)

# add example handler

def start(update, context):
        context.bot.send_message(chat_id=update.message.chat_id, text="Hello, I am dice bot and I will roll some tasty dice for you.")

    start_handler = CommandHandler('start', start)
    dispatcher.add_handler(start_handler)

# start webhook polling

updater.start_webhook(listen="0.0.0.0",
                      port=PORT,
                      url_path=TOKEN)
updater.bot.set_webhook("https://<appname>.herokuapp.com/" + TOKEN)
updater.idle()

此代码与长时间轮询具有相同的结构,因此我知道如何添加其他处理程序.但是它有两个问题:

This code has the same structure as long polling so I know how to add additional handlers. However it has two problems:

  1. 这是 heroku 的文档中的代码段,因此我不知道这对Google Cloud函数是否起作用

  1. It is the code snippet from the documentation for heroku, so I do not know whether this works the same for google cloud functions

不会产生一个我可以在云函数中调用的函数,我尝试将我上面的所有代码包装在一个大函数 webhook 中,然后简单地运行它,但是它不起作用(并且不会在我的Google仪表板上产生错误).

This does not produce one function I can call in cloud functions, I tried wrapping all my code above in one big function webhook and simply running that but it does not work (and does not produce an error on my google dashboard).

感谢您的帮助!

推荐答案

我找到了这个来自yukuku的github telebot回购,并在App Engine上设置了电报机器人,并使用python实现了webhook.如前所述,您可能需要使用App Engine才能在同一 main.py 文件上实现具有许多功能的bot.

I found this github telebot repo from yukuku with the setup of a telegram bot on App Engine and the webhook implementation using python. As mentioned before you may want to use App Engine in order to implement your bot with many functions on the same main.py file.

我刚刚尝试过,并且对我有用.

I just tried and it's working for me.

这篇关于如何在Google Cloud功能上使用电报Webhook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆