我正试图在cron.schedule()中使用上下文。
this.onTeamsMembersAddedEvent(async (membersAdded, teamInfo, turnContext, next) => {
const members = await TeamsInfo.getTeamMembers(turnContext);
const channels = await TeamsInfo.getTeamChannels(turnContext);
const teamDetails = await TeamsInfo.getTeamDetails(turnContext);
let msteam = await msTeamsWorkspace.findOne({
teamName: teamDetails.name,
teamID: teamDetails.id,
channelID: channels[0].id
});
cron.schedule("* * * * * *", async function(){
var manager_detail = await Users.findById('5edb94e1182d254d5055775e')
turnContext.activity.conversation.id = manager_detail.conversationId;
await turnContext.sendActivity("Hey you got it");
});
await next();
});
错误。
TypeError: Cannot perform 'get' on a proxy that has been revoked
at Task.execution (K:\Project\MSTeams Bot\src\bot\bot.js:187:29)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
第187行:turnContext.activity.conversation.id = manager_detail.conversationId。
基本上,发生这种情况是因为 context
只有在你调用 next()
,所以一旦消息被发送,机器人使用的代理就不再可用了。这就使得使用回调。setInterval
, setTimeout
和 cron
使用起来有点麻烦。推荐的途径是使用 积极主动的信息.
关键步骤是: 。
turnContext.adapter
(见下文)conversationReference
你可以参考。样板这样做 此处adapter
和 conversationReference
发送一个主动的信息,使用 continueConversation
. 该样本是这样做的 此处具体怎么做取决于你和你的机器人的使用案例。但这里有一个快速的代码示例,以显示它的工作与 cron
:
const { ActivityHandler, MessageFactory, TurnContext } = require('botbuilder');
const cron = require('node-cron');
var conversationReferences = {};
var adapter;
class EchoBot extends ActivityHandler {
constructor() {
super();
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
this.onMessage(async (context, next) => {
const replyText = `Echo: ${ context.activity.text }`;
await context.sendActivity(MessageFactory.text(replyText, replyText));
const currentUser = context.activity.from.id;
conversationReferences[currentUser] = TurnContext.getConversationReference(context.activity);
adapter = context.adapter;
cron.schedule('* * * * * *', async function() {
await adapter.continueConversation(conversationReferences[currentUser], async turnContext => {
await turnContext.sendActivity('proactive hello');
});
});
// By calling next() you ensure that the next BotHandler is run.
await next();
});
[...]