【发布时间】:2020-03-02 23:48:10
【问题描述】:
我正在尝试将 Telegraf 库与 Firebase Functions 一起使用,但它没有按预期工作。
我遵循这些this article 和出现在webhooks (as appears for express example) 和webhookcallback 中的说明,出现在telegraf 文档中。
const Telegraf = require('telegraf')
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions')
// The Firebase Admin SDK to access the Firebase Realtime or Firestore Database.
const admin = require('firebase-admin')
// set telegraf and responses.
const BOT_TOKEN = 'my-telegram-bot-token'
const bot = new Telegraf(BOT_TOKEN)
bot.start((ctx) => ctx.reply("Start instructions"))
bot.help((ctx) => ctx.reply("This is help"))
bot.hears('hi', (ctx) => ctx.reply('Hola'))
bot.on('text', (ctx) => ctx.reply('Response to any text'))
bot.catch((err, ctx) => {
console.log(`Ooops, ecountered an error for ${ctx.updateType}`, err)
})
// initialize bot
bot.launch() // <-- (2)
//appends middleware
exports.ideas2coolBot = functions.https.onRequest(bot.webhookCallback(`/my-path`));
在 firebase 服务器中,我需要添加 bot.launch() (2) 才能工作,但它仅适用于 Firebase 函数中设置的最大超时。我需要调用 Telegram "setWebhook" API 才能再次开始工作,并且它可以同时工作。就像是生成了一个函数实例,时间到了就关闭了。
我注意到 telegraf.launch() 可以选择以 poll 或 webhook 模式启动,但我不太清楚如何使用它选项。
我应该如何使用 telegram.launch() 在 Firebase 中以 webhook 模式工作?
编辑: 当我使用getWebhookInfo 时,我得到了这个结果:
{
"ok": true,
"result": {
"url": "https://0dbee201.ngrok.io/test-app-project/us-central1/testAppFunction/bot",
"has_custom_certificate": false,
"pending_update_count": 7,
"last_error_date": 1573053003,
"last_error_message": "Read timeout expired",
"max_connections": 40
}
}
控制台显示传入连接但什么也不做...
i functions: Beginning execution of "ideas2coolBot"
i functions: Finished "ideas2coolBot" in ~1s
编辑2:
我也一直在尝试添加 Express...
app.use(bot.webhookCallback('/bot'))
app.get('/', (req, res) => {
res.send('Hello World from Firebase!')
})
exports.ideas2coolBot = functions.https.onRequest(app);
'/' 路径有效,但 '/bot' 却一无所获。 POST 到 '/bot' 没有响应。
顺便说一句,我尝试了一个快速的独立版本并且可以完美运行,但是将它与 firebase 一起使用没有响应(“读取超时已过期”)。
【问题讨论】:
-
这与 Microsoft Bot Framework 有什么关系?
-
没什么,谢谢指正!
-
这就像“ctx.reply”函数不响应 throw express 或者只是 firebase 丢弃“app.use”内容。
标签: firebase google-cloud-functions telegraf telegram-webhook