public async Task <Conversation> StartConversation(ChatConfig chatConfig) { Conversation conversation = null; HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"https://directline.botframework.com/v3/directline/conversations"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", chatConfig.Token); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { var body = await response.Content.ReadAsStringAsync(); conversation = JsonConvert.DeserializeObject <Conversation>(body); } return(conversation); }
public async Task <MessageEvent> SendMessage(string message, Conversation conversation, ChatConfig chatConfig) { MessageEvent botMessageEvent = null; HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage( HttpMethod.Post, $"https://directline.botframework.com/v3/directline/conversations/{conversation.conversationId}/activities"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", conversation.token); var botMessage = new { type = "message", text = message, from = new { id = chatConfig.UserId } }; request.Content = new StringContent(JsonConvert.SerializeObject(botMessage), Encoding.UTF8, "application/json"); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { var body = await response.Content.ReadAsStringAsync(); botMessageEvent = JsonConvert.DeserializeObject <MessageEvent>(body); } return(botMessageEvent); }