在Bot Framework网络聊天中,如何连接到我的机器人服务器?

时间:2018-10-09 08:07:23

标签: botframework

所有教程都连接到直线,使用我自己的机器人服务器该怎么办?如何使用javascript连接?

我已经设置了服务器端:

// Setup BotFramework
var connector = new Builder.ChatConnector({

    appId: "a8...",
    appPassword: "ko...",
    openIdMetadata: process.env.BotOpenIdMetadata

});

// Setup Bot
var bot = new Builder.UniversalBot(connector);
bot.set('storage', new Builder.MemoryBotStorage());
// Setup Server
var server = Restify.createServer();

server.listen(process.env.port || process.env.PORT || 4000, function () {
    console.log("Listening on port "+(process.env.port || process.env.PORT || 4000));
});
server.post('/api/messages', connector.listen());

在客户端,这是我目前使用直线电话获得的内容:

<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>

<script src="https://cdn.botframework.com/botframework-webchat/latest/CognitiveServices.js"></script>

<script>
    const params = BotChat.queryParams(location.search);

    const speechOptions = {
        speechRecognizer: new BotChat.Speech.BrowserSpeechRecognizer(),
        speechSynthesizer: new BotChat.Speech.BrowserSpeechSynthesizer()
    };


    BotChat.App({
        showUploadButton: false,
        directLine: { secret: '_bm...' },
        bot: { id: 'a8...' },
        locale: params['locale'],
        resize: 'detect',
        speechOptions: speechOptions,
        user: {
            id: 'WebChatDemoUser',
            name: 'You'
        },
    }, document.getElementById('ChatBot'));
    var header = document.getElementsByClassName("wc-header");
    header[0].innerHTML = "<span>Chat</span>"
</script>

这项工作有效,但是它直接连接到bot框架,我需要像这样连接到我的服务器:

http://localhost:4000/api/messages

或者在生产中是这样的:

http://myserver.com:4000/api/messages

基本上,与Bot Emulator的连接方式类似:

enter image description here

1 个答案:

答案 0 :(得分:0)

DirectLineJs 将默认调用 Direct Line Connector服务。然后,连接器服务会调用您的漫游器,然后该漫游器会通过响应(或主动消息)回调到连接器服务。 DirectLineJs 确实提供了您可以提供的域参数以覆盖默认值:

const dl = new DirectLine({
    secret: /* put your Direct Line secret here */,
    token: /* or put your Direct Line token here (supply secret OR token, not both) */,
    domain: /* optional: if you are not using the default Direct Line endpoint, e.g. if you are using a region-specific endpoint, put its full URL here */
    webSocket: /* optional: false if you want to use polling GET to receive messages. Defaults to true (use WebSocket). */,
    pollingInterval: /* optional: set polling interval in milliseconds. Default to 1000 */,
});

但是,托管您自己的直接线路连接器服务涉及很多事情。 SDK代码将使用活动的 serviceUrl 属性将消息发送回连接器服务。该机器人期望的终结点如此MockChannelController中的终结点:https://github.com/Microsoft/BotFramework-Samples/blob/master/blog-samples/CSharp/MockChannel/Controllers/MockChannelController.cs

更多详细信息,请参见以下负载测试博客文章:https://blog.botframework.com/2017/06/19/load-testing-a-bot/

还有一个受限节点示例:offline-directline