Пример #1
0
        public static string CombineAudioAndTextForSSML(Uri audioStream, string text)
        {
            StringBuilder sb         = new StringBuilder();
            const string  ssmlPrefix = @"<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' " +
                                       "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
                                       "xsi:schemaLocation='http://www.w3.org/2001/10/synthesis " +
                                       "http://www.w3.org/TR/speech-synthesis/synthesis.xsd' " +
                                       "xml:lang='en-us'>";
            const string ssmlSuffix = "</speak>";

            sb.Append(ssmlPrefix);
            sb.Append($"<audio src='{audioStream.AbsoluteUri}'/>");
            sb.Append(GetInnerSsmlContents(SsmlWrapper.Wrap(text)));
            sb.Append(ssmlSuffix);
            return(sb.ToString());
        }
Пример #2
0
        private static IMessageActivity CreateResponse
        (
            IDialogContext context,
            IMessageActivity message,
            string displayText,
            string speakText,
            MessageType messageType,
            string audioToPlay          = null,
            IList <string> optionsToAdd = null,
            string inputHint            = InputHints.AcceptingInput
        )
        {
            var activityToSend = context.MakeMessage();

            if (displayText != null)
            {
                activityToSend.Text = displayText;
            }

            var ssml = (string)null;

            if (speakText != null)
            {
                var escapedSpeakText = speakText.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
                ssml = SsmlWrapper.Wrap(escapedSpeakText);

                if (audioToPlay != null && message.ChannelId != "cortana")
                {
                    var assetPath = "http://" + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + "/Assets/";
                    var uri       = new Uri(assetPath + audioToPlay);
                    ssml = CombineAudioAndTextForSSML(uri, ssml);
                }
            }

            activityToSend.Speak     = ssml;
            activityToSend.InputHint = inputHint;

            var appEntities =
                new AppEntities
            {
                MessageType         = messageType,
                TriviaAnswerOptions = optionsToAdd?.Count > 0 ? optionsToAdd : null
            };

            if (optionsToAdd != null)
            {
                List <CardAction> cardButtons = new List <CardAction>();

                bool numberOptions = optionsToAdd.Count > 2;
                for (int i = 0; i < optionsToAdd.Count; i++)
                {
                    var display = numberOptions ? $"{(i + 1).ToString()}: {optionsToAdd[i]}" : optionsToAdd[i];
                    cardButtons.Add(new CardAction()
                    {
                        Value = optionsToAdd[i], Type = "postBack", Title = display
                    });
                }

                var plCard = new ThumbnailCard()
                {
                    // Title = "Pick an answer",
                    Buttons = cardButtons,
                };
                activityToSend.Attachments.Add(plCard.ToAttachment());
            }

            var entity = new Entity();

            entity.SetAs <AppEntities>(appEntities);
            activityToSend.Entities.Add(entity);

            return(activityToSend);
        }