【问题标题】:TypeError: Cannot perform 'get' on a proxy that has been revokedTypeError:无法在已撤销的代理上执行“获取”
【发布时间】:2020-09-27 06:37:18
【问题描述】:

我正在尝试在 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;

【问题讨论】:

  • 你能用我的回答来解决这个问题吗?如果是这样,请“接受”并投票,以便其他人可以快速找到答案,我可以从我的支持跟踪器中清除它。如果没有,请告诉我我还能提供哪些帮助!
  • 嘿,你也能回答这个问题吗stackoverflow.com/questions/62953661/…

标签: node.js botframework


【解决方案1】:

基本上,这是因为context 仅在您调用next() 之前存在,因此一旦发送消息,机器人使用的代理将不再可用。这使得使用回调、setIntervalsetTimeoutcron 有点难以使用。推荐路线是使用Proactive Messages

关键步骤是:

  1. 将机器人适配器保存在您可以使用的地方。该示例是这样做的here,但它也存在于turnContext.adapter(见下文)
  2. 保存一个conversationReference,供您参考。示例这样做here
  3. 使用adapterconversationReference 通过continueConversation 发送主动消息。示例这样做here

具体如何执行取决于您和您的机器人的用例。但这里有一个快速的代码示例,显示它可以与 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();
        });
[...]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2021-02-14
    • 2021-02-01
    • 2021-10-25
    • 2020-12-17
    • 2022-08-11
    • 2021-04-26
    相关资源
    最近更新 更多