Node.js 如何在SDK4中截获bot消息
我有一个SDK 4机器人,将用户交互记录到Blob存储。我还想记录机器人的响应。在SDK 3中,我使用类似于Node.js 如何在SDK4中截获bot消息,node.js,botframework,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, u
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中,我可以配置拦截用户活动的方法,但我似乎无法拦截机器人活动。我似乎在文档中找不到任何东西,但我是SDK 4的新手,可能缺少一些东西
有人知道我如何截获用户和机器人事件,以便我可以记录吗?官方样本库中的Node.js中已经有2个样本:
- 1称为“记录器”:
- 1名被称为“转录本记录器”:
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 }
有关生成的代码的更多详细信息,请参见项目。如果你看一下,要点是:
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];
}
官方样本库中的Node.js中已有2个样本:
- 1称为“记录器”:
- 1名被称为“转录本记录器”:
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 }
有关生成的代码的更多详细信息,请参见项目。如果你看一下,要点是:
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];
}
啊!应该发现那些。非常感谢。不幸的是,上面的链接现在不可用。Doh!应该发现那些。非常感谢。不幸的是,上面的链接现在不可用。