Unable to succesfully pass Javascript variable in createDirectLine (botframework webchat) resulting in 403 error
每个网络聊天会话,我都会从与 https://webchat.botframework.com/api/tokens 对话的 restify 服务中检索一个令牌。我使用直接密码来获取令牌。令牌被正确检索。当我手动使用令牌(通过在 html 中输入)时,网络聊天会呈现。当我在函数中传递 Javascript 变量时,我无法创建直线。一些指导表示赞赏。一种解决方法是在 html 中使用秘密,但我宁愿不这样做
我尝试了多种语法来传递变量:window.WebChat.createDirectLine({ webChatToken }) or (webChatToken) or ({ webChatToken }) or (token: webChatToken) and ({ token: webChatToken })
唯一有效的方法是在 html 中手动输入令牌。
我使用我的 index.js(用于示例机器人)中的 restify 服务器来侦听和处理令牌请求:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// Create HTTP server and Cors
let server = restify.createServer(); server.pre(cors.preflight); server.use(cors.actual); server.listen(process.env.port || process.env.PORT || 3978, function() { }); // Listen for bot requests. // Listen for token requests. |
以及 html 和 Javascript 代码中的网络聊天客户端。在此示例中,它与 restify 服务器的本地实例通信。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//get token
const webChatToken = getToken(); window.WebChat.renderWebChat({ async function getToken() { |
我希望有一个可以工作的网络聊天客户端。在 chrome dev 扩展中,我看到 “POST https://directline.botframework.com/v3/directline/conversations 403 (Forbidden)。
直达api对应的响应:
{
“错误”: {
“code”: “BadArgument”,
“message”: “无效的令牌或秘密”
}
}
直线选项具有 token 属性,而不是 webChatToken 属性。您需要在选项对象中将 token 值设置为 webChatToken。查看下面的代码片段和 Direct Line Documentetion,了解有关初始化 Web Chat 和 Direct Line 客户端的更多详细信息。
1
2 3 4 5 6 7 8 9 10 |
(async function () {
const res = await fetch(‘http://localhost:3978/api/token’, { method: ‘GET’ }); const webChatToken = await res.json(); window.WebChat.renderWebChat({ document.querySelector(‘#webchat > *’).focus(); |
希望这会有所帮助!
- 谢谢你。不确定是令牌属性的设置还是您的代码的美丽起到了作用;-) 它奏效了!
来源:https://www.codenong.com/57079503/