Bot framework - unexpected error when call turnContext.sendActivity method inside Cron callback function

痴心易碎 提交于 2019-12-25 01:35:48

问题


I have some trouble to make scheduled skype notifications.

Error:

(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)

My code:

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');

First call of sendActivity works fine, but second in Cron callback not.

Even if I try to call inside axios then() function its also working.. :

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

Is there is way how to call sendActivity in Cron anonymous function?


回答1:


Try using the async/await method with your Axios request instead of the then/catch method. I've seen this issues in the past with calling sendActivity from a callback function.

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



回答2:


You're best option is to set up the cron job as an external service. Then, set the cron job to make an api call to the bot following your set schedule. When the api is hit, it will send a proactive message.

There are any number of ways to set up a cron job (or something similar), including creating an Azure Function with a timer trigger (doc here).

However, you can easily build a node based javascript service that could make your api calls to the bot.

To start, you would first create the directory and install the node modules needed.

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

npm install express node-cron fs

Next, build the project. You would make your api call (using Axios, for example) in place of the console.log(). You can read more about the following code snippet 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);
[...]

The 16.proactive-messages sample from the Botbuilder-Samples repo demonstrates how to create the api and setup a basic proactive message system.

Hope of help!



来源:https://stackoverflow.com/questions/55382624/bot-framework-unexpected-error-when-call-turncontext-sendactivity-method-insid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!