类型错误:无法在已撤销的代理上执行“获取”

Mad*_*tal 0 node.js botframework

我正在尝试在 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();
    });
Run Code Online (Sandbox Code Playgroud)

错误 :

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)
Run Code Online (Sandbox Code Playgroud)

第 187 行:turnContext.activity.conversation.id = manager_detail.conversationId;

mdr*_*son 5

基本上,发生这种情况是因为context只存在直到您调用next(),因此一旦发送消息,机器人使用的代理将不再可用。这使得使用回调、setIntervalsetTimeoutcron使用起来有点棘手。推荐的路线是使用Proactive Messages

关键步骤是:

  1. 将机器人适配器保存在您可以使用的地方。示例在这里这样做,但它也存在于turnContext.adapter(见下文)
  2. 保存一个conversationReference你可以参考的。示例在这里这样做
  3. 使用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();
        });
[...]
Run Code Online (Sandbox Code Playgroud)