【问题标题】:Unable to Intercept Bot Response in Azure Bot Framework无法在 Azure 机器人框架中拦截机器人响应
【发布时间】:2021-09-08 08:43:19
【问题描述】:

我想拦截用户和机器人之间交换的所有消息。我用来拦截的PFB代码。我正在使用 Bot Dialoglog 框架进行网络聊天。这里的问题是我无法从包含从机器人发送给用户的消息的活动对象中提取值。

adapter.use(async (turnContext, next) => {
// pre-processing of the current incoming activity
console.log("adapture use::turnContext::" + turnContext.activity.text);


// hook up a handler to process any outgoing activities sent during this turn
turnContext.onSendActivities(async (sendContext, activities, nextSend) => {
    // pre-processing of outgoing activities
    await nextSend();
    console.log("adapture use::activities::POST##::" + flat.stringify(activities));
    // post-processing outgoing activities
});

await next();

// post-processing of the current incoming activity 
console.log(`Processing activity ${turnContext.activity.id} finishing. `);

});

【问题讨论】:

  • 要更新现有消息,请将具有现有活动 ID 的新 Activity 对象传递给 TurnContext 对象的 updateActivity 方法:docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/…
  • 接受/投票支持更大的 Stack Overflow 社区和任何有类似问题的人。如果您觉得我的回答足够,请“接受”并点赞。如果没有,请告诉我我还能提供哪些帮助!

标签: node.js azure botframework chatbot


【解决方案1】:

要访问机器人的响应,您可以将 bot.js 文件(即定义了活动处理程序的文件)更新为如下所示。

简而言之,onSendActivities() 方法保留了已传入活动的数组。您可以循环遍历将它们推入数组的活动,然后对特定的活动进行操作。或者,在每个到达时做出反应。下面是每个输出的示例。

const { ActivityHandler, MessageFactory } = require('botbuilder');

class EchoBot extends ActivityHandler {
    constructor() {
        super();

        [...]

        const transcript = [];
        this.onTurn(async (context, next1) => {
            let responses = {};

            logActivity(transcript, cloneActivity(context.activity));

            context.onSendActivities(async (ctx, activities, next2) => {
                responses = await next2();
                activities.forEach((a) => logActivity(transcript, cloneActivity(a)));
                console.log('TRANSCRIPT ', activities);
                return responses;
            });

            await next1();
        });
    }
}

const logActivity = (transcript, activity) => {
    if (!activity.timestamp) {
        activity.timestamp = new Date();
    }
    transcript.push(activity);
};

const cloneActivity = (activity) => {
    return Object.assign({}, activity);
};

module.exports.EchoBot = EchoBot;

记录transcript 数组:显示活动列表。

[
  {
    type: 'conversationUpdate',
    id: '1hEUP37Da8S',
    timestamp: 2021-09-07T23:01:04.910Z,
    serviceUrl: 'https://directline.botframework.com/',
    channelId: 'directline',
    from: { id: 'dl_16310556645490.nc93iu9jr1' },
    conversation: { id: '5JgOxxxxxxxxxxxxv6sw-g' },
    recipient: { id: 'somebot@QaeuoeEamLg', name: 'Some Bot' },
    membersAdded: [ [Object], [Object] ],
    rawTimestamp: '2021-09-07T23:01:04.9109865Z',
    callerId: 'urn:botframework:azure'
  },
  {
    type: 'message',
    text: 'Hello and welcome!',
    inputHint: 'acceptingInput',
    speak: 'Hello and welcome!',
    channelId: 'directline',
    locale: undefined,
    serviceUrl: 'https://directline.botframework.com/',
    conversation: { id: '5JgOxxxxxxxxxxxxv6sw-g' },
    from: { id: 'somebot@QaeuoeEamLg', name: 'Some Bot' },
    recipient: { id: 'dl_16310556645490.nc93iu9jr1' },
    timestamp: 2021-09-07T23:01:06.547Z
  },
  {
    type: 'message',
    id: '5JgOxxxxxxxxxxxxv6sw-g|0000001',
    timestamp: 2021-09-07T23:01:08.704Z,
    localTimestamp: 2021-09-07T23:01:08.527Z,
    localTimezone: 'America/Los_Angeles',
    serviceUrl: 'https://directline.botframework.com/',
    channelId: 'directline',
    from: { id: 'dl_16310556645490.nc93iu9jr1', name: '' },
    conversation: { id: '5JgOxxxxxxxxxxxxv6sw-g' },
    recipient: { id: 'somebot@QaeuoeEamLg', name: 'Some Bot' },
    textFormat: 'plain',
    locale: 'en-US',
    text: 'Hi',
    entities: [ [Object] ],
    channelData: {
      clientActivityID: '1631055668527tzwhm47a4qd',
      clientTimestamp: '2021-09-07T23:01:08.527Z'
    },
    rawTimestamp: '2021-09-07T23:01:08.704318Z',
    rawLocalTimestamp: '2021-09-07T16:01:08.527-07:00',
    callerId: 'urn:botframework:azure'
  },
  {
    type: 'message',
    text: 'Echo: Hi',
    inputHint: 'acceptingInput',
    speak: 'Echo: Hi',
    channelId: 'directline',
    locale: 'en-US',
    serviceUrl: 'https://directline.botframework.com/',
    conversation: { id: '5JgOxxxxxxxxxxxxv6sw-g' },
    from: { id: 'somebot@QaeuoeEamLg', name: 'Some Bot' },
    recipient: { id: 'dl_16310556645490.nc93iu9jr1', name: '' },
    replyToId: '5JgOxxxxxxxxxxxxv6sw-g|0000001',
    timestamp: 2021-09-07T23:01:09.147Z
  }
]

仅记录 activities 显示机器人或用户通过的最后一项。

[
  {
    type: 'message',
    text: 'Echo: Hi',
    inputHint: 'acceptingInput',
    speak: 'Echo: Hi',
    channelId: 'directline',
    locale: 'en-US',
    serviceUrl: 'https://directline.botframework.com/',
    conversation: { id: 'AURKxxxxxxxxxJHpO-f' },
    from: { id: 'somebot@QaeuoeEamLg', name: 'Some Bot' },
    recipient: { id: 'dl_16310605730140.3nrm4evq6um', name: '' },
    replyToId: 'AURKxxxxxxxxxJHpO-f|0000001'
  }
]

【讨论】:

  • 感谢@steven-kanberg 提供此解决方案。这就是我要找的。​​span>
【解决方案2】:

谢谢Rajeesh Menoth。发布您的建议作为帮助其他社区成员的答案。

为了更新现有消息,我们可以将带有现有活动 ID 的 NewActivityObject 传递给 TurnContext 对象的 UpdateActivity 方法

以下是传递新对象 ID 的示例

const newActivity = MessageFactory.text('The new text for the activity');
newActivity.id = activityId;
await turnContext.updateActivity(newActivity);

要在按钮选择上更新现有卡片,您可以使用传入活动的ReplyToId

const message = MessageFactory.attachment(card);
message.id = context.activity.replyToId;
await context.updateActivity(message);

更多信息请查看Update Messages

【讨论】:

  • 我想拦截用户和机器人之间的消息交换并且不想更新它所以寻求相同的解决方案。
  • 此外,网络聊天(使用 BotFramework Direct Line)不支持更新和/或删除活动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2018-04-25
  • 2018-07-17
  • 1970-01-01
相关资源
最近更新 更多