Ejemplo n.º 1
0
        protected override async Task <InvokeResponse> ProcessMessageRequestAsync(HttpRequestMessage request, BotFrameworkAdapter botFrameworkAdapter, Func <ITurnContext, Task> botCallbackHandler, CancellationToken cancellationToken)
        {
            const string BotAppIdHttpHeaderName           = "MS-BotFramework-BotAppId";
            const string BotAppIdQueryStringParameterName = "BotAppId";

            var botAppId = default(string);

            if (request.Headers.TryGetValues(BotAppIdHttpHeaderName, out var botIdHeaders))
            {
                botAppId = botIdHeaders.FirstOrDefault();
            }
            else
            {
                botAppId = request.GetQueryNameValuePairs()
                           .Where(kvp => kvp.Key == BotAppIdQueryStringParameterName)
                           .Select(kvp => kvp.Value)
                           .FirstOrDefault();
            }

            if (string.IsNullOrEmpty(botAppId))
            {
                throw new InvalidOperationException($"Expected a Bot App ID in a header named \"{BotAppIdHttpHeaderName}\" or in a querystring parameter named \"{BotAppIdQueryStringParameterName}\".");
            }

            var conversationReference = await request.Content.ReadAsAsync <ConversationReference>(BotMessageHandlerBase.BotMessageMediaTypeFormatters, cancellationToken);

            await botFrameworkAdapter.ContinueConversationAsync(botAppId, conversationReference, botCallbackHandler, cancellationToken);

            return(null);
        }
Ejemplo n.º 2
0
        public static async Task DoWork(IConfiguration configuration, OutgoingMessageQueueData queueData, ILogger log = null)
        {
            //var api = new CosmosInterface(configuration);

            if (string.IsNullOrEmpty(queueData.PhoneNumber) ||
                string.IsNullOrEmpty(queueData.Message))
            {
                return;
            }

            MicrosoftAppCredentials.TrustServiceUrl(configuration.ServiceUrl());
            var creds = new MicrosoftAppCredentials(configuration.MicrosoftAppId(), configuration.MicrosoftAppPassword());
            var credentialProvider = new SimpleCredentialProvider(creds.MicrosoftAppId, creds.MicrosoftAppPassword);
            var adapter            = new BotFrameworkAdapter(credentialProvider);
            var botAccount         = new ChannelAccount()
            {
                Id = configuration.BotPhoneNumber()
            };
            var userAccount = new ChannelAccount()
            {
                Id = PhoneNumber.Standardize(queueData.PhoneNumber)
            };
            var convoAccount = new ConversationAccount(id: userAccount.Id);
            var convo        = new ConversationReference(null, userAccount, botAccount, convoAccount, configuration.ChannelId(), configuration.ServiceUrl());

            await adapter.ContinueConversationAsync(creds.MicrosoftAppId, convo, async (context, token) =>
            {
                await context.SendActivityAsync(queueData.Message);
            }, new CancellationToken());
        }
Ejemplo n.º 3
0
        protected override async Task <InvokeResponse> ProcessMessageRequestAsync(HttpRequest request, BotFrameworkAdapter botFrameworkAdapter, Func <ITurnContext, Task> botCallbackHandler, CancellationToken cancellationToken)
        {
            const string BotAppIdHttpHeaderName        = "MS-BotFramework-BotAppId";
            const string BotIdQueryStringParameterName = "BotAppId";

            if (!request.Headers.TryGetValue(BotAppIdHttpHeaderName, out var botAppIdHeaders))
            {
                if (!request.Query.TryGetValue(BotIdQueryStringParameterName, out botAppIdHeaders))
                {
                    throw new InvalidOperationException($"Expected a Bot App ID in a header named \"{BotAppIdHttpHeaderName}\" or in a querystring parameter named \"{BotIdQueryStringParameterName}\".");
                }
            }

            var botAppId = botAppIdHeaders.First();
            var conversationReference = default(ConversationReference);

            using (var bodyReader = new JsonTextReader(new StreamReader(request.Body, Encoding.UTF8)))
            {
                conversationReference = BotMessageHandlerBase.BotMessageSerializer.Deserialize <ConversationReference>(bodyReader);
            }

            await botFrameworkAdapter.ContinueConversationAsync(botAppId, conversationReference, botCallbackHandler, cancellationToken);

            return(null);
        }
Ejemplo n.º 4
0
        public async Task <ObjectResult> Post([FromBody] ProactiveMessage proMsg)
        {
            await _adapter.ContinueConversationAsync(proMsg.BotId, proMsg.Conversation, async (context, token) =>
            {
                await context.SendActivityAsync(proMsg.MessagePayload);
            }, new System.Threading.CancellationToken());

            return(Ok("Message Sent"));
        }
        public async Task ContinueConversationAsyncWithAudience()
        {
            // Arrange
            var mockCredentialProvider = new Mock <ICredentialProvider>();
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>();
            var httpClient             = new HttpClient(mockHttpMessageHandler.Object);
            var adapter = new BotFrameworkAdapter(mockCredentialProvider.Object, customHttpClient: httpClient);

            // Create ClaimsIdentity that represents Skill2-to-Skill1 communication
            var skill2AppId = "00000000-0000-0000-0000-000000skill2";
            var skill1AppId = "00000000-0000-0000-0000-000000skill1";

            var skillClaims = new List <Claim>
            {
                new Claim(AuthenticationConstants.AudienceClaim, skill1AppId),
                new Claim(AuthenticationConstants.AppIdClaim, skill2AppId),
                new Claim(AuthenticationConstants.VersionClaim, "1.0")
            };
            var skillsIdentity   = new ClaimsIdentity(skillClaims);
            var skill2ServiceUrl = "https://continuetest.skill2.com/api/skills/";

            // Skill1 is calling ContinueSkillConversationAsync() to proactively send an Activity to Skill 2
            var callback = new BotCallbackHandler((turnContext, ct) =>
            {
                GetAppCredentialsAndAssertValues(turnContext, skill1AppId, skill2AppId, 1);
                GetConnectorClientsAndAssertValues(
                    turnContext,
                    skill1AppId,
                    skill2AppId,
                    new Uri(skill2ServiceUrl),
                    1);

                // Get "skill1-to-skill2" ConnectorClient off of TurnState
                var contextAdapter = turnContext.Adapter as BotFrameworkAdapter;
                var clientCache    = GetCache <ConcurrentDictionary <string, ConnectorClient> >(contextAdapter, ConnectorClientsCacheName);
                clientCache.TryGetValue($"{skill2ServiceUrl}{skill1AppId}:{skill2AppId}", out var client);

                var turnStateClient = turnContext.TurnState.Get <IConnectorClient>();
                var clientCreds     = turnStateClient.Credentials as AppCredentials;

                Assert.Equal(skill1AppId, clientCreds.MicrosoftAppId);
                Assert.Equal(skill2AppId, clientCreds.OAuthScope);
                Assert.Equal(client.BaseUri, turnStateClient.BaseUri);

                var scope = turnContext.TurnState.Get <string>(BotAdapter.OAuthScopeKey);
                Assert.Equal(skill2AppId, scope);

                return(Task.CompletedTask);
            });

            // Create ConversationReference to send a proactive message from Skill1 to Skill2
            var refs = new ConversationReference(serviceUrl: skill2ServiceUrl);

            await adapter.ContinueConversationAsync(skillsIdentity, refs, skill2AppId, callback, default);
        }
Ejemplo n.º 6
0
 private async Task SendProactiveMessage()
 {
     foreach (var conversationReference in _conversationReferences.Values)
     {
         await adapter.ContinueConversationAsync(
             _appId,
             conversationReference,
             BotCallback,
             default(CancellationToken)
             );
     }
 }