Wait for Even type Activity in a waterfallstep dialog (bot framework 4.0)

我只是一个虾纸丫 提交于 2021-02-10 15:28:11

问题


It's possible to wait and receive an Event type activity in a waterfall step dialog. I use directline 3.0 and inside a dialog flow I send an event from the bot to the client. After i would like to send an event from the client to the bot as an answer to previous send. If i use prompt await dc.Prompt("waitEvent",activity) where waitEvent is a textprompt and i answer with a message it works fine but I would like to answer to an event with an event. I was thinking that i could write a custom prompt but i didn't find documentation and obviously I could manage the conversation flow but I prefer use Dialogs where possible


回答1:


You can use the ActivityPrompt abstract class to build an "EventActivityPrompt" class.

There aren't any BotFramework samples of this usage yet, but there are new tests written by the BotFramework team that you can use as an example.

To create your own EventActivityPrompt, you just need to implement the ActivityPrompt like so:

public class EventActivityPrompt : ActivityPrompt
{
    public EventActivityPrompt(string dialogId, PromptValidator<Activity> validator)
        : base(dialogId, validator)
    {
    }
}

The core difference between an ActivityPrompt and other Prompts (besides its abstract status) is that ActivityPrompts require a PromptValidator<Activity>, in order to validate the user input.

The next step is to create your validator. Here is the example:

async Task<bool> _validator(PromptValidatorContext<Activity> promptContext, CancellationToken cancellationToken)
    {
        var activity = promptContext.Recognized.Value;
        if (activity.Type == ActivityTypes.Event)
        {
            if ((int)activity.Value == 2)
            {
                promptContext.Recognized.Value = MessageFactory.Text(activity.Value.ToString());
                return true;
            }
        }
        else
        {
            await promptContext.Context.SendActivityAsync("Please send an 'event'-type Activity with a value of 2.");
        }
        return false;
    }


来源:https://stackoverflow.com/questions/52514248/wait-for-even-type-activity-in-a-waterfallstep-dialog-bot-framework-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!