Ejemplo n.º 1
0
        /// <summary>
        ///     Every conversation turn for our Echo Bot will call this method.
        ///     There are no dialogs used, since it's "single turn" processing, meaning a single
        ///     request and response.
        /// </summary>
        /// <param name="turnContext">
        ///     A <see cref="ITurnContext" /> containing all the data needed
        ///     for processing this conversation turn.
        /// </param>
        /// <param name="cancellationToken">
        ///     (Optional) A <see cref="CancellationToken" /> that can be used by other objects
        ///     or threads to receive notice of cancellation.
        /// </param>
        /// <returns>A <see cref="Task" /> that represents the work queued to execute.</returns>
        /// <seealso cref="BotStateSet" />
        /// <seealso cref="ConversationState" />
        /// <seealso cref="IMiddleware" />
        public async Task OnTurnAsync(ITurnContext turnContext,
                                      CancellationToken cancellationToken = default(CancellationToken))
        {
            // Handle Message activity type, which is the main activity type for shown within a conversational interface
            // Message activities may contain text, speech, interactive cards, and binary or unknown attachments.
            // see https://aka.ms/about-bot-activity-message to learn more about the message and other activity types
            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                // Get the conversation state from the turn context.
                var state = await _accessors.CounterState.GetAsync(turnContext, () => new CounterState());

                // Bump the turn count for this conversation.
                state.TurnCount++;

                // Set the property using the accessor.
                await _accessors.CounterState.SetAsync(turnContext, state);

                // Save the new turn count into the conversation state.
                await _accessors.ConversationState.SaveChangesAsync(turnContext);

                IMessageActivity responseMessageActivity;
                switch (turnContext.Activity.Text.ToLower())
                {
                case "start":
                    responseMessageActivity      = turnContext.Activity.CreateReply();
                    responseMessageActivity.Text = $"Start conversation {state.TurnCount}";
                    break;

                case "local test":
                    //로컬에서 테스트할 때
                    var fileFullPath = Environment.GetEnvironmentVariable("HOME") + "test.wav";
                    var testResult   = await _speechHelper.GetSpeechResultFromAudioAsync(fileFullPath);

                    if (testResult == null)
                    {
                        return;
                    }

                    var mp3FileName = $"{testResult.DisplayText}.mp3";
                    var list        = await _blobHelper.ListBlobAsync();

                    var exist = list.Results.Cast <CloudBlockBlob>()
                                .FirstOrDefault(i => i.Name == mp3FileName);
                    if (exist == null)
                    {
                        var pollyResult =
                            await _pollyHelper.GetSynthesizeSpeechFromTextAsync(testResult.DisplayText);

                        if (pollyResult == null)
                        {
                            return;
                        }
                        await _blobHelper.UploadBlockBlobAsync(mp3FileName, pollyResult.AudioStream);
                    }

                    var testAttachment = await MakeAttachmentAsync(mp3FileName);

                    responseMessageActivity             = turnContext.Activity.CreateReply();
                    responseMessageActivity.Text        = testResult.DisplayText;
                    responseMessageActivity.Attachments = new List <Attachment> {
                        testAttachment
                    };
                    break;

                case "voice command":
                    var attachment = turnContext.Activity.Attachments.FirstOrDefault();
                    if (attachment == null)
                    {
                        responseMessageActivity = new Activity(text: $"No voice data found {state.TurnCount}");
                    }
                    else
                    {
                        var result = await _speechHelper.GetSpeechResultFromAttachmentAsync(attachment);

                        responseMessageActivity = await GetResponseFromSpeechResultAsync(turnContext, result);
                    }

                    break;

                default:
                    responseMessageActivity = new Activity(text: $"Unknown message {state.TurnCount}");
                    break;
                }

                await turnContext.SendActivityAsync(responseMessageActivity);
            }
            else
            {
                await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected");
            }
        }