我尝试使用直接线路将UWP应用程序与Bot框架集成,但是bot框架网站向我显示了这个错误:" HTTP状态代码InternalServerError",所以我尝试调试,经过研究,我发现错误发生在"发送活动到机器人"部分,但我不知道如何解决它。 (如果我使用bot模拟器进行测试,它可以工作,这是否意味着机器人没有任何问题?)
namespace BotClient
{
public sealed partial class MainPage : Page
{
BotService myBot;
string ConversationId;
public MainPage()
{
this.InitializeComponent();
myBot = new BotService();
}
private async void btnAsk_Click(object sender, RoutedEventArgs e)
{
string msg = txtInput.Text;
ConversationId = await myBot.StartConversation("Bearer KNEkUE8KsPg.cwA.28g.gw6z3bjzRutZID4T2xDAp0XYnCb7mTysJV53rbivy0s");
await myBot.SendMessage(msg);
ConversationActitvities messages = await myBot.GetMessages();
for (int i = 1; i < messages.activities.Length; i++)
{
lblMessage.Text += messages.activities[i].text + Environment.NewLine;
}
}
}
}
namespace BotFrameworkTestClient
{
class Conversation
{
public string conversationId { get; set; }
public string token { get; set; }
public string eTag { get; set; }
public string expires_in { get; set; }
}
public class ConversationReference
{
public string id { get; set; }
}
public class ConversationActitvities
{
public Activity[] activities { get; set; }
public string watermark { get; set; }
public string eTag { get; set; }
}
public class UserId
{
public string id { get; set; }
public string name { get; set; }
}
public class ActivityReference
{
public string id { get; set; }
}
public class ActivityMessage
{
public string type { get; set; }
public UserId from { get; set; }
public string text { get; set; }
}
public class Activity : ActivityMessage
{
public string id { get; set; }
public DateTime timestamp { get; set; }
public ConversationReference conversation { get; set; }
public string channelId { get; set; }
public string replyToId { get; set; }
public DateTime created { get; set; }
public Channeldata channelData { get; set; }
public string[] images { get; set; }
public Attachment[] attachments { get; set; }
public string eTag { get; set; }
}
public class Channeldata
{
}
public class Attachment
{
public string url { get; set; }
public string contentType { get; set; }
}
class KeyRequest
{
public string Mainkey { get; set; }
}
public class BotService
{
private string APIKEY;
private string botToken;
private string activeConversation;
private string activeWatermark;
private string newActivityId;
private string lastResponse;
public BotService()
{
// Constructor
}
public async Task<string> StartConversation(string secret)
{
//APIKEY = secret;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://directline.botframework.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Authorize
//client.DefaultRequestHeaders.Add("Authorization", "Bearer " + APIKEY);
client.DefaultRequestHeaders.Add("Authorization", secret);
// Get a new token as dummy call
var keyreq = new KeyRequest() { Mainkey = "" };
var stringContent = new StringContent(keyreq.ToString());
HttpResponseMessage response = await client.PostAsync("v3/directline/conversations", stringContent);
if (response.IsSuccessStatusCode)
{
var re = response.Content.ReadAsStringAsync().Result;
var myConversation = JsonConvert.DeserializeObject<Conversation>(re);
activeConversation = myConversation.conversationId;
botToken = myConversation.token;
return myConversation.conversationId;
}
}
return "Error";
}
public async Task<bool> SendMessage(string message)
{
using (var client = new HttpClient())
{
string conversationId = activeConversation;
client.BaseAddress = new Uri("https://directline.botframework.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Authorize
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + botToken);
// Send a message
string messageId = Guid.NewGuid().ToString();
DateTime timeStamp = DateTime.Now;
var attachment = new Attachment();
var myMessage = new ActivityMessage()
{
type = "message",
from = new UserId() { id = "Joe" },
text = message
};
string postBody = JsonConvert.SerializeObject(myMessage);
String urlString = "v3/directline/conversations/" + conversationId + "/activities";
HttpContent httpContent = new StringContent(postBody, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(urlString, httpContent);
if (response.IsSuccessStatusCode)
{
var re = response.Content.ReadAsStringAsync().Result;
lastResponse = re;
var ar = JsonConvert.DeserializeObject<ActivityReference>(re);
newActivityId = ar.id;
return true;
}
else
{
lastResponse = response.Content.ReadAsStringAsync().Result;
}
return false;
}
}
public async Task<string> GetNewestActivity()
{
ConversationActitvities cm = await GetNewestActivities();
if (cm.activities.Length > 0)
{
return cm.activities[cm.activities.Length - 1].text;
}
else
{
return "";
}
}
public async Task<ConversationActitvities> GetNewestActivities()
{
await Task.Delay(TimeSpan.FromMilliseconds(200)).ConfigureAwait(true);
int inc = 0;
ConversationActitvities cm = await GetMessages();
while (++inc < 5)
{
Debug.WriteLine(cm.activities.Length + "conversations received");
for (int i = 0; i < cm.activities.Length; i++)
{
var activity = cm.activities[i];
Debug.WriteLine("activity received = " + activity.text);
lastResponse = activity.id + " / " + activity.replyToId + " / " + newActivityId;
// wait for reply message from my message
if (activity.replyToId != null && activity.replyToId.Equals(newActivityId))
{
Debug.WriteLine("activity is response to " + newActivityId);
return cm;
}
}
await Task.Delay(TimeSpan.FromMilliseconds(5)).ConfigureAwait(true);
cm = await GetMessages();
}
return cm;
}
public async Task<ConversationActitvities> GetMessages()
{
using (var client = new HttpClient())
{
string conversationId = activeConversation;
client.BaseAddress = new Uri("https://directline.botframework.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Authorize
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + botToken);
ConversationActitvities cm = new ConversationActitvities();
string messageURL = "v3/directline/conversations/" + conversationId + "/activities";
if (activeWatermark != null)
messageURL += "?watermark=" + activeWatermark;
HttpResponseMessage response = await client.GetAsync(messageURL);
if (response.IsSuccessStatusCode)
{
var re = response.Content.ReadAsStringAsync().Result;
lastResponse = re.ToString();
cm = JsonConvert.DeserializeObject<ConversationActitvities>(re);
activeWatermark = cm.watermark;
return cm;
}
return cm;
}
}
}
}