在自定义客户端中嵌入自定义表单流程机器人

最后发布: 2018-03-15 09:43:02


问题

I have created a customized form flow. 我创建了一个定制的表单流程。 I am going to integrate it into my custom client. 我将把它集成到我的自定义客户端中。 For this, I have created a form template bot in the Azure portal and published my updated code. 为此,我在Azure门户中创建了一个表单模板bot,并发布了更新的代码。

I don't want to use default chat window, I want to integrate into my custom chat window. 我不想使用默认的聊天窗口,我想集成到我的自定义聊天窗口中。 For this, I have set up a direct line channel. 为此,我建立了一条直线通道。

Here is my code:- 这是我的代码:

public string TalkToTheBot2(string paramMessage)
{
    DirectLineClient client = new DirectLineClient(DirectLineSecret);
    Conversation conversation = System.Web.HttpContext.Current.Session["conversation"] as Conversation;
    string watermark = System.Web.HttpContext.Current.Session["watermark"] as string;
    if (conversation == null)
    {
        conversation = client.Conversations.StartConversation();
    }
    Activity message = new Activity
    {
       Text = paramMessage,
       From = new ChannelAccount(),
       Type = ActivityTypes.Message
    };
    var result = client.Conversations.PostActivityAsync(conversation.ConversationId, message).Result;
    Chat objChat = ReadBotMessagesAsync(client, conversation.ConversationId, watermark);
    System.Web.HttpContext.Current.Session["conversation"] = conversation;
    System.Web.HttpContext.Current.Session["watermark"] = objChat.watermark;
    objChat.ChatMessage = paramMessage;
    return JsonConvert.SerializeObject(objChat);
}

private Chat ReadBotMessagesAsync(DirectLineClient client, string conversationId, string watermark)
{
    Chat objChat = new Chat();
    bool messageReceived = false;
    while (!messageReceived)
    {
        var activitySet = client.Conversations.GetActivitiesAsync(conversationId, watermark).Result;
        watermark = activitySet?.Watermark;
        var activities = from x in activitySet.Activities
                                 where x.From.Id == botId
                                 select x;
        foreach (Activity message in activities)
        {
          if (message.Text != null)
          {
              objChat.ChatResponse
                 += " "
                 + message.Text.Replace("\n\n", "<br />");
          }
          if (message.Attachments.Count > 0)
          {
              bjChat.ChatResponse
                  += " " + RenderImageHTML(message.Attachments[0].ContentUrl);
          }
        }
        messageReceived = true;
    }
     objChat.watermark = watermark;
     return objChat;
}

And from my view, I just post data coming from user to this action method and get the response from action method back to view. 从我的角度来看,我只是将来自用户的数据发布到此操作方法,并将操作方法​​的响应返回给视图。 As it, a form flow bot, so many questions will be having multiple options and some messages will be having attachments to it. 因为它是一个表单流机器人,所以很多问题将具有多个选项,并且某些消息将带有附件。 How to manage these options and attachments here. 如何在此处管理这些选项和附件。

For detecting attachments, I am currently using message.Attachments.Count . 为了检测附件,我目前正在使用message.Attachments.Count

I don't know how to manage options. 我不知道如何管理选项。

c# botframework formflow
回答

For detecting attachments, I am currently using message.Attachments.Count. 为了检测附件,我目前正在使用message.Attachments.Count。 I don't know how to manage options. 我不知道如何管理选项。

I do a test with SandwichOrder bot (using FormFlow), and I make request to receive messages using Direct Line API and check the response, I find that the options are contained in buttons filed and the ContentType is application/vnd.microsoft.card.hero . 我使用SandwichOrder机器人进行了测试(使用FormFlow),并请求使用Direct Line API接收消息并检查响应,我发现选项包含在buttons application/vnd.microsoft.card.heroContentTypeapplication/vnd.microsoft.card.hero

As you did, you can detect if Attachments are null. 如您所做的,您可以检测附件是否为空。 And to access and extract options, you can detect the ContentType and get options from content . 要访问和提取选项,您可以检测ContentType并从content获取选项。

Example response in my test: 我的测试中的示例响应:

在此处输入图片说明