How to use Suggested actions in Dialog and form fl

2019-07-31 15:41发布

问题:

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
    var cli = new ConnectorClient(new Uri(activity.ServiceUrl));
    var activity = await result as IMessageActivity;

    await context.PostAsync($"{activity.Text}");

    activity.SuggestedActions = new SuggestedActions()
    {
        Actions = new List<CardAction>()
        {
            new CardAction(){ Title = "Blue", Type=ActionTypes.ImBack, Value="Blue" },
            new CardAction(){ Title = "Red", Type=ActionTypes.ImBack, Value="Red" },
            new CardAction(){ Title = "Green", Type=ActionTypes.ImBack, Value="Green" }
        }
    };

    await context.PostAsync(activity);

    context.Wait(MessageReceivedAsync);
}

I want to make the bot suggest action to user, i need to do this in the dialog and also sometimes in form flow. i have not been able to figure it out. It actually worked when i tried in the message controller.

回答1:

Try creating a reply like:

    var activity = await result as Activity;
    var reply = activity.CreateReply("I have colors in mind, but need your help to choose the best one.");

    reply.SuggestedActions = new SuggestedActions()
    {
        Actions = new List<CardAction>()
        {
            new CardAction(){ Title = "Blue", Type=ActionTypes.ImBack, Value="Blue" },
            new CardAction(){ Title = "Red", Type=ActionTypes.ImBack, Value="Red" },
            new CardAction(){ Title = "Green", Type=ActionTypes.ImBack, Value="Green" }
        }
    };

    await context.PostAsync(reply);

    context.Wait(MessageReceivedAsync);

you should be able to paste this into you MessageReceivedAsync method and it should work. You also do not need to create your ClientConnector.