Telegram Bot部署Google Cloud Functions后次日失效的问题及解决方法
Telegram Bot部署Google Cloud Functions后次日失效问题及解决方法
问题详情
使用Node.js搭配Telegraf库开发Telegram Bot,部署到Google Cloud Functions实现命令触发逻辑。本地运行正常,首次部署后的前10分钟也能正常工作(云函数默认超时设置为1分钟),但次日Bot无法正常响应命令。
原代码
// 导入依赖 bot.start((ctx) => {/* 业务代码 */}); bot.command('Instagram', (ctx) => { /* 业务代码 */ }); bot.command('Collaborazione', (ctx) => { /* 业务代码 */ }); bot.telegram.setWebhook('我的HTTPS触发器地址'); bot.launch(); exports.myBot = functions.https.onRequest(async (request, response) => { functions.logger.info('Hello logs!', { structuredData: true }); return await bot.handleUpdate(request.body, response).then((rv) => { // 如果请求并非来自Telegram,rv会是undefined,但我们仍需返回200状态码 return !rv && response.sendStatus(200); }); });
解决方法
将云函数的异步处理函数改为普通函数,并移除return语句,修改后的代码如下:
exports.myBot = functions.https.onRequest((request, response) => { bot.handleUpdate(request.body, response) })
内容的提问来源于stack exchange,提问作者Gabriele Sabatino
相关产品推荐
相关产品推荐

