[Route("{conversationId}/activities/{activityId}")] // Matches 'v3/conversations/{conversationId}/activities/{activityId}' public HttpResponseMessage ReplyToActivity(string conversationId, string activityId, [FromBody] Activity activity) { // When a bot wants to reply to a user, it calls SendActivityAsync() which then calls this method so // that the channel (a mock channel in this case) can reply to the client in whatever manor the channel // requires. // TODO: Add whatever code is necessary to send the bot's response back to the client or do nothing here // if you just want to flow messages to your bot and you don't care about getting the response back to // the client. // You could create a custom API on this MockChannel and call it here and pass it the Activity and then // use the conversationId and activityId to correlate the bot response with its corresponding utterance MockTurn turn = _turns[long.Parse(activityId)]; turn.TurnStart.Stop(); _messagePerSecond = _activityCount / (_startOfTest.ElapsedMilliseconds / 1000.0); if (turn.TurnStart.ElapsedMilliseconds > _slowestResponseTime) { _slowestResponseTime = turn.TurnStart.ElapsedMilliseconds; } if (turn.TurnStart.ElapsedMilliseconds < _fastestResponseTime) { _fastestResponseTime = turn.TurnStart.ElapsedMilliseconds; } _totalResponseTime += turn.TurnStart.ElapsedMilliseconds; lock (_conversationLock) { _turns.Remove(long.Parse(activityId)); } System.Diagnostics.Debug.WriteLine($"MockChannel ReplyToActivity - conversationId - {conversationId}, activityId - {activityId}, activity.Action - {activity.Action}, activity.GetActivityType - {activity.GetActivityType()}, activity.ReplyToId - {activity.ReplyToId}, activity.Text - {activity.Text}, response time: {turn.TurnStart.ElapsedMilliseconds}"); // This HttpResponseMessage flows back to SendActivityAsync(), not the client that Posted to original message // This response lets the bot's SendActivityAsync() know that the channel successfully brokered the // response back to the client. return(Request.CreateResponse(HttpStatusCode.OK, new ResourceResponse(id: Guid.NewGuid().ToString("n")))); }
[Route("{conversationId}/activities")] // Matches 'v3/conversations/{conversationId}/activities' async public Task <HttpResponseMessage> SendToConversation(string conversationId, [FromBody] Activity activity) { MockTurn turn; HttpResponseMessage response; var requestBody = new Dictionary <string, string>(); requestBody.Add("grant_type", "client_credentials"); requestBody.Add("client_id", _appID); requestBody.Add("client_secret", _appPassword); requestBody.Add("scope", $"{_appID}/.default"); // Get BearerToken if we haven't yet if (_tokenResponse == null) { using (var bearerContent = new FormUrlEncodedContent(requestBody)) { if (!_tokenClient.DefaultRequestHeaders.Contains("Host")) { _tokenClient.DefaultRequestHeaders.Add("Host", "login.microsoftonline.com"); } // Send the request response = await _tokenClient.PostAsync(new Uri("https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token"), bearerContent); _tokenResponse = JsonConvert.DeserializeObject <TokenResponse>(await response.Content.ReadAsStringAsync()); } } // Add incoming Activity to Turn database lock (_conversationLock) { if (_activityCount == 0) { // Start timer for test so we can calculate message per second on summary _startOfTest.Start(); } // Get new activity ID activity.Id = (++_activityCount).ToString(); turn = new MockTurn() { Activity = activity }; _turns.Add(long.Parse(activity.Id), turn); } // Send Activity to bot's endpoint using (StringContent activityContent = new StringContent(JsonConvert.SerializeObject(activity), Encoding.UTF8, "application/json")) { if (!_botClient.DefaultRequestHeaders.Contains("Authorization")) { _botClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_tokenResponse.access_token}"); } System.Diagnostics.Debug.WriteLine($"MockChannel SendActivity - activity.Conversation.Id - {activity.Conversation.Id}, activity.Id - {activity.Id}, activity.ServiceUrl - {activity.ServiceUrl} activity.GetActivityType - {activity.GetActivityType()}, activity.Text - {activity.Text}"); // Start the Turn timer turn.TurnStart.Start(); // Send the request response = await _botClient.PostAsync(new Uri(_botEndpoint), activityContent); } return(Request.CreateResponse(response.StatusCode, new ResourceResponse(id: activity.Id))); }