protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken) { await turnContext.SendActivityAsync(MessageFactory.Text("parent: before child"), cancellationToken); var activity = MessageFactory.Text("parent to child"); activity.ApplyConversationReference(turnContext.Activity.GetConversationReference(), true); activity.DeliveryMode = DeliveryModes.BufferedReplies; var response = await _client.PostActivityAsync <Activity[]>( null, "toBotId", new Uri("http://localhost:3979/api/messages"), new Uri("http://tempuri.org/whatever"), Guid.NewGuid().ToString(), activity, cancellationToken); if (response.Status == (int)HttpStatusCode.OK) { await turnContext.SendActivitiesAsync(response.Body, cancellationToken); } await turnContext.SendActivityAsync(MessageFactory.Text("parent: after child"), cancellationToken); }
public async Task PostActivityUsingInvokeResponse(string expectedResponseBodyJson) { var httpClient = CreateHttpClientWithMockHandler((request, cancellationToken) => { // Create mock response. var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = expectedResponseBodyJson == null ? null : new StringContent(expectedResponseBodyJson) }; return(Task.FromResult(response)); }); var sut = new BotFrameworkHttpClient(httpClient, new Mock <ICredentialProvider>().Object); var activity = new Activity { Conversation = new ConversationAccount() }; var result = await sut.PostActivityAsync(string.Empty, string.Empty, new Uri("https://skillbot.com/api/messages"), new Uri("https://parentbot.com/api/messages"), "NewConversationId", activity); // Assert Assert.IsType <InvokeResponse <object> >(result); Assert.Equal(200, result.Status); if (expectedResponseBodyJson == null) { Assert.Null(result.Body); } else { var typedContent = JsonConvert.DeserializeObject <TestContentBody>(JsonConvert.SerializeObject(result.Body)); Assert.Equal("someId", typedContent.Id); Assert.Equal("theProp", typedContent.SomeProp); } }
public async Task PostActivityUsingInvokeResponseOfTToSelf() { var activity = new Activity { Conversation = new ConversationAccount(id: Guid.NewGuid().ToString()) }; var httpClient = CreateHttpClientWithMockHandler((request, cancellationToken) => { var sentActivity = JsonConvert.DeserializeObject <Activity>(request.Content.ReadAsStringAsync().Result); // Assert the activity we are sending is what we passed in. Assert.Equal(activity.Conversation.Id, sentActivity.Conversation.Id); // Create mock response. var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JObject.FromObject(new TestContentBody("someId", "theProp")).ToString()) }; return(Task.FromResult(response)); }); var client = new BotFrameworkHttpClient(httpClient, new Mock <ICredentialProvider>().Object); var result = await client.PostActivityAsync <TestContentBody>(string.Empty, new Uri("https://skillbot.com/api/messages"), activity); Assert.IsType <InvokeResponse <TestContentBody> >(result); Assert.Equal((int)HttpStatusCode.OK, result.Status); Assert.NotNull(result.Body); }
public async Task PostAsync() { var inboundActivity = await HttpHelper.ReadRequestAsync <Activity>(Request); var nextConversationId = await _factory.CreateSkillConversationIdAsync(inboundActivity.GetConversationReference(), CancellationToken.None); await _client.PostActivityAsync(null, null, _toUri, _serviceUrl, nextConversationId, inboundActivity); // ALTERNATIVE API IDEA... //var inboundConversationReference = inboundActivity.GetConversationReference(); //var outboundActivity = MessageFactory.CreateActivity(inboundActivity); //outboundActivity.ApplyConversationReference(inboundConversationReference, _serviceUrl, nextConversationId); //await _client.PostActivityAsync(_toUri, outboundActivity, null, null); }
public async void DoesNotOverwriteNonNullRecipientValues() { const string skillRecipientId = "skillBot"; Func <HttpRequestMessage, Task <HttpResponseMessage> > verifyRequestAndCreateResponse = (HttpRequestMessage request) => { var content = request.Content.ReadAsStringAsync().Result; var a = JsonConvert.DeserializeObject <Activity>(content); Assert.NotNull(a.Recipient); Assert.Equal(skillRecipientId, a.Recipient?.Id); var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StringContent(new JObject { }.ToString()); return(Task.FromResult(response)); }; var mockHttpMessageHandler = new Mock <HttpMessageHandler>(); mockHttpMessageHandler.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .Returns((HttpRequestMessage request, CancellationToken cancellationToken) => verifyRequestAndCreateResponse(request)) .Verifiable(); var httpClient = new HttpClient(mockHttpMessageHandler.Object); var mockCredentialProvider = new Mock <ICredentialProvider>(); var client = new BotFrameworkHttpClient(httpClient, mockCredentialProvider.Object); var activity = new Activity { Conversation = new ConversationAccount(), Recipient = new ChannelAccount("skillBot") }; await client.PostActivityAsync(string.Empty, string.Empty, new Uri("https://skillbot.com/api/messages"), new Uri("https://parentbot.com/api/messages"), "NewConversationId", activity); // Assert Assert.Equal("skillBot", activity?.Recipient?.Id); }
protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken) { // for signin, just use an oauth prompt to get the exchangeable token // also ensure that the channelId is not emulator if (turnContext.Activity.ChannelId != "emulator") { if (_magicCodeRegex.IsMatch(turnContext.Activity.Text) || turnContext.Activity.Text == "login") { // start an oauth prompt await _conversationState.LoadAsync(turnContext, true, cancellationToken); await _userState.LoadAsync(turnContext, true, cancellationToken); await _dialog.RunAsync(turnContext, _conversationState.CreateProperty <DialogState>(nameof(DialogState)), cancellationToken); } else if (turnContext.Activity.Text == "logout") { var adapter = turnContext.Adapter as IExtendedUserTokenProvider; await adapter.SignOutUserAsync(turnContext, _connectionName, turnContext.Activity.From.Id, cancellationToken); await turnContext.SendActivityAsync(MessageFactory.Text("logout from parent bot successful"), cancellationToken); } else if (turnContext.Activity.Text == "skill login" || turnContext.Activity.Text == "skill logout") { // incoming activity needs to be cloned for buffered replies var cloneActivity = MessageFactory.Text(turnContext.Activity.Text); cloneActivity.ApplyConversationReference(turnContext.Activity.GetConversationReference(), true); cloneActivity.DeliveryMode = DeliveryModes.ExpectReplies; var response1 = await _client.PostActivityAsync <ExpectedReplies>( _fromBotId, _toBotId, new Uri("http://localhost:2303/api/messages"), new Uri("http://tempuri.org/whatever"), turnContext.Activity.Conversation.Id, cloneActivity, cancellationToken); if (response1.Status == (int)HttpStatusCode.OK && response1.Body?.Activities != null) { var activities = response1.Body.Activities.ToArray(); if (!(await InterceptOAuthCards(activities, turnContext, cancellationToken))) { await turnContext.SendActivitiesAsync(activities, cancellationToken); } } } return; } await turnContext.SendActivityAsync(MessageFactory.Text("parent: before child"), cancellationToken); var activity = MessageFactory.Text("parent to child"); activity.ApplyConversationReference(turnContext.Activity.GetConversationReference(), true); activity.DeliveryMode = DeliveryModes.ExpectReplies; var response = await _client.PostActivityAsync <ExpectedReplies>( _fromBotId, _toBotId, new Uri("http://localhost:2303/api/messages"), new Uri("http://tempuri.org/whatever"), Guid.NewGuid().ToString(), activity, cancellationToken); if (response.Status == (int)HttpStatusCode.OK) { await turnContext.SendActivitiesAsync(response.Body?.Activities?.ToArray(), cancellationToken); } await turnContext.SendActivityAsync(MessageFactory.Text("parent: after child"), cancellationToken); }
public async Task PostActivityUsingInvokeResponseOfT(string testRecipientId) { // The test activity being sent. var testActivity = new Activity { Conversation = new ConversationAccount { Id = "TheActivityConversationId", Name = "TheActivityConversationName", ConversationType = "TheActivityConversationType", AadObjectId = "TheActivityAadObjectId", IsGroup = true, Properties = new JObject(), Role = "TheActivityRole", TenantId = "TheActivityTenantId", }, ServiceUrl = "https://theactivityServiceUrl", Recipient = testRecipientId == null ? null : new ChannelAccount(testRecipientId), RelatesTo = null, }; var httpClient = CreateHttpClientWithMockHandler((request, cancellationToken) => { // Assert the request properties Assert.Equal(new Uri("https://skillbot.com/api/messages"), request.RequestUri); // Assert expected values are in the activity being sent. var activitySent = JsonConvert.DeserializeObject <Activity>(request.Content.ReadAsStringAsync().Result); Assert.Equal("https://parentbot.com/api/messages", activitySent.ServiceUrl); Assert.Equal("NewConversationId", activitySent.Conversation.Id); if (testRecipientId == null) { // Ensure a default recipient is set if we don't pass one. Assert.NotNull(activitySent.Recipient); } else { // Ensure the recipient we want is set if it is passed. Assert.Equal("42", activitySent.Recipient.Id); } Assert.NotNull(activitySent.RelatesTo); Assert.Equal(testActivity.Conversation.Id, activitySent.RelatesTo.Conversation.Id); Assert.Equal("TheActivityConversationName", activitySent.RelatesTo.Conversation.Name); Assert.Equal("TheActivityConversationType", activitySent.RelatesTo.Conversation.ConversationType); Assert.Equal("TheActivityAadObjectId", activitySent.RelatesTo.Conversation.AadObjectId); Assert.Equal(true, activitySent.RelatesTo.Conversation.IsGroup); Assert.NotNull(activitySent.RelatesTo.Conversation.Properties); Assert.Equal("TheActivityRole", activitySent.RelatesTo.Conversation.Role); Assert.Equal("TheActivityTenantId", activitySent.RelatesTo.Conversation.TenantId); Assert.Equal("https://theactivityServiceUrl", activitySent.RelatesTo.ServiceUrl); // Create mock response. var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JObject.FromObject(new TestContentBody("someId", "theProp")).ToString()) }; return(Task.FromResult(response)); }); var client = new BotFrameworkHttpClient(httpClient, new Mock <ICredentialProvider>().Object); var result = await client.PostActivityAsync <TestContentBody>(string.Empty, string.Empty, new Uri("https://skillbot.com/api/messages"), new Uri("https://parentbot.com/api/messages"), "NewConversationId", testActivity); // Assert Assert.Equal((int)HttpStatusCode.OK, result.Status); Assert.IsType <InvokeResponse <TestContentBody> >(result); Assert.NotNull(result.Body); var typedContent = JsonConvert.DeserializeObject <TestContentBody>(JsonConvert.SerializeObject(result.Body)); Assert.Equal("someId", typedContent.Id); Assert.Equal("theProp", typedContent.SomeProp); }
private async Task ProcessSocketAsync(WebSocket webSocket) { var botClient = new BotFrameworkHttpClient( _httpClientFactory.CreateClient(), new SimpleCredentialProvider()); var proxyBot = new DelegateStreamingBot(activity => { return(botClient.PostActivityAsync( _msaAppId, _botAppId, _botEndpoint, _serviceEndpoint, activity.Conversation.Id, activity)); }); var streamingRequestHandler = new StreamingRequestHandler(proxyBot, proxyBot, webSocket); _server = new WebSocketServer(webSocket, streamingRequestHandler); var closedTask = _server.StartAsync(); var buffer = new byte[1024 * 4]; var cts = new CancellationTokenSource(); Task <Tuple <Activity, TaskCompletionSource <ResourceResponse> > > bufferReceiveTask = null; try { while (!cts.IsCancellationRequested) { if (bufferReceiveTask == null) { bufferReceiveTask = Task.Run(() => _replyBuffer.ReceiveAsync(cts.Token)); } await Task.WhenAny(closedTask, bufferReceiveTask); // Send from bot to socket if (bufferReceiveTask.IsCompleted) { var activity = bufferReceiveTask.Result.Item1; var tcs = bufferReceiveTask.Result.Item2; bufferReceiveTask = null; var response = await streamingRequestHandler.SendActivityAsync(activity); tcs.SetResult(response); } if (closedTask.IsCompleted) { break; } } } finally { _server.Disconnect(); cts.Cancel(); if (bufferReceiveTask != null) { try { await bufferReceiveTask; } catch (TaskCanceledException) { } } if (closedTask != null) { try { await closedTask; } catch (TaskCanceledException) { } } } }