Sonali
Sonali

Reputation: 2273

Embedding customized form flow bot in custom client

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.

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.

I don't know how to manage options.

Upvotes: 0

Views: 105

Answers (1)

Fei Han
Fei Han

Reputation: 27793

For detecting attachments, I am currently using 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.

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.

Example response in my test:

enter image description here

Upvotes: 0

Related Questions