【问题标题】:Telegraf and Cloud Functions: How to stop Telegram Bot from receiving endless old updateTelegraf 和云功能:如何阻止 Telegram Bot 接收无休止的旧更新
【发布时间】:2020-05-22 01:04:04
【问题描述】:

我使用 Telegraf 框架编写了一个简单的 Telegram 机器人并将其部署到 Firebase Cloud Function,一切都很好,直到有一天晚上出现错误,机器人停止响应。在进行了一些调试之后,我重新部署到 Cloud Functions,而一连串的旧消息不断地充斥着聊天室。

我读到它与getUpdates有关,并且我需要在成功发送消息后发送响应200。这一直存在,但不知何故它不起作用。

这是我的实现:

//at functions/index.js
const functions = require('firebase-functions');
const bot = require('./bot')

exports.start = functions.https.onRequest((req, res) => {
    return bot.handleUpdate(req.body, res)
    .then(() => {
        res.status(200).send();
    })
    .catch((err) => {
        console.log('Function start err', err)
    })
})

// at bot.js
const Telegraf = require('telegraf')
const constants = require('./constants')
const token = constants.telegramBotToken
const bot = new Telegraf(token)

let commands = [
    require('./commands/start'),
    require('./commands/settings'),
    //and a few more commands
]

commands.map((command) => {
    return command(bot)
})

bot.launch()
module.exports = bot

//An example at command code, ie at /commands/start
module.exports = (bot) => bot.start((ctx) => {
    let msg = ctx.message
    let userFirstName = msg.from.first_name
    let startMessage = `
    <b>Hello ${userFirstName}!</b> blahblah`
    var option = {
        "parse_mode": "HTML",
    }

    ctx.reply(startMessage)
    .catch((err) => {
       console.log('/start error', err)
    })
})

我在部署时遵循此tutorial,已尝试以下但均未解决:

  1. 删除 webhook
  2. 删除启动云功能并重新部署

【问题讨论】:

    标签: javascript node.js google-cloud-functions telegram-bot telegraf


    【解决方案1】:

    您的函数应在所有条件下向客户端发送响应。现在,你只在handleUpdate返回的promise表明成功时才发送响应。如果失败,您还应该发送错误响应:

    .catch((err) => {
        console.log('Function start err', err)
        res.send(500)
    })
    

    这只是一个基本的 500 错误,但你应该发送任何你想要的。

    如果你的函数没有发送响应,那么它会在默认的 60 秒后超时。

    【讨论】:

    • 感谢您的回复。问题是这个catch 块中从未捕获到错误,即Function start err 的日志语句从未打印在gcloud 控制台上。特别是,我没有看到任何 Function execution startFunction execution ended 日志,但 Bot 继续爆出这些消息。
    • 好的,好吧,看来您没有展示 handleUpdate 的实现,所以我看不到其他的情况。我也不知道客户端是如何发出请求的,也不知道它是如何决定调用(甚至不调用)该函数的。我从未听说过 HTTP 函数具有您所描述的行为(我已经花了很多时间研究它们),所以可能发生了其他事情。
    • 方法handleUpdate来自Telegraf框架telegraf.js.org/#/?id=handleupdateindex.js 中的机器人是module.exportsbot.js 中的机器人。
    猜你喜欢
    • 2020-11-23
    • 2020-07-16
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 2021-05-29
    • 2020-06-26
    • 2015-10-20
    • 1970-01-01
    相关资源
    最近更新 更多