Example #1
0
        public ActionResult Post([FromBody] Activity activity)
        {
            var s = JsonConvert.SerializeObject(activity.ChannelData);
            var d = JsonConvert.DeserializeObject <List <string> >(s);
            ItemContextParameters parameters = (d.Any())
                ? JsonConvert.DeserializeObject <ItemContextParameters>(d[0])
                : new ItemContextParameters();

            if (activity.Type != ActivityTypes.Message)
            {
                return(null);
            }

            var result = !string.IsNullOrWhiteSpace(activity.Text) ? LuisService.Query(ChatSettings.OleApplicationId, activity.Text, true) : null;

            var conversationContext = ConversationContextFactory.Create(
                ChatSettings.OleApplicationId,
                Translator.Text("Chat.Clear"),
                Translator.Text("Chat.ConfirmMessage"),
                "decision - yes",
                "decision - no",
                "frustrated",
                "profile user - quit",
                parameters,
                result);

            var conversation   = conversationContext.GetCurrentConversation();
            var inConversation = conversation?.IsEnded ?? false;
            var isQuestion     = result.Query.Split(new char[] { ' ' }).Take(2).Any(a => QuestionWords.Contains(a));
            var response       = (!inConversation && isQuestion)
                ? IntentProvider.GetIntent(ChatSettings.OleApplicationId, "self - websearch").Respond(result, parameters, conversation);

                : LuisConversationService.ProcessUserInput(conversationContext);
            var newMessage = Regex.Replace(response.Message, "<.*?>", " ");

            var relativePath = $"temp\\ole-{CreateMD5Hash(newMessage)}.mp3";
            var filePath     = $"{Request.PhysicalApplicationPath}{relativePath}";

            var locale      = SpeechLocaleOptions.enUS;
            var voice       = VoiceName.EnUsGuyNeural;
            var gender      = GenderOptions.Male;
            var audioFormat = AudioOutputFormatOptions.Audio24Khz160KBitRateMonoMp3;

            SpeechService.TextToFile(newMessage, filePath, locale, voice, gender, audioFormat);

            var reply = activity.CreateReply(response.Message, "en-US");

            reply.ChannelData = new ChannelData
            {
                Input      = response.Input,
                Selections = response.Selections?.ToDictionary(a => a.Key, b => b.Value.DisplayName) ?? null,
                Action     = response.Action,
                AudioFile  = $"\\{relativePath}"
            };

            return(Json(reply));
        }
        public ActionResult CreateMediaFile(string id, string db, string language)
        {
            var page = DataWrapper.GetItemByIdValue(id, db);
            var html = page.Fields["Content"].Value;

            var sb   = new StringBuilder();
            var tags = new List <string> {
                "<sub", "</sub>", "<sup", "</sup>"
            };
            var arr          = html.Split(tags.ToArray(), StringSplitOptions.RemoveEmptyEntries);
            var shouldRemove = false;

            for (var i = 0; i < arr.Length; i++)
            {
                var el = arr[i];
                if (!el.StartsWith(">") && !shouldRemove)
                {
                    sb.Append(el);
                }

                shouldRemove = el.Contains("/");
            }

            var cleanText = Regex.Replace(sb.ToString(), "<.*?>", " ").Replace("\"", "").Replace("&", " and ").Replace("   ", " ").Replace("  ", " ");

            var maleRelativePath   = $"temp\\blogcast-{page.ID.Guid.ToString("N")}-male.mp3";
            var maleFilePath       = $"{Request.PhysicalApplicationPath}{maleRelativePath}";
            var femaleRelativePath = $"temp\\blogcast-{page.ID.Guid.ToString("N")}-female.mp3";
            var femaleFilePath     = $"{Request.PhysicalApplicationPath}{femaleRelativePath}";

            var locale      = SpeechLocaleOptions.enUS;
            var gender      = GenderOptions.Male;
            var audioFormat = AudioOutputFormatOptions.Audio24Khz160KBitRateMonoMp3;

            SpeechService.TextToFile(cleanText, maleFilePath, locale, VoiceName.EnUsGuyNeural, gender, audioFormat);
            SpeechService.TextToFile(cleanText, femaleFilePath, locale, VoiceName.EnUsJessaNeural, gender, audioFormat);

            return(Json(new { Succeeded = true }));
        }