Search code examples
c#botframeworkdirect-line-botframework

In the Bot Framework, Is there a common location where I can set the speak property to true in ChannelData?


I am using the Bot Framework to build a speech enabled Bot which handles various events, and triggers a Dialog based on the event. The Bot is connected using WebChat as the interface. As this is mostly a proactive scenario, there is no prior input from the user. As a result, even though the dialog is triggered, there is no output speech, since the speech is activated only if the previous interaction was via Speech. To enable output speech, I right now have to explicitly set the ChannelData of every outgoing activity with 'speak' property as true using activity.ChannelData = new {speak = true};, which WebChat understands , and voices out the message associated with the Activity.

Is there a more efficient way to do this, by setting this property in a common location, so that all outgoing activities by default will be spoken out?


Solution

  • To run any code at all whenever the turn context sends an activity, you can use TurnContext.OnSendActivities:

    turnContext.OnSendActivities(async (tc, activities, next) =>
    {
        activities.ForEach(activity => activity.ChannelData = new { speak = true });
    
        return await next().ConfigureAwait(false);
    });
    

    This is often done with middleware, but you could possibly choose to do it at the start of your main bot logic.