如何在SDK4中拦截机器人消息

时间:2018-11-16 22:39:33

标签: node.js botframework

我有一个SDK 4机器人,将用户交互记录到Blob存储中。我也想记录机器人的回应。在SDK 3中,我使用类似...

bot.use({
    // Code for handling message receives
    receive: function (session, event, next) {
        var userId = session.address.user.id;
        logger.logUserConversation(session.text, userId, session.address.conversation.id, userId);
        next();
    },
    // Code for handling message sends
    send: function (event, next) {
        var text = event.text;    
        if(!event.text) {
            text = "Attachments sent";
        }
        logger.logUserConversation(text, 'bot', event.address.conversation.id, event.address.user.id);
        next();
    }
});

在SDK 4中,我可以配置middleware来拦截用户活动,但似乎无法拦截机器人活动。我似乎在文档中找不到任何内容,但是我是SDK 4的新手,可能会丢失一些东西。

有人知道我如何拦截用户和机器人事件,以便我可以登录?

1 个答案:

答案 0 :(得分:2)

官方示例存储库中的Node.js中已经有2个示例:

我尝试了第一个,可以确认它同时记录了用户输入和漫游器回复。它正在记录每个活动,甚至ConversationUpdate

请参阅下面生成的成绩单示例:

 Activity Received: { type: 'conversationUpdate',
  membersAdded: [ { id: '1', name: 'Bot' } ],
  channelId: 'emulator',
  conversation: { id: '36e25420-ec19-11e8-8040-2ba105e71021|livechat' },
  id: '370f7ea0-ec19-11e8-9ee4-fb60855d29c5',
  localTimestamp: 2018-11-19T16:36:07.000Z,
  recipient: { id: '1', name: 'Bot', role: 'bot' },
  timestamp: 2018-11-19T16:36:07.689Z,
  from:
   { id: 'fd3fd64d-6297-4e36-98c5-ee398857f2b6',
     name: 'User',
     role: 'user' },
  locale: '',
  serviceUrl: 'http://localhost:58083' } 

 Activity Received: { type: 'conversationUpdate',
  membersAdded:
   [ { id: 'fd3fd64d-6297-4e36-98c5-ee398857f2b6', name: 'User' } ],
  channelId: 'emulator',
  conversation: { id: '36e25420-ec19-11e8-8040-2ba105e71021|livechat' },
  id: '3711c890-ec19-11e8-9ee4-fb60855d29c5',
  localTimestamp: 2018-11-19T16:36:07.000Z,
  recipient: { id: '1', name: 'Bot', role: 'bot' },
  timestamp: 2018-11-19T16:36:07.705Z,
  from:
   { id: 'fd3fd64d-6297-4e36-98c5-ee398857f2b6',
     name: 'User',
     role: 'user' },
  locale: '',
  serviceUrl: 'http://localhost:58083' } 

 Activity Received: { text:
   'I am a bot that demonstrates custom logging. We will have a short conversation where I ask a few questions  to collect your name and age, then store those values in UserState for later use. after this you will be able to find a log of the conversation in the folder set by the transcriptsPath environment variable Say anything to continue.',
  inputHint: 'acceptingInput',
  channelId: 'emulator',
  serviceUrl: 'http://localhost:58083',
  conversation: { id: '36e25420-ec19-11e8-8040-2ba105e71021|livechat' },
  from: { id: '1', name: 'Bot', role: 'bot' },
  recipient:
   { id: 'fd3fd64d-6297-4e36-98c5-ee398857f2b6',
     name: 'User',
     role: 'user' },
  replyToId: '3711c890-ec19-11e8-9ee4-fb60855d29c5',
  type: 'message',
  timestamp: 2018-11-19T16:36:08.408Z } 

 Activity Received: { type: 'message',
  text: 'test',
  from:
   { id: 'fd3fd64d-6297-4e36-98c5-ee398857f2b6',
     name: 'User',
     role: 'user' },
  locale: '',
  textFormat: 'plain',
  timestamp: 2018-11-19T16:36:23.421Z,
  channelData: { clientActivityId: '1542645367574.7109285295569892.0' },
  entities:
   [ { type: 'ClientCapabilities',
       requiresBotState: true,
       supportsTts: true,
       supportsListening: true } ],
  channelId: 'emulator',
  conversation: { id: '36e25420-ec19-11e8-8040-2ba105e71021|livechat' },
  id: '406fdad0-ec19-11e8-9ee4-fb60855d29c5',
  localTimestamp: 2018-11-19T16:36:23.000Z,
  recipient: { id: '1', name: 'Bot', role: 'bot' },
  serviceUrl: 'http://localhost:58083' } 

 Activity Received: { text: 'What is your name, human?',
  inputHint: 'expectingInput',
  channelId: 'emulator',
  serviceUrl: 'http://localhost:58083',
  conversation: { id: '36e25420-ec19-11e8-8040-2ba105e71021|livechat' },
  from: { id: '1', name: 'Bot', role: 'bot' },
  recipient:
   { id: 'fd3fd64d-6297-4e36-98c5-ee398857f2b6',
     name: 'User',
     role: 'user' },
  replyToId: '406fdad0-ec19-11e8-9ee4-fb60855d29c5',
  type: 'message',
  timestamp: 2018-11-19T16:36:23.443Z } 

有关项目here上可用的生成代码的更多详细信息。如果您看一下,重点是:

if (activity.value === 'endOfInput') {
    console.log(this.conversations[id]);
    var transcriptfileName = util.format('%s/log_%s.transcript', process.env.transcriptsPath, id);
    fs.writeFile(transcriptfileName, JSON.stringify(this.conversations[id], null, 3), function(err) {
        if (err) throw err;
    });
    delete this.conversations[id];
}