Botframework v4 Directline 集成:有没有办法获取从直接传输到聊天机器人(nodejs 代码)生成的对话 ID
Botframework v4 Directline integration: Is there a way to get the conversation id generated from directline transfer to the Chatbot (nodejs code)
这可能很简单,但我找不到与此相关的任何参考。
我使用直线 API 将聊天机器人集成到网络应用程序中;我正在使用此 API 生成对话 ID:
POST: https://directline.botframework.com/v3/directline/conversations
我正在尝试从上面 API(从网络应用程序代码)获取生成的对话 ID 到聊天机器人代码(NodeJS)。有没有办法或任何参考来做到这一点?
根据 this 问题评论的一种方法是在开始对话之前使用以下方式向机器人发送数据:
var params = BotChat.queryParams(location.search);
var my_token = params['my_token'];
var botConnection = new BotChat.DirectLine({
secret: 'DIRECTLINE_SECRET'
});
BotChat.App({
botConnection: botConnection
,user: { id: 'USER_ID', name: 'User' } // user.id auto updates after first user message
}, document.getElementById("bot"));
botConnection.connectionStatus$.subscribe(function (status) {
if (status == 2) { // wait for connection is 'OnLine' to send data to bot
var convID = botConnection.conversationId;
botConnection.postActivity({
from: { id: convID } // because first time user ID == conversation ID
,type: 'event'
,name: 'registerUserData' // event name as we need
,value: my_token // data attached to event
}).subscribe(function (activityId) {
// This subscription is a MUST
// If I remove this handler the postActivity not reaches the bot
});
}
});
这里您订阅了 botConnection.connectionStatus$,当状态等于 2 时,您从 botConnection 对象中获取对话 ID。
然后,您可以在bot代码中添加这段中间件代码来获取数据:
bot.use({ receive: function(event, next) {
if (!!event && !!event.address && event.name == 'registerUserData') {
var message = new builder.Message().address(event.address).text('my_token:' + event.value);
bot.send(message, function (err) {}); // simulate proactive message to user
}
next();
} });
希望对您有所帮助。
我用Botframework Web Chat后台解决了,这里link供参考:
https://github.com/Microsoft/BotFramework-WebChat
在我使用直线生成对话 ID 后 API:
POST: https://directline.botframework.com/v3/directline/conversations
我通过反向通道将数据从网络应用程序发送到聊天机器人。
<div id="webchat"></div>
<script>
(async function () {
// We are using a customized store to add hooks to connect event
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
// When we receive DIRECT_LINE/CONNECT_FULFILLED action, we will send an event activity using WEB_CHAT/SEND_EVENT
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'webchat/join',
value: { conversation_id: conversationID }
}
});
}
return next(action);
});
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({ token }),
store
}, document.getElementById('webchat'));
document.querySelector('#webchat > *').focus();
})().catch(err => console.error(err));
//Note: conversationID and token is generated in the backend code of the web app.
</script>
这可能很简单,但我找不到与此相关的任何参考。 我使用直线 API 将聊天机器人集成到网络应用程序中;我正在使用此 API 生成对话 ID:
POST: https://directline.botframework.com/v3/directline/conversations
我正在尝试从上面 API(从网络应用程序代码)获取生成的对话 ID 到聊天机器人代码(NodeJS)。有没有办法或任何参考来做到这一点?
根据 this 问题评论的一种方法是在开始对话之前使用以下方式向机器人发送数据:
var params = BotChat.queryParams(location.search);
var my_token = params['my_token'];
var botConnection = new BotChat.DirectLine({
secret: 'DIRECTLINE_SECRET'
});
BotChat.App({
botConnection: botConnection
,user: { id: 'USER_ID', name: 'User' } // user.id auto updates after first user message
}, document.getElementById("bot"));
botConnection.connectionStatus$.subscribe(function (status) {
if (status == 2) { // wait for connection is 'OnLine' to send data to bot
var convID = botConnection.conversationId;
botConnection.postActivity({
from: { id: convID } // because first time user ID == conversation ID
,type: 'event'
,name: 'registerUserData' // event name as we need
,value: my_token // data attached to event
}).subscribe(function (activityId) {
// This subscription is a MUST
// If I remove this handler the postActivity not reaches the bot
});
}
});
这里您订阅了 botConnection.connectionStatus$,当状态等于 2 时,您从 botConnection 对象中获取对话 ID。
然后,您可以在bot代码中添加这段中间件代码来获取数据:
bot.use({ receive: function(event, next) {
if (!!event && !!event.address && event.name == 'registerUserData') {
var message = new builder.Message().address(event.address).text('my_token:' + event.value);
bot.send(message, function (err) {}); // simulate proactive message to user
}
next();
} });
希望对您有所帮助。
我用Botframework Web Chat后台解决了,这里link供参考: https://github.com/Microsoft/BotFramework-WebChat
在我使用直线生成对话 ID 后 API: POST: https://directline.botframework.com/v3/directline/conversations
我通过反向通道将数据从网络应用程序发送到聊天机器人。
<div id="webchat"></div>
<script>
(async function () {
// We are using a customized store to add hooks to connect event
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
// When we receive DIRECT_LINE/CONNECT_FULFILLED action, we will send an event activity using WEB_CHAT/SEND_EVENT
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'webchat/join',
value: { conversation_id: conversationID }
}
});
}
return next(action);
});
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({ token }),
store
}, document.getElementById('webchat'));
document.querySelector('#webchat > *').focus();
})().catch(err => console.error(err));
//Note: conversationID and token is generated in the backend code of the web app.
</script>