Bot的Microsoft DirectLine API不起作用 [英] Microsoft DirectLine API to bot does not work

查看:55
本文介绍了Bot的Microsoft DirectLine API不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,为了解释我的问题,我必须向您提供具体情况.

So to explain my problem, I have to give you the context.

我有一个Bot,该Bot的Microsoft Bot框架部署在Slack上.现在,可以在机器人与之通信的后端发生这些事件".当事件发生时,我想将其通知给我的机器人,然后让它向所有会话发送一条消息,通知发生了什么事情.所以基本上:

I got a Bot built with microsoft bot framework deployed on slack. Now it can happen these "events" on my backend that the bot communicates with. When a event occurs, I want to notify my bot of it and then let it send a message to all of it's conversations that something has happend. So basicly:

Backend>Microserivce>Bot>users

要做到这一点,我必须将所有对话存储在我的后端中,我在那里的数据库中进行存储.当事件发生时,后端会将带有所有对话(基本上是其ID)的活动发布到bot上,并向其显示事件.

To do this I have to store all conversations in my backend, which I do in a database there. When a event happends, the backend will post an activity to the bot with all the conversations(basicly their id's) and the event it should show them.

因此,从本质上讲,我的后端需要向我的机器人发布一条消息.

So in essence my backend need to post a message to my bot.

为此,我发现了Microsoft Directline api,它在这里充当中间人,这是与bot进行通信的一种更为抽象的方式.问题是我不知道该怎么做.我遵循了Microsoft自己的教程,但它似乎对我不起作用:

For doing this I found the microsoft directline api which acts as a middleman here, a more abstract way to talk with the bot. The problem is that I don't know how to do it. I followed microsofts own tutorial but it doesn't seem to work for me:

这是我的后端用来通知漫游器的终结点.内容"以json格式的字符串包含对话和事件.

This is the endpoint that my backend uses to notify the bot. "content" contains conversations and events as a json formated string.

    [HttpPost]
    [Route("conversationsEvents")]
    public HttpResponseMessage PostConversationsEvents([FromBody]string content)
    {
        NotifyBot.Notify(content);
        return Request.CreateResponse(HttpStatusCode.NoContent );
    }

NotifyBot.Notify(content)看起来像这样:

NotifyBot.Notify(content) looks like this:

        private static async Task StartBotConversation( string contents)
    {
        string directLineSecret = "secret";
        string fromUser = "microserviceNotifyEndpoint";
        Activity activity = new Activity
        {
            From = new ChannelAccount(fromUser),
            Text = contents,
            Type = ActivityTypes.Event
        };
        DirectLineClient client = new DirectLineClient(directLineSecret);
        var conversation = await client.Conversations.StartConversationAsync();
        await client.Conversations.PostActivityAsync(conversation.ConversationId, activity);
    }

基本上,执行被卡在 var session = await client.Conversations.StartConversationAsync(); 上,它只是永远等待.我尝试将其更改为 var session = await client.Conversations.StartConversationAsync().ConfigureAwait(continueOnCapturedContext:false); 执行继续,但活动似乎未发布.

Basicly the execution get's stuck at var conversation = await client.Conversations.StartConversationAsync(); , it just waits forever. I tried changing it to var conversation = await client.Conversations.StartConversationAsync().ConfigureAwait(continueOnCapturedContext: false);´the execution goes on but the activity doesn't seem to get posted.

推荐答案

您不需要使用DirectLine,它是用于创建替代的bot UI的.

You don't need to use DirectLine, it is designed for creating alternative bot UIs.

要实现您想要的功能,您可以尝试以下操作:

To implementing what your want, you may try the following:

首先,您需要存储要向其发送消息的用户地址.我可以通过将用户的最后一条消息的ResumptionCookie存储在后端数据库中来完成操作.

First, you need to store users addresses to whom you want to send the messages. It my be done by storing the ResumptionCookie of a user last message in your backend database.

var state = new ResumptionCookie(message).GZipSerialize();

调用 PostConversationsEvents 时,您可以在最近一点与每个用户恢复对话.

When your PostConversationsEvents is called, you may resume the conversation at the latest point with each users.

        var resumptionCookie = ResumptionCookie.GZipDeserialize(state);
        var message = resumptionCookie.GetMessage();
        message.Text = content;
        await Conversation.ResumeAsync(resumptionCookie, message);

这不是唯一的解决方案.正如我所说,在这种情况下,您只是在最近一点恢复了与用户的对话.另一种解决方案是保存用户地址(用户使用相同的ResumptionCookie类),但是在需要以下情况时启动对话:

It is not the only solution. As I said, in this case you just resumed the conversation with the user at the latest point. Another solution is to save the user address (user the same ResumptionCookie class) but start the conversation when you need to:

        var resumptionCookie = ResumptionCookie.GZipDeserialize(state);
        var message = cookie.GetMessage();

        ConnectorClient client = new ConnectorClient(new Uri(message.ServiceUrl));

        var conversation = await 
        client.Conversations.CreateDirectConversationAsync(message.Recipient, message.From);
        message.Conversation.Id = conversation.Id;
        var newMessage = message.CreateReply();
        newMessage.Text = content;
        await client.Conversations.SendToConversationAsync(newMessage);

有关BotFramework文档的更多详细信息.

See more details on BotFramework documentation.

这篇关于Bot的Microsoft DirectLine API不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆