在向机器人触发消息之前,不会显示问候消息
Greeting message is not showing up until a message is triggered to the bot
我正在尝试发送问候消息并询问用户姓名。该机器人试图向用户询问一些问题,然后帮助他进行重定向 link。它在模拟器或 Microsoft 在线测试工具中运行完美,但一旦将机器人集成到 ASP.NET 应用程序中,问候消息就不会显示。
我搜索并找到了一些有用的 link,但我仍然无法解决问题。
我尝试了两种不同的方法,iframes
和 window.WebChat.renderWebChat
,但在这两种情况下都会出现问题,在我向机器人发送消息之前不会显示问候消息。
正如在其他帖子中讨论的那样,所有解决方案都以这个 topic 结束,因此聊天机器人集成必须在 javascript 或 Node 中实现;并且,需要将自定义事件(作为触发器)发送到聊天机器人。但是,虽然我的 bot 已部署在个人 windows 服务器上,但我不知道如何配置它并设置 directLine
参数。
已发布 here 可能的解决方案如下代码所示,但我无法配置和设置所需的参数来获取令牌。
消息端点:https://xxx.yyy.com/api/messages
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Web Chat: Send welcome event</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script crossorigin="anonymous" src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<style>
html,
body {
height: 100%;
}
body {
margin: 0;
}
#webchat {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="webchat"></div>
<script>
(async function() {
//I have no idea how this line of code must be set to achieve the token
//------------------------------------------------------
const res = await fetch('https://xxx.yyy.com/directline/token', { method: 'POST' });
const { token } = await res.json();
//------------------------------------------------------
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'webchat/join',
value: { language: window.navigator.language }
}
});
}
return next(action);
});
window.WebChat.renderWebChat(
{
directLine: window.WebChat.createDirectLine({ token }),
store
},
document.getElementById('webchat')
);
document.querySelector('#webchat > *').focus();
})().catch(err => console.error(err));
</script>
</body>
</html>
最后,我要强调的是,Bot在在线测试工具和模拟器上运行良好,但在asp.net应用程序中集成时出现错误。
截图:
左边的照片来自网站应用程序,右边的照片来自测试环境:
编辑 (1): 更新了标题,添加了屏幕截图。
下面的代码修复了令牌生成和发送事件以触发 Web 应用程序内的聊天机器人的问题。
$.ajax({
url: 'https://directline.botframework.com/v3/directline/tokens/generate',
method: "POST",
headers: {
"Authorization": "Bearer SECRET GOES HERE"
},
}).then(function (response) {
const { token } = JSON.parse(JSON.stringify(response));
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'webchat/join',
value: { language: window.navigator.language }
}
});
}
return next(action);
});
window.WebChat.renderWebChat(
{
directLine: window.WebChat.createDirectLine({ token }),
store
},
document.getElementById('chatBotFrame')
);
document.querySelector('#chatBotFrame > *').focus();
}).catch(function (err) {
console.error(err);
});
我正在尝试发送问候消息并询问用户姓名。该机器人试图向用户询问一些问题,然后帮助他进行重定向 link。它在模拟器或 Microsoft 在线测试工具中运行完美,但一旦将机器人集成到 ASP.NET 应用程序中,问候消息就不会显示。
我搜索并找到了一些有用的 link,但我仍然无法解决问题。
我尝试了两种不同的方法,iframes
和 window.WebChat.renderWebChat
,但在这两种情况下都会出现问题,在我向机器人发送消息之前不会显示问候消息。
正如在其他帖子中讨论的那样,所有解决方案都以这个 topic 结束,因此聊天机器人集成必须在 javascript 或 Node 中实现;并且,需要将自定义事件(作为触发器)发送到聊天机器人。但是,虽然我的 bot 已部署在个人 windows 服务器上,但我不知道如何配置它并设置 directLine
参数。
已发布 here 可能的解决方案如下代码所示,但我无法配置和设置所需的参数来获取令牌。
消息端点:https://xxx.yyy.com/api/messages
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Web Chat: Send welcome event</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script crossorigin="anonymous" src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<style>
html,
body {
height: 100%;
}
body {
margin: 0;
}
#webchat {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="webchat"></div>
<script>
(async function() {
//I have no idea how this line of code must be set to achieve the token
//------------------------------------------------------
const res = await fetch('https://xxx.yyy.com/directline/token', { method: 'POST' });
const { token } = await res.json();
//------------------------------------------------------
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'webchat/join',
value: { language: window.navigator.language }
}
});
}
return next(action);
});
window.WebChat.renderWebChat(
{
directLine: window.WebChat.createDirectLine({ token }),
store
},
document.getElementById('webchat')
);
document.querySelector('#webchat > *').focus();
})().catch(err => console.error(err));
</script>
</body>
</html>
最后,我要强调的是,Bot在在线测试工具和模拟器上运行良好,但在asp.net应用程序中集成时出现错误。
截图:
左边的照片来自网站应用程序,右边的照片来自测试环境:
编辑 (1): 更新了标题,添加了屏幕截图。
下面的代码修复了令牌生成和发送事件以触发 Web 应用程序内的聊天机器人的问题。
$.ajax({
url: 'https://directline.botframework.com/v3/directline/tokens/generate',
method: "POST",
headers: {
"Authorization": "Bearer SECRET GOES HERE"
},
}).then(function (response) {
const { token } = JSON.parse(JSON.stringify(response));
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'webchat/join',
value: { language: window.navigator.language }
}
});
}
return next(action);
});
window.WebChat.renderWebChat(
{
directLine: window.WebChat.createDirectLine({ token }),
store
},
document.getElementById('chatBotFrame')
);
document.querySelector('#chatBotFrame > *').focus();
}).catch(function (err) {
console.error(err);
});