1

我已经设置了一个简单的机器人——在 Bot Connector 中注册——我只是想建立基本的 Direct Line API 连接。在我的单独应用程序 (C#) 中,我成功地通过使用 HttpClient 并检索了 conversationId(通过反序列化响应)启动了对话。

但是,然后我尝试向线程发布消息,我收到“内部服务器错误”,错误代码 500。附加的唯一消息是“发生错误。”。

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 是输出到控制台的简单方法,而 convo_id 是从初始调用站点获取的 conversationId。

4

2 回答 2

0

代码行:

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

是问题的根源。查看预期消息的格式: http ://docs.botframework.com/sdkreference/restapi-directline/#!/Conversations/Conversations_PostMessage

它期待一个带有一堆字段的 JSON 对象。我能够解决您的问题并更改了消息的内容,并且错误消失了。

Message message = new Message(conversationId: convId, text: "Happy days");
string output = JsonConvert.SerializeObject(message);
var post_content = new StringContent(output,Encoding.UTF8,"application/json");
于 2016-04-26T20:45:14.243 回答
0

使用 Microsoft.Bot.Connector.DirectLine NuGet 包,我在 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;

PrintResponse 定义为:

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


}
于 2016-04-26T19:52:54.207 回答