예제 #1
0
        private async Task HandleIncomingMessage(ITurnContext turnContext, CancellationToken cancellationToken)
        {
            if (!this.translator.IsConfigured || turnContext.Activity.Type != ActivityTypes.Message)
            {
                return;
            }

            // Store the original incoming text so that it is still available after translation.
            turnContext.Activity.ChannelData = turnContext.Activity.Text;

            var user = await api.GetUser(turnContext);

            // Translate messages sent from the user to the default language.
            if (user.Language != Translator.DefaultLanguage)
            {
                // For the first message we need to detect the user's language and store it.
                if (string.IsNullOrEmpty(user.Language))
                {
                    var result = await this.translator.TranslateToDataAsync(turnContext.Activity.Text, Translator.DefaultLanguage, cancellationToken);

                    turnContext.Activity.Text = result?.Translations?.FirstOrDefault()?.Text;

                    user.Language = result?.DetectedLanguage?.Language ?? Translator.DefaultLanguage;
                    await this.api.Update(user);
                }
                else
                {
                    await TranslateAsync(turnContext.Activity, Translator.DefaultLanguage, cancellationToken);
                }
            }
        }
        public static PromptValidator <FoundChoice> Create(IApiInterface api)
        {
            return(async(promptContext, cancellationToken) =>
            {
                if (!promptContext.Recognized.Succeeded)
                {
                    return await Task.FromResult(false);
                }

                if (promptContext.Recognized.Value.Value == Phrases.None)
                {
                    return await Task.FromResult(true);
                }

                var user = await api.GetUser(promptContext.Context);

                var resources = await api.GetResourcesForUser(user);

                // Make sure the index and name still match up in case the schema changed.
                var selectedChoice = promptContext.Recognized.Value;
                var selectedIndex = selectedChoice.Index;

                if (selectedIndex >= resources.Count)
                {
                    return await Task.FromResult(false);
                }

                return await Task.FromResult(resources[selectedIndex].Name == selectedChoice.Value);
            });
        }
예제 #3
0
        public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Create the user if they haven't been seen yet.
            var user = await api.GetUser(turnContext);

            if (user == null)
            {
                user             = new User();
                user.PhoneNumber = Helpers.GetUserToken(turnContext);
                await this.api.Create(user);
            }

            // Invoke the next middleware.
            await next(cancellationToken).ConfigureAwait(false);
        }