javascript - 如何: MS Bot directline messaging using a websocket

标签 javascript botframework

我正在尝试了解如何使用 MicroSoft bot Directline 通信。我可以正常连接,使用 POST 和 GET 发送和接收消息。我的问题是在尝试使用 WebSockets 时。我开始对话,取回我的 conversationId 和 streamUrl,我使用 streamUrl 创建一个新的 WS,我的机器人定期发出 ping 信号,证明我已连接。使用 we.Send() 不会引发错误,但我没有收到回复。有谁知道下一步?提前致谢。

function StartConversation(params) {
    fetch('https://directline.botframework.com/v3/directline/conversations', {
            method: "POST", // *GET, POST, PUT, DELETE, etc.
            mode: "cors", // no-cors, cors, *same-origin
            cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
            credentials: "same-origin", // include, same-origin, *omit
            headers: {
                "Authorization": `Bearer ${BearerToken}`,
                "Content-Type": "application/json; charset=utf-8"
            },
            redirect: "follow", // manual, *follow, error
            referrer: "no-referrer", // no-referrer, *client 
        })
        .then(response => response.text())  
        .then(function (params) {
            localStorage.setItem(storageKey, params);
            ConnectWebSocet();
        })
        .catch(error => console.error(`Fetch Error =\n`, error));
}

function ConnectWebSocet() {
    var token = GetBotToken();
    ws = new WebSocket(token.streamUrl);
    ws.onopen = function (v, e) {
        console.log('open', v);
    };

    ws.onmessage = function (evt) {
        console.log(evt);
    };
}

最佳答案

doesn't throw an error but I'm not getting a response.

根据你的代码,我们可以发现你只是start a conversation , 但不是 send activities .在那一刻,可能没有可用的消息可以推送到通过 WebSocket 连接的客户端,因此您没有发现任何消息被接收并写入浏览器控制台选项卡。

如果您在开始对话后尝试发送事件,如下所示,您通过 WebSocket 连接的客户端应该能够接收消息。

开始对话后发送事件并连接 WebSocket:

//your code logic to start a conversation
.then(function (params) {
    localStorage.setItem(storageKey, params);

    //send an activity to bot
    SendActivity();

    ConnectWebSocet();
})

函数SendActivity()的定义:

function SendActivity() {
    var token = GetBotToken();

    var mes = {
        "type": "message",
        "from": {
            "id": "Fei Han"
        },
        "text": "hello"
    };

    fetch(`https://directline.botframework.com/v3/directline/conversations/${token.conversationId}/activities`, {
        method: "POST",
        headers: {
            "Authorization": `Bearer ${BearerToken}`,
            "Content-Type": "application/json"
        },
        body: JSON.stringify(mes),
    })
        .then(response => response.text())
        .then(function (myJson) {
            console.log(myJson);
        })
        .catch(error => console.error(`Fetch Error =\n`, error));
}

测试结果:

enter image description here

关于javascript - 如何: MS Bot directline messaging using a websocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51808963/

相关文章:

node.js - 如何跳过机器人框架 Waterfall BeginDialog Nodejs 中的步骤?

c# - BOT 不在网络聊天中运行

javascript - 按单元格内容以外的值对列进行排序

javascript - 到图像点击我给了一个函数,点击它就获取图像ID,使用这个我必须使用javascript更改图像

javascript - 如何使用 jquery toggle() 为与内容相邻的内容设置动画(滑动)

node.js - Bot Framework WebChat 失败 [405] 不允许使用方法

c# - 同一对话机器人框架中的 3 个不同 channel

javascript - 如何在 Windows 8 应用程序中显示数组中的数据

javascript - JQuery 选择选中复选框的标签

azure - 为什么来自 Azure 机器人框架的第一条消息总是很慢