【发布时间】: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,已尝试以下但均未解决:
- 删除 webhook
- 删除启动云功能并重新部署
【问题讨论】:
标签: javascript node.js google-cloud-functions telegram-bot telegraf