public async Task <IActionResult> Post()
        {
            var request = await client.GetRequest(Request.Headers["SignatureCEK"], Request.Body, true);

            var response = new CEKResponse();

            // get session information by specifying default value in case no session information
            var mySessionValue = request.GetSessionAttribute("mySessionKey", "defaultValue");

            // adding session information
            response.AddSession("mySessionKey", "mySessionValue");

            switch (request.Request.Type)
            {
            case RequestType.LaunchRequest:
                // Single Text Reply
                response.AddText("Welcome to CEK", Lang.En);
                response.ShouldEndSession = false;
                break;

            case RequestType.SessionEndedRequest:
                response.AddText("Good bye!", Lang.En);
                response.ShouldEndSession = true;
                break;

            case RequestType.IntentRequest:
                switch (request.Request.Intent.Name)
                {
                case "Clova.YesIntent":
                    // Add single URL Response and Text Reprompt
                    response.AddUrl("https://clova-common.line-scdn.net/dice/rolling_dice_sound.mp3");
                    response.AddRepromptText("Tell me something, please", Lang.En);
                    response.ShouldEndSession = false;
                    break;

                case "Clova.NoIntent":
                    // Add Brief and Verbose as SpeechSet
                    response.AddBriefText("Brief explain.", Lang.En);
                    response.AddVerboseText("Detail explain 1.", Lang.En);
                    response.AddVerboseText("Detail explain 2.", Lang.En);
                    response.AddVerboseUrl("https://clova-common.line-scdn.net/dice/rolling_dice_sound.mp3");
                    response.ShouldEndSession = false;
                    break;

                case "Clova.GuideIntent":
                    // Add multiple Reposonses and Reprompts
                    response.AddText("Sure!", Lang.En);
                    response.AddUrl("https://clova-common.line-scdn.net/dice/rolling_dice_sound.mp3");
                    response.AddText("Let me explain how to use it!", Lang.En);
                    response.AddRepromptText("Did you understand?", Lang.En);
                    response.AddRepromptText("Now tell me what you want.", Lang.En);
                    response.AddRepromptUrl("https://clova-common.line-scdn.net/dice/rolling_dice_sound.mp3");
                    response.ShouldEndSession = false;
                    break;
                }
                break;
            }
            return(new OkObjectResult(response));
        }
Exemplo n.º 2
0
        private static async Task <IActionResult> ExecuteLaunchRequestAsync(CEKRequest request, CloudTable locationLogs, AppConfiguration config)
        {
            var response           = new CEKResponse();
            var taskForSettings    = MessagingChatSettings.GetSettingsByUserIdAsync(locationLogs, request.Session.User.UserId);
            var taskForLocationLog = LocationLog.GetLocationLogByUserIdAsync(locationLogs, request.Session.User.UserId);
            await Task.WhenAll(taskForSettings, taskForLocationLog);

            var settings = taskForSettings.Result ?? new MessagingChatSettings {
                RowKey = request.Session.User.UserId
            };
            var locationLog = taskForLocationLog.Result;

            try
            {
                if (!settings.IsLineFrend)
                {
                    response.AddText(ClovaMessages.GetAddingLineFrendMessage());
                    return(new OkObjectResult(response));
                }

                AddHistory(settings);
                response.AddText(ClovaMessages.GetGuideMessage(settings.YourName));
                if (locationLog == null || !DateTimeOffsetUtils.IsToday(locationLog.Timestamp))
                {
                    // データが無い
                    response.AddText(ClovaMessages.GetNoLogMessage(settings.YourName));
                    await AskCurrentLocationAsync(request, config, settings);

                    return(new OkObjectResult(response));
                }

                if (DateTimeOffsetUtils.IsBefore(locationLog.Timestamp, TimeSpan.Parse(config.Cek.IsBeforeThreshold ?? Clova.IsBeforeThresholdDefaultValue)))
                {
                    // 古いデータ
                    response.AddText(ClovaMessages.GetOldLocationMessage(settings.YourName, locationLog));
                    await AskCurrentLocationAsync(request, config, settings);

                    return(new OkObjectResult(response));
                }

                // データがある
                response.AddText(ClovaMessages.GetLocationMessage(settings.YourName, locationLog));
                if (!string.IsNullOrEmpty(locationLog.Comment))
                {
                    response.AddText(ClovaMessages.GetCommentMessage(settings.YourName, locationLog));
                }
                else if (!string.IsNullOrEmpty(locationLog.AudioCommentUrl))
                {
                    response.AddText(ClovaMessages.GetVoiceMessagePrefixMessage(settings.YourName));
                    response.AddUrl(locationLog.AudioCommentUrl);
                }

                return(new OkObjectResult(response));
            }
            finally
            {
                await locationLogs.ExecuteAsync(TableOperation.InsertOrReplace(settings));
            }
        }
Exemplo n.º 3
0
        public CEKResponse ToClovaResponse()
        {
            var response = new CEKResponse();

            // normal
            foreach (var output in OutputObjects
                     .Where(output => output.TargetPlatform == Platform.All || output.TargetPlatform == Platform.Clova))
            {
                if (output.Type == OutputType.Text)
                {
                    response.AddText(output.Value);
                }
                else if (output.Type == OutputType.Url)
                {
                    response.AddUrl(output.Value);
                }
                else if (output.Type == OutputType.Break)
                {
                    if (output.BreakTime <= 5)
                    {
                        response.AddUrl($"https://himanago.github.io/silent-mp3/silent_{output.BreakTime}s.mp3");
                    }
                    else
                    {
                        response.AddUrl($"https://himanago.github.io/silent-mp3/silent_5s.mp3");
                        response.AddUrl($"https://himanago.github.io/silent-mp3/silent_{output.BreakTime - 5}s.mp3");
                    }
                }
            }

            // AudioPlayer
            foreach (var audio in AudioItemObjects
                     .Where(audio => audio.TargetPlatform == Platform.All || audio.TargetPlatform == Platform.Clova))
            {
                response.Response.Directives.Add(new Directive()
                {
                    Header = new DirectiveHeader()
                    {
                        Namespace = DirectiveHeaderNamespace.AudioPlayer,
                        Name      = DirectiveHeaderName.Play
                    },
                    Payload = new AudioPlayPayload
                    {
                        AudioItem = new LineDC.CEK.Models.AudioItem
                        {
                            AudioItemId   = audio.AudioItemId,
                            HeaderText    = audio.Title,
                            TitleText     = audio.Title,
                            TitleSubText1 = audio.SubTitle,
                            ArtImageUrl   = audio.ImageUrl,
                            Stream        = new AudioStreamInfoObject
                            {
                                BeginAtInMilliseconds = 0,
                                Url         = audio.Url,
                                UrlPlayable = true
                            }
                        },
                        PlayBehavior = audio.AudioPlayBehavior == AudioPlayBehavior.Enqueue
                            ? LineDC.CEK.Models.AudioPlayBehavior.ENQUEUE
                            : LineDC.CEK.Models.AudioPlayBehavior.REPLACE_ALL,

                        Source = new Source {
                            Name = audio.Title
                        }
                    }
                });
            }

            if (!ShouldEndSession)
            {
                response.KeepListening();
            }

            return(response);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Post()
        {
            var request = await clova.GetRequest(Request.Headers["SignatureCEK"], Request.Body);

            var userId = request.Session.User.UserId;

            answer_count = request.Session.SessionAttributes.ContainsKey("answer_count") ?
                           int.Parse(request.Session.SessionAttributes["answer_count"].ToString()) : 1;
            correct_count = request.Session.SessionAttributes.ContainsKey("correct_count") ?
                            int.Parse(request.Session.SessionAttributes["correct_count"].ToString()) : 0;
            quizNum = request.Session.SessionAttributes.ContainsKey("quiz_num") ?
                      int.Parse(request.Session.SessionAttributes["quiz_num"].ToString()) : 1;

            userQuizCache = QuizService.GetUserQuizCache(userId);
            currentQuiz   = QuizService.GetQuizByNumber(userQuizCache.CurrentQuizNumber);

            switch (request.Request.Type)
            {
            case RequestType.LaunchRequest:
                userQuizCache = QuizService.InitializeUserQuizCache(userId);
                currentQuiz   = QuizService.GetQuizByNumber(userQuizCache.CurrentQuizNumber);
                response.AddText($"イングリッシュクイズへようこそ!英語でクイズを出すので、日本語で回答してください!全部で{userQuizCache.UserQuizes.Count}問です。1問につき3度まで回答できます。");
                response.AddText($"では第{quizNum}問");
                response.AddUrl("https://eigodequiz.azurewebsites.net/assets/quizintro.mp3");
                response.AddText(currentQuiz.QuestionType);
                response.AddText(currentQuiz.Question, Lang.En);
                SaveSession(quizNum, answer_count, correct_count);
                SetReprompt();
                break;

            case RequestType.SessionEndedRequest:
                response.AddText("また挑戦してください。");     // This doesn't seems to work..
                break;

            case RequestType.IntentRequest:
                switch (request.Request.Intent.Name)
                {
                case "Answer":
                case "Clova.GuideIntent":
                case "Clova.YesIntent":
                case "Clova.NoIntent":
                    var answer = request.Request.Intent.Slots.ContainsKey("Answer") ? request.Request.Intent.Slots["Answer"] : null;
                    if (answer == null || answer.Value != currentQuiz.Answer)
                    {
                        if (answer_count == 3)
                        {
                            response.AddText($"残念!正解は{currentQuiz.Answer}でした。");
                            SetNextQuestion();
                        }
                        else
                        {
                            if (answer_count == 1)
                            {
                                if (answer != null && answer.Value == "ヒント")
                                {
                                    response.AddText($"1つ目のヒントです。");
                                }
                                else
                                {
                                    response.AddText($"{answer_count}回目の不正解です。ヒントです。");
                                }
                                response.AddText(currentQuiz.Hint1, Lang.En);
                            }
                            else
                            {
                                if (answer != null && answer.Value == "ヒント")
                                {
                                    response.AddText($"最後のヒントです。");
                                }
                                else
                                {
                                    response.AddText($"{answer_count}回目の不正解です。最後のヒントです。");
                                }
                                response.AddText(currentQuiz.Hint2, Lang.En);
                            }
                            answer_count++;
                            SaveSession(quizNum, answer_count, correct_count);
                            SetReprompt();
                        }
                    }
                    else
                    {
                        response.AddText($"正解です!");
                        correct_count++;
                        SetNextQuestion();
                    }
                    break;
                }
                break;
            }
            return(new OkObjectResult(response));
        }