2016-06-22 23 views
0

REST api在發送消息時詳細說明了POST正文的JSON格式,但是不清楚字段中發生了什麼。這裏是格式...用於使用Microsoft BotConnector發送新消息的POST正文

{ 
    "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服務器錯誤作爲回報。機器人使用本地機器人模擬器在本地工作良好。

這個JSON需要在Android應用程序中構建。

編輯:這裏是攔截的JSON和響應我送

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" 
} 
+0

您是否使用直接API? – SilentCoder

+0

是的,我正在使用Direct Line REST API – Brendan

回答

0

在JSON,文本是用戶發送給機器人的消息。當我啓用與直線API的網絡聊天時,我得到500錯誤。但它會在5或6個小時後自動解決。

通過根據微軟的幫助的方式,他們給了我這兩個選項, 專線直達會在兩種情況下會返回錯誤:

  1. 沒有您的消息或直接
  2. 行內部錯誤的問題至於你提到
  3. 你的機器人返回錯誤消息

,在仿真器正常工作。所以這是因爲你傳遞的信息。

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#代碼,可能是會幫助你,

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類如下,

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

而對於對話,

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

I hope thi答案會給你一個解決你的問題的幫助。 乾杯!

+0

謝謝,很高興看到源代碼 - 您發佈的Message類是否與BotDirectLineApproch.Models.Message相同或僅由JSON響應組成?如果是這樣,你有什麼想法'消息'字段如何填充?除了'text'之外,所有的字段都是默認的嗎? – Brendan

+0

我的機器人還需要發佈到POST消息嗎?我的機器人目前正在審查中,我可以開始對話並下載所有消息,但僅在發佈新消息時纔會獲得500個消息... – Brendan

+0

由JSON響應組成的消息。 – SilentCoder