public async static Task <string> PostMessage(string message) { HttpClient client; HttpResponseMessage response; bool IsReplyReceived = false; string ReceivedString = null; client = new HttpClient(); client.BaseAddress = new Uri("https://directline.botframework.com/api/conversations/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); try { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "myI48W5qkkk.cwA.t8E.jseT_7Y9odqoQHOvehPnW-voEgR1NC7eW99NfaonN_Q"); } catch (Exception e) { return("验证环节失败"); } response = await client.GetAsync("/api/tokens/"); if (response.IsSuccessStatusCode) { var conversation = new Conversation(); try { response = await client.PostAsJsonAsync("/api/conversations/", conversation); } catch (Exception e) { return("取id环节失败"); } //response = await client.PostAsync("/api/conversations/", null); if (response.IsSuccessStatusCode) { Conversation ConversationInfo = response.Content.ReadAsAsync(typeof(Conversation)).Result as Conversation; string conversationUrl = ConversationInfo.conversationId + "/messages/"; Message msg = new Message() { text = message }; response = await client.PostAsJsonAsync(conversationUrl, msg); if (response.IsSuccessStatusCode) { response = await client.GetAsync(conversationUrl); if (response.IsSuccessStatusCode) { MessageSet BotMessage = response.Content.ReadAsAsync(typeof(MessageSet)).Result as MessageSet; ReceivedString = BotMessage.messages[1].text; IsReplyReceived = true; } } } } return(ReceivedString); }
public async static Task <string> PostMessage(string message) { HttpClient client; HttpResponseMessage response; bool IsReplyReceived = false; string ReceivedString = null; client = new HttpClient(); client.BaseAddress = new Uri("https://directline.botframework.com/api/conversations/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "uc0nyMvM0NI.cwA.wYs.B8F1M7cEBm9StsTDG8pmuOjhnxeCJd2LdvNlKVfBgro"); // for leon //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "aDyJxnUSx30.cwA.WOg.4DzXtwItzBC6jyUCxHXG8fLKcgdx2zZYf2BkkfW5Lpc"); //for roche client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", System.Configuration.ConfigurationManager.AppSettings["BotKey"]); // for doudou //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "iCoMHD3whk8.cwA.50s.vlUvoUn7IjjRuHkSeNZQ2dOIHHTOftz07bAJRF2WJy8"); response = await client.GetAsync("/api/tokens/"); if (response.IsSuccessStatusCode) { var conversation = new Conversation(); response = await client.PostAsJsonAsync("/api/conversations/", conversation); //response = await client.PostAsync("/api/conversations/", null); if (response.IsSuccessStatusCode) { Conversation ConversationInfo = response.Content.ReadAsAsync(typeof(Conversation)).Result as Conversation; string conversationUrl = ConversationInfo.conversationId + "/messages/"; Message msg = new Message() { text = message }; response = await client.PostAsJsonAsync(conversationUrl, msg); if (response.IsSuccessStatusCode) { response = await client.GetAsync(conversationUrl); if (response.IsSuccessStatusCode) { MessageSet BotMessage = response.Content.ReadAsAsync(typeof(MessageSet)).Result as MessageSet; ReceivedString = BotMessage.messages[1].text; IsReplyReceived = true; } } } } return(ReceivedString); }