首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Microsoft BotConnector发送新消息的POST正文

使用Microsoft BotConnector发送新消息的POST正文
EN

Stack Overflow用户
提问于 2016-06-22 11:01:11
回答 1查看 530关注 0票数 0

REST在发送消息时详细介绍了POST主体的JSON格式,但是不清楚字段中的内容。这是格式..。

代码语言:javascript
复制
{
  "id": "string",                         // ?
  "conversationId": "string",             // Conversation ID
  "created": "2016-06-22T10:45:48.618Z",  // Current time?
  "from": "string",                       // Username?
  "text": "string",                       // The message to be sent
  "channelData": "string",                // ?
  "images": [
    "string"                              // Image URL?
  ],
  "attachments": [
    {
      "url": "string",                    // Attachment URL
      "contentType": "string"             // ContentType
    }
  ],
  "eTag": "string"                        // ?
}

我已经把我不确定的地方标记出来了。现在,我发送请求并得到一个500服务器错误作为回报。该机器人使用本地bot仿真器在本地很好地工作。

这个JSON需要在Android应用程序中构建。

编辑:这里是我要发送的截取的JSON和响应

代码语言:javascript
复制
POST https://directline.botframework.com/api/conversations/D0I7X8284zv/messages HTTP/1.1
Host: directline.botframework.com
Connection: keep-alive
Content-Length: 239
Authorization: BotConnector Ve7jitnSIdE.dAA.RAAwAEkANwBYADgAMgA4ADQAegB2AA.Iy0ZhjLN0QE.e9o7v6n2Xz4.8C7zj2UlOP6202jMEpHqjXVfZxexO5JxzFE7VrRgaXg
Postman-Token: dd7b4c43-84ea-7c38-bd2c-681f6c031eb0
Cache-Control: no-cache
Origin: chrome-extension://aicmkgpgakddgnaphhhpliifpcfhicfo
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6,fr;q=0.4
Cookie: XXXXXXXXXXXXXXXXXXXXX

{
  "id": "SomeId",
  "conversationId": "D0I7X8284zv",
  "created": "2016-06-23T09:05:06.718Z",
  "from": "someusername",
  "text": "annual leave",
  "channelData": "Conv1",
  "images": [],
  "attachments": [],
  "eTag": "blah"
}
HTTP/1.1 500 Internal Server Error
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 43
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
Set-Cookie: UserId=XXXXXXXXXXXXXXXXXXXXXXX; path=/
Access-Control-Allow-Origin: *
X-Powered-By: ASP.NET
Strict-Transport-Security: max-age=31536000
Date: Thu, 23 Jun 2016 09:06:06 GMT

{
  "message": "failed to send message"
}
EN

回答 1

Stack Overflow用户

发布于 2016-06-22 13:41:59

在JSON中,文本是用户发送给bot的消息。我得到了500个错误,当我启用网络聊天直接行API。但它在5或6个小时后就自动解决了。

顺便说一下,根据微软的帮助,他们给了我这两个选项,Direct会在两种情况下返回错误:

  1. 您的消息有问题,或者在直通车中有内部错误。
  2. 您的bot正在返回一条错误消息。

正如您所提到的,在模拟器中,它工作得很好。这是因为你传递的信息。

代码语言:javascript
复制
Message {
id (string, optional): ID for this message ,
conversationId (string, optional): Conversation ID for this message ,
created (string, optional): UTC timestamp when this message was created ,
from (string, optional): Identity of the sender of this message ,
text (string, optional): Text in this message ,
channelData (string, optional): Opaque block of data passed to/from bot via the ChannelData field ,
images (Array[string], optional): Array of URLs for images included in this message ,
attachments (Array[Attachment], optional): Array of non-image attachments included in this message ,
eTag (string, optional)
}
Attachment {
url (string, optional): URL for this attachment ,
contentType (string, optional): Content type for this attachment 
} 

Conversation {
conversationId (string, optional): ID for this conversation ,
token (string, optional): Token scoped to this conversation ,
eTag (string, optional)
} 

在这里我分享C#代码,也许这会对你有帮助,

代码语言:javascript
复制
 private async Task<bool> PostMessage(string message)
        {

            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 BOT TOKEN]");
            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/";
                    BotDirectLineApproch.Models.Message msg = new BotDirectLineApproch.Models.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;
        }

我用相同的JSON创建了一个类,如下所示,

代码语言:javascript
复制
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; }
    }

在谈话中,

代码语言:javascript
复制
public class Conversation
    {
        public string conversationId { get; set; }
        public string token { get; set; }
        public string eTag { get; set; }
    }

我希望这个答案能帮助你解决你的问题。干杯!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37966077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档