如何从android cilent连接到Microsoft bot框架

问题描述:

我创建了一个简单的android应用,该应用使用restfull jersey WS通过JSON格式发送消息

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

我应该在连接机器人的应用程序中输入哪个URL?

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

该机器人如何接收消息并发回响应?

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

截至目前,我正在使用Microsoft的bot模拟器

As of now I am using bot emulator by Microsoft

先谢谢了.

您可以将Android客户端与DirectLine Rest API连接起来,然后才能在bot仪表板中进行网络聊天. 请参阅有关Bot框架的直接方法的文档.

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.

您要做的是使用 https://directline.botframework.com/api/conversations 作为端点并按文档中所示调用这些API.

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.

示例:-我刚刚尝试使用ASP.MVC应用程序.我创建了一个文本框和一个按钮,用于向机器人提交消息.

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

1.首先在您的机器人应用程序中启用直接链接.然后记住那个秘密.

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

2.以下代码示例向您展示如何将聊天应用程序或公司应用程序与使用漫游器框架工作构建的漫游器连接.

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.首先,您需要授权您对直接链接API的访问.

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.如果成功获得了先前的回复,则可以启动新的会话模型-

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; } 
}

控制器内部的代码-

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

如果成功获得此响应,您将获得对话ID和令牌以开始消息传递.

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

5.然后通过以下代码将您的消息传递给bot

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)

如果获得成功响应,则意味着您已经将消息发送给了漫游器.现在,您需要从BOT获取回复消息

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.要从漫游器获取消息,

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; }

在这里,您将获得一个消息集,这意味着您发送的消息以及Bot的回复.现在,您可以在聊天窗口中显示它.

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.

消息模型-

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; }
}

使用这些API调用,您可以轻松地将任何自定义聊天应用程序与bot框架连接起来.以下是一种方法中的完整代码,可让您了解如何归档目标.

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.