Bot Framework - Unexpected Error When Call TurnContext.sendActivity Method Inside Cron Callback Function
Solution 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');
Solution 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!
Post a Comment for "Bot Framework - Unexpected Error When Call TurnContext.sendActivity Method Inside Cron Callback Function"