mol*_*vec 2 firebase google-cloud-functions telegraf telegram-webhook
我正在尝试将 Telegraf 库与 Firebase 函数一起使用,但它没有按我预期的那样工作。
我遵循这些本文和webhooks 中出现的说明(如快速示例所示)和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`));
Run Code Online (Sandbox Code Playgroud)
在firebase服务器中,我需要添加bot.launch() (2) 才能工作,但它仅适用于 Firebase 函数中设置的最大超时。我需要回忆一下 Telegram 的“ setWebhook ”API 才能再次工作,而且它可以同时工作。这就像它生成一个函数实例并在时间结束时关闭。
我注意到telegraf.launch()有以轮询或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
}
}
Run Code Online (Sandbox Code Playgroud)
和控制台显示传入连接但什么都不做......
{
"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
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2:
我也一直在尝试添加 Express...
i functions: Beginning execution of "ideas2coolBot"
i functions: Finished "ideas2coolBot" in ~1s
Run Code Online (Sandbox Code Playgroud)
它在'/'路径上有效,但对'/bot'一无所获。POST 到“/bot”没有响应。
顺便说一下,我尝试了一个快速的独立版本并且工作正常,但是将它与 firebase 一起使用没有响应(“读取超时过期”)。
小智 5
删除
bot.launch()
Run Code Online (Sandbox Code Playgroud)
尝试添加这个
exports.YOURFUNCTIONNAME = functions.https.onRequest(
(req, res) => bot.handleUpdate(req.body, res)
)
Run Code Online (Sandbox Code Playgroud)
然后手动设置你的 webhook
https://api.telegram.org/bot{BOTTOKEN}/setWebhook?url={FIREBASE FUNCTION URL}'
| 归档时间: |
|
| 查看次数: |
1943 次 |
| 最近记录: |