我使用Telegraf框架编写了一个简单的Telegram bot,并将其部署到Firebase Cloud函数中,直到有一天晚上出现错误,机器人才停止响应。在做了一些调试之后,我重新部署到了云函数,整个系列的旧消息一直充斥着聊天。
我读到它与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)
})
})我在部署时遵循了这个教程,尝试了以下操作,但都没有解决:
发布于 2020-02-06 03:27:50
您的函数应该在所有条件下向客户端发送响应。现在,您只在handleUpdate返回的承诺表示成功时才发送响应。如果错误响应失败,还应该发送错误响应:
.catch((err) => {
console.log('Function start err', err)
res.send(500)
})这只是一个基本的500错误,但你应该发送任何你想要的。
如果您的函数没有发送响应,那么它将在默认的60秒之后超时。
https://stackoverflow.com/questions/60087262
复制相似问题