2016-06-22 24 views
0

Die REST-API gibt das JSON-Format des POST-Nachrichtentexts beim Senden einer Nachricht an, es ist jedoch nicht klar, was in die Felder gehört. Hier ist das Format ...POST-Nachricht zum Senden einer neuen Nachricht mit dem Microsoft BotConnector

{ 
    "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"      // ? 
} 

Ich habe das Feld markiert, dass ich unsicher bin. Im Moment sende ich die Anfragen und bekomme einen 500 Server Fehler zurück. Der Bot funktioniert lokal gut mit dem lokalen Bot-Emulator.

Dieser JSON muss in einer Android-App erstellt werden.

EDIT: Hier ist der abgefangenen JSON und Antwort, die ich das Senden am

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

Verwenden Sie direkte API? – SilentCoder

+0

Ja, ich benutze die Direct Line REST API – Brendan

Antwort

0

Im JSON Text ist die Nachricht, dass Benutzer Bot senden. Ich habe einen Fehler von 500, wenn ich den Web-Chat mit direkter API-Leitung aktiviere. Aber es löste sich automatisch nach 5 oder 6 Stunden.

Durch die Art und Weise nach der Microsoft-Hilfe, gaben sie mir diese beiden Optionen, Direct Line werden Fehler in zwei Situationen zurück:

  1. Es gibt ein Problem mit Ihrer Nachricht oder einen internen Fehler in Direct Line
  2. Ihr bot kehrt eine Fehlermeldung

Wie Sie erwähnt haben, in Emulator funktioniert es gut. Das liegt an der Nachricht, die du übergibst.

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

hier mit I C# -Code zu teilen, kann sein, dass Ihnen helfen,

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

Und ich erstellt eine Klasse mit demselben JSON wie folgt

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

Und für das Gespräch ,

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

ich hoffe thi s Antwort gibt Ihnen eine Hilfe, um Ihr Problem zu lösen. Prost!

+0

Danke, es ist gut, Quelle zu sehen - Ist die 'Message' Klasse die gleiche wie' BotDirectLineApproch.Models.Message' gepostet oder nur aus einer JSON Antwort zusammengesetzt? Wenn ja, haben Sie eine Ahnung, wie die 'Message'-Felder gefüllt werden? Sind alle Felder neben "Text" voreingestellt? – Brendan

+0

Muss mein Bot auch in POST-Nachrichten veröffentlicht werden? Mein Bot ist im Moment in Überprüfung und ich kann Konversationen starten und alle Nachrichten herunterladen, aber 500 nur erhalten, wenn Sie eine neue Nachricht verfassen ... – Brendan

+0

Nachricht aus der JSON-Antwort. – SilentCoder