Example #1
0
        public async Task OnTurn(ITurnContext context)
        {
            var state         = context.GetConversationState <ReservationData>();
            var dialogContext = _dialogs.CreateContext(context, state);
            await dialogContext.Continue();

            // This bot is only handling Messages
            if (context.Activity.Type == ActivityTypes.Message)
            {
                // Get the conversation state from the turn context
                // var state = context.GetConversationState<EchoState>();

                // Bump the turn count.
                // state.TurnCount++;

                // Echo back to the user whatever they typed.
                // await context.SendActivity($"Turn {state.TurnCount}: You sent '{context.Activity.Text}'");

                if (!context.Responded)
                {
                    var result    = context.Services.Get <RecognizerResult>(LuisRecognizerMiddleware.LuisRecognizerResultKey);
                    var topIntent = result?.GetTopScoringIntent();

                    switch (topIntent != null ? topIntent.Value.intent : null)
                    {
                    case "TodaysSpecialty":
                        // await context.SendActivity($"For today we have the following options: {string.Join(", ", BotConstants.Specialties)}");
                        await TodaysSpecialtiesHandler(context);

                        break;

                    case "ReserveTable":
                        var amountPeople = result.Entities["AmountPeople"] != null ? (string)result.Entities["AmountPeople"]?.First : null;
                        var time         = GetTimeValueFromResult(result);
                        ReservationHandler(dialogContext, amountPeople, time);
                        break;

                    case "GetDiscounts":
                        await GetDiscountsHandler(context);

                        break;

                    default:
                        await context.SendActivity("Sorry, I didn't understand that.");

                        break;
                    }
                }
            }
            else if (context.Activity.Type == ActivityTypes.ConversationUpdate && context.Activity.MembersAdded.FirstOrDefault()?.Id == context.Activity.Recipient.Id)
            {
                var msg = "Hi! I'm a restaurant assistant bot. I can help you with your reservation.";
                await context.SendActivity(msg, _ttsService.GenerateSsml(msg, BotConstants.EnglishLanguage));
            }
        }
Example #2
0
 /// <summary>
 /// Sends a text.
 /// </summary>
 /// <param name="msg">The text.</param>
 /// <param name="context">The bot context.</param>
 /// <returns>The response of the bot framework.</returns>
 public virtual async Task <ResourceResponse> SendMessage(string msg, ITurnContext ctx)
 {
     if (_ttsLanguage == null)
     {
         return(await ctx.SendActivityAsync(msg));
     }
     return(await ctx.SendActivityAsync(msg, TextToSpeechService.GenerateSsml(CleanupTextForSpeech(msg), _ttsLanguage)));
 }
Example #3
0
        private async Task TranslateMessageAsync(ITurnContext turnContext, IMessageActivity message, CancellationToken cancellationToken, bool receivingMessage = false)
        {
            var text     = message.Text;
            var audioUrl = GetAudioUrl(turnContext.Activity);
            var state    = await ReservationStateAccessor.GetAsync(turnContext, () => new ReservationData(), cancellationToken);

            var conversationLanguage = receivingMessage ? turnContext.Activity.Locale : state.ConversationLanguage ?? BotConstants.EnglishLanguage;

            if (string.IsNullOrEmpty(state.ConversationLanguage) || !conversationLanguage.Equals(state.ConversationLanguage))
            {
                state.ConversationLanguage = conversationLanguage;
            }

            // Skip translation if the source language is already English
            if (!conversationLanguage.Contains(BotConstants.EnglishLanguage))
            {
                // STT target language will be English for this lab
                if (!string.IsNullOrEmpty(audioUrl) && string.IsNullOrEmpty(text))
                {
                    var transcript = await this._speechTranslationService.SpeechToTranslatedTextAsync(audioUrl, conversationLanguage, BotConstants.EnglishLanguage);

                    if (transcript != null)
                    {
                        text = transcript.Translation;
                    }
                }
                else
                {
                    // Use TTS translation
                    text = await _translatorTextService.Translate(BotConstants.EnglishLanguage, conversationLanguage, message.Text);

                    turnContext.Activity.Text = text;

                    var ssml = _textToSpeechService.GenerateSsml(text, conversationLanguage);
                    await SendAudioResponseAsync(turnContext, text, ssml);
                }
            }

            message.Text = text;
        }
Example #4
0
        private async Task <DialogTurnResult> TimeStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var state = await UserProfileAccessor.GetAsync(stepContext.Context);

            if (string.IsNullOrEmpty(state.Time))
            {
                var msg  = "When do you need the reservation?";
                var opts = new PromptOptions
                {
                    Prompt = new Activity
                    {
                        Type  = ActivityTypes.Message,
                        Text  = msg,
                        Speak = _ttsService.GenerateSsml(msg, BotConstants.EnglishLanguage),
                    },
                };
                return(await stepContext.PromptAsync(TimePrompt, opts));
            }
            else
            {
                return(await stepContext.NextAsync());
            }
        }