javascript - 在向机器人触发消息之前,不会显示问候消息
问题描述
我正在尝试发送问候消息并询问用户的姓名。该机器人试图向用户询问一些问题,然后通过重定向链接帮助他。它在模拟器或 Microsoft 在线测试工具中完美运行,但是一旦将机器人集成到 ASP.NET 应用程序中,问候消息就不会出现。
我已经搜索并找到了几个有用的链接,但我仍然无法解决这个问题。
我尝试了两种不同的方法,iframes
但window.WebChat.renderWebChat
在这两种情况下都会出现问题,直到我向机器人发送消息后才会显示问候消息。
正如其他帖子所讨论的,所有的解决方案都以这个主题结束,因此聊天机器人集成必须在 javascript 或 Node 中实现;并且,需要将自定义事件(作为触发器)发送到聊天机器人。但是,虽然我的机器人已部署在个人 Windows 服务器上,但我不知道如何配置它并设置directLine
参数。
正如此处发布的那样,可能的解决方案是以下代码,但我无法配置和设置获取令牌所需的参数。
消息传递端点: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);
});
推荐阅读
- python - 使用一个类,其方法由另一个目录中的@ray.remote 修饰
- google-app-engine - 将 Rasa 与 Google Cloud Platform 一起使用时,Dockerfile 中应包含哪些内容
- javascript - 从android react-native + expo的手机本地存储加载图像
- r - 从R中的特殊字符中删除引号
- java - 在 java lambdas 的帮助下,重构方法来清理许多 if else
- python - 绘图点分散得足够远并将它们连接起来
- .net - C:\Windows\assembly 和 C:\Windows\Microsoft.NET\assembly 目录有什么区别?
- python - 无法连接到在线activemq
- android - Webview 图像不会在真实设备上加载
- singlestore - Memsql 工作室显示黑屏