Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to connect to Microsoft bot framework from android cilent

I created a simple android app which sends a message though JSON format using restfull jersey WS

which URL should i enter in my app that connects the bot?

and how does that bot receive message and send back the response?

As of now I am using bot emulator by Microsoft

Thanks in advance.

like image 717
Madhu Babu Avatar asked Jun 13 '16 07:06

Madhu Babu


People also ask

How do I add chatbot to my Android app?

Add Chatbot To Android App To create a chatbot for an Android app, the developer needs to create an agent either in Dialogflow or Kommunicate. Just fill in the required fields such as language and time. After this, all you have to do is train the chatbot.


1 Answers

You can connect your android client with DirectLine Rest API, before that enble web chat in your bot dashboard. Please refer documentation about direct line approach for Bot framework.

What you have to do is use https://directline.botframework.com/api/conversations as your endpoint and call those API as shown in the documentation.

Example :- I just tried with ASP.MVC application. I created a text box and button for submit message to bot.

1.First enable direct link in your bot application. Then remember that secret.

2.Following code sample shows you how to connect your chat app or your company app with bot you built using bot frame work.

3.First you need to authorize your access to direct link API.

client = new HttpClient();
client.BaseAddress = new Uri("https://directline.botframework.com/api/conversations/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "[Your Secret Key Here]");

response = await client.GetAsync("/api/tokens/");

if (response.IsSuccessStatusCode)

4.If you are success with previous response you can start a new Conversation Model -

public class Conversation { 
public string conversationId { get; set; } 
public string token { get; set; } 
public string eTag { get; set; } 
}

Code inside controller -

var conversation = new Conversation();
 response = await client.PostAsJsonAsync("/api/conversations/",conversation);
 if (response.IsSuccessStatusCode)

If you success with this response you will get conversationId and a token to start messaging.

5.Then pass your message to bot via following code,

Conversation ConversationInfo = response.Content.ReadAsAsync(typeof(Conversation)).Result as Conversation; string conversationUrl = ConversationInfo.conversationId+"/messages/"; Message msg = new Message() { text = message }; response = await client.PostAsJsonAsync(conversationUrl,msg); if (response.IsSuccessStatusCode)

If you get a success response, that means you have already sent your message to the bot. Now you need to get the reply message from BOT

6.To get the message from bot,

response = await client.GetAsync(conversationUrl); if (response.IsSuccessStatusCode){ MessageSet BotMessage = response.Content.ReadAsAsync(typeof(MessageSet)).Result as MessageSet; ViewBag.Messages = BotMessage; IsReplyReceived = true; }

Here you get a Message set, That means the message you sent and the reply from the Bot. You can now display it in your chat window.

Message Model -

public class MessageSet
{
    public Message[] messages { get; set; }
    public string watermark { get; set; }
    public string eTag { get; set; }
}

public class Message
{
    public string id { get; set; }
    public string conversationId { get; set; }
    public DateTime created { get; set; }
    public string from { get; set; }
    public string text { get; set; }
    public string channelData { get; set; }
    public string[] images { get; set; }
    public Attachment[] attachments { get; set; }
    public string eTag { get; set; }
}

public class Attachment
{
    public string url { get; set; }
    public string contentType { get; set; }
}

Using those API calls you can easily connect any of your custom chat applications with bot framework. Below is the full code inside one method for you to get idea about how you can archive your goal.

 private async Task<bool> PostMessage(string message)
    {
        bool IsReplyReceived = false;

        client = new HttpClient();
        client.BaseAddress = new Uri("https://directline.botframework.com/api/conversations/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "[Your Secret Code Here]");
        response = await client.GetAsync("/api/tokens/");
        if (response.IsSuccessStatusCode)
        {
            var conversation = new Conversation();
            response = await client.PostAsJsonAsync("/api/conversations/", conversation);
            if (response.IsSuccessStatusCode)
            {
                Conversation ConversationInfo = response.Content.ReadAsAsync(typeof(Conversation)).Result as Conversation;
                string conversationUrl = ConversationInfo.conversationId+"/messages/";
                Message msg = new Message() { text = message };
                response = await client.PostAsJsonAsync(conversationUrl,msg);
                if (response.IsSuccessStatusCode)
                {
                    response = await client.GetAsync(conversationUrl);
                    if (response.IsSuccessStatusCode)
                    {
                        MessageSet BotMessage = response.Content.ReadAsAsync(typeof(MessageSet)).Result as MessageSet;
                        ViewBag.Messages = BotMessage;
                        IsReplyReceived = true;
                    }
                }
            }

        }
        return IsReplyReceived;
    }

Thanks Cheers with your bot.

like image 100
SilentCoder Avatar answered Dec 30 '22 23:12

SilentCoder