我已经设置了一个简单的机器人 - 在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。
答案 0 :(得分: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);
}
}
答案 1 :(得分: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");