1
votes

I've set up a simple bot - registered with Bot Connector - and I'm just trying to get the basic Direct Line API connection set up. In my separate application (C#), I've succeeded at initiating a conversation by using an HttpClient and retrieving the conversationId (by deserializing the response).

However, I then attempt to post a message to the thread, and I'm getting a "Internal Server Error", error code 500. The only message attached is "An error has occurred.".

using(var client =  new HttpClient())
{
client.BaseAddress = new Uri("https://directline.botframework.com/");
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Authorization", $"BotConnector {token}");
client.DefaultRequestheaders.Add("Type", "Message");
var post_content = new StringContent("Adding to the convo", Encoding.UTF8, "application/json");
HttpResponseMessage response = new client.PostAsync($"api/conversations/{convo_id}/messages", post_content).Result;
log(response.ReadAsStringAsync().Result);
}

log is a simple method to output to the console, while convo_id is the conversationId taken from the initial call to the site.

2
Have you posted the full error? This answer suggests there should be a number associated with it, maybe that could help find the anwer : stackoverflow.com/questions/18713666/… - Bassie
Edited to respond: it's a simple 500 error. That's the confusion: there's no details provided by the Bot Connector API to explain what's going awry. - Matthew Bohrer

2 Answers

0
votes

The line of code:

var post_content = new StringContent("Adding to the convo", Encoding.UTF8, "application/json");

is the source of the problems. Look at the format of the message expected: http://docs.botframework.com/sdkreference/restapi-directline/#!/Conversations/Conversations_PostMessage

It's expecting a JSON object with a bunch of fields. I was able to repo your problem and changed the content of the message and the error goes away.

Message message = new Message(conversationId: convId, text: "Happy days");
string output = JsonConvert.SerializeObject(message);
var post_content = new StringContent(output,Encoding.UTF8,"application/json");
0
votes

Using the Microsoft.Bot.Connector.DirectLine NuGet package, I'm having some success with the DirectLine API.

var uri = new Uri("https://directline.botframework.com");

DirectLineClientCredentials creds = new DirectLineClientCredentials(secret);

DirectLineClient client = new DirectLineClient(uri, creds);
Conversations convs = new Conversations(client);

string waterMark;

var conv = convs.NewConversation();
var set = convs.GetMessages(conv.ConversationId);
waterMark = set.Watermark;

Message message = new Message(conversationId: conv.ConversationId, text: "your text");
Console.WriteLine(message.Text);
convs.PostMessage(conv.ConversationId, message);

set = convs.GetMessages(conv.ConversationId, waterMark);
PrintResponse(set);
waterMark = set.Watermark;

with the PrintResponse defined as:

private static void PrintResponse(MessageSet set)
{
  var q = from x in set.Messages
          where x.FromProperty == "<YOUR BOTS APP ID HERE>"
          select x.Text;

  foreach (var str in q.ToList())
   {
      Console.WriteLine(">> " +str);
   }


}