尝试通过直线 API 在 Microsoft Bot Connector 中发送 post 消息时收到 "Internal Server Error"
Getting an "Internal Server Error" when trying to post messages in Microsoft Bot Connector by Direct Line API
我已经设置了一个简单的机器人 - 在 Bot Connector 中注册 - 我只是想建立基本的 Direct Line API 连接。在我单独的应用程序 (C#) 中,我已经通过使用 HttpClient 并检索 conversationId(通过反序列化响应)成功地发起对话。
但是,我随后尝试 post 向线程发送消息,但我收到 "Internal Server Error",错误代码 500。附加的唯一消息是 "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 是一种输出到控制台的简单方法,而 convo_id 是从对站点的初始调用中获取的 conversationId。
使用 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);
}
}
代码行:
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");
我已经设置了一个简单的机器人 - 在 Bot Connector 中注册 - 我只是想建立基本的 Direct Line API 连接。在我单独的应用程序 (C#) 中,我已经通过使用 HttpClient 并检索 conversationId(通过反序列化响应)成功地发起对话。
但是,我随后尝试 post 向线程发送消息,但我收到 "Internal Server Error",错误代码 500。附加的唯一消息是 "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 是一种输出到控制台的简单方法,而 convo_id 是从对站点的初始调用中获取的 conversationId。
使用 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);
}
}
代码行:
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");