Bot框架 - 在Cron回调函数中调用turnContext.sendActivity方法时出现意外错误

问题描述 投票:0回答:2

制作预定的Skype通知我有些麻烦。

错误:

(node:3720) UnhandledPromiseRejectionWarning: TypeError: Cannot perform 'get' on a proxy that has been revoked

at CronJob.<anonymous> (C:\bots\clean\bot.js:101:43)
at CronJob.fireOnTick (C:\bots\clean\node_modules\cron\lib\cron.js:554:23)
at Timeout.callbackWrapper [as _onTimeout] (C:\bots\clean\node_modules\cron\lib\cron.js:621:10)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)

(node:3720) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 61)

我的代码:

await turnContext.sendActivity('Successful write to log.');        
var CronJob = require('cron').CronJob;
new CronJob('*/5 * * * * *', async function() {
    console.log('Executed');
    await turnContext.sendActivity('Executed'); //here is error
}, null, true, 'Europe/Riga');

sendActivity的第一次调用工作正常,但在Cron回调中没有第二次调用。

即使我试图调用axios then()功能,它也工作..:

axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(async function (response) {
     await turnContext.sendActivity('Executed');
  })

有没有办法如何在Cron匿名函数中调用sendActivity

javascript cron botframework anonymous-function
2个回答
0
投票

尝试将async / await方法与Axios请求一起使用,而不是使用then / catch方法。我在过去看过这个问题,从回调函数调用sendActivity。

const res = await axios.get('/user', { params: { ID: 12345 }});
await turnContext.sendActivity('Executed');

0
投票

您最好的选择是将cron作业设置为外部服务。然后,设置cron作业,按照设定的时间表对机器人进行api调用。当api被击中时,它将发送主动消息。

有许多方法可以设置cron作业(或类似的东西),包括使用计时器触发器创建Azure函数(doc here)。

但是,您可以轻松地构建基于节点的javascript服务,该服务可以使您对机器人进行api调用。

首先,您将首先创建目录并安装所需的节点模块。

mkdir cron-jobs-node cd cron-jobs-node
npm init -y

npm install express node-cron fs

接下来,构建项目。您可以进行api调用(例如使用Axios)代替console.log()。您可以阅读以下代码片段here的更多信息。

// index.js
const cron = require("node-cron");
const express = require("express");
const fs = require("fs");

app = express();

// schedule tasks to be run on the server   
cron.schedule("* * * * *", function() {
    console.log("running a task every minute");
});

app.listen(3128);
[...]

来自Botbuilder-Samples repo的16.proactive-messages sample演示了如何创建api并设置基本的主动消息系统。

希望有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.