-->

FormFlow prompts are not being spoken in Cortana S

2019-05-06 22:24发布

问题:

I am building a Coratana skill by first building a bot, using FormFlow. I detect my intents and entities using LUIS and pass the entities to my FormFlow dialog. If one or more FormFlow fields is not filled in, FormFlow dialog prompts the user to fill in the missing information, but this prompt is not spoken, only shows on the cortana screen. Is there any way for FormFlow to speak the prompts?

In the screenshot shown below, the prompt "Do you need airport shuttle?" was just displayed and not spoken:

My formFlow definition looks like this:

 [Serializable]
public class HotelsQuery
{
    [Prompt("Please enter your {&}")]
    [Optional]
    public string Destination { get; set; }

    [Prompt("Near which Airport")]
    [Optional]
    public string AirportCode { get; set; }

    [Prompt("Do you need airport shuttle?")]
    public string DoYouNeedAirportShutle { get; set; }
}

回答1:

I don't think Speak is currently supported in FormFlow.

What you could do, as a workaround is adding an IMessageActivityMapper that basically promote text to speak automatically.

namespace Code
{
    using Microsoft.Bot.Builder.Dialogs;
    using Microsoft.Bot.Builder.Dialogs.Internals;
    using Microsoft.Bot.Connector;

    /// <summary>
    /// Activity mapper that automatically populates activity.speak for speech enabled channels.
    /// </summary>
    public sealed class TextToSpeakActivityMapper : IMessageActivityMapper
    {
        public IMessageActivity Map(IMessageActivity message)
        {
            // only set the speak if it is not set by the developer.
            var channelCapability = new ChannelCapability(Address.FromActivity(message));

            if (channelCapability.SupportsSpeak() && string.IsNullOrEmpty(message.Speak))
            {
                message.Speak = message.Text;
            }

            return message;
        }
    }
}

Then to use it, you need to register it in your Global.asax.cs file as:

 var builder = new ContainerBuilder();

 builder
   .RegisterType<TextToSpeakActivityMapper>()
   .AsImplementedInterfaces()
   .SingleInstance();

 builder.Update(Conversation.Container);


回答2:

The answer form Ezequiel Jadib helped me to solve what I needed for my use case. I just added a few additional lines to set the InputHint field to ExpectingInput if the text is a question. With this configuration Cortana automatically listens to my answer and I don't have to activate the microphone by myself.

public IMessageActivity Map(IMessageActivity message)
{
    // only set the speak if it is not set by the developer.
    var channelCapability = new ChannelCapability(Address.FromActivity(message));

    if (channelCapability.SupportsSpeak() && string.IsNullOrEmpty(message.Speak))
    {
        message.Speak = message.Text;

        // set InputHint to ExpectingInput if text is a question
        var isQuestion = message.Text?.EndsWith("?");
        if (isQuestion.GetValueOrDefault())
        {
            message.InputHint = InputHints.ExpectingInput;
        }
    }

    return message;
}