Botframework V4: Messenger location, phone and email quick reply

青春壹個敷衍的年華 提交于 2019-11-27 08:48:47

问题


Hello i have this code on that sends a quick reply with location to the user. I put it in a text prompt to wait for user input. But its producing an error on messenger after the user sends it location. i tried text and attachment prompt but it is not working.

           Activity reply = stepContext.Context.Activity.CreateReply();

            reply.ChannelData = JObject.FromObject(
            new
            {
                text = "loc",
                quick_replies = new object[]
                {
                    new
                    {
                        content_type = "location",
                    },
                },
            });

            return await stepContext.PromptAsync(
               ATTACHPROMPT,
               new PromptOptions
               {
                   Prompt = reply,
               });
        }

I am using C# and Botframework V4


回答1:


You need to provide a custom validator if you want to capture a user's location with Facebook Messenger's location quick reply in a Text or Attachment Prompt - I would recommend using a Text Prompt.

Constructor

Create your waterfall and add your prompts to the dialog stack in your constructor. Be sure to add a custom validator to the text prompt; otherwise, the bot will repeatedly prompt the user for their location since it is expecting a text value which the quick reply does not provide.

public MultiTurnPromptsBot(MultiTurnPromptsBotAccessors accessors)
{
    ...
    // This array defines how the Waterfall will execute.
    var waterfallSteps = new WaterfallStep[]
    {
        PromptForLocation,
        CaptureLocation,
    };
    ...
    // Add named dialogs to the DialogSet. These names are saved in the dialog state.
    _dialogs.Add(new WaterfallDialog("details", waterfallSteps));
    _dialogs.Add(new TextPrompt("location", LocationPromptValidatorAsync));

}

Location Validator

In the custom validator, you can check the incoming activity for the location object, which is in the activity's entities property. If the activity doesn't have a location, you can return false and the prompt will ask the user for their location again; otherwise, it will continue onto the next step.

public Task<bool> LocationPromptValidatorAsync(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
{
    var activity = promptContext.Context.Activity;
    var location = activity.Entities?.FirstOrDefault(e => e.Type == "Place");
    if (location != null) {
        return Task.FromResult(true);
    }
    return Task.FromResult(false);
}  

Prompt for Location

As you had in your code snippet above, you can add the Facebook Messenger quick reply to the reply's channel data.

private static async Task<DialogTurnResult> PromptForLocation(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    Activity reply = stepContext.Context.Activity.CreateReply();
    reply.Text = "What is your location?";
    reply.ChannelData = JObject.FromObject( new {

        quick_replies = new object[]
        {
            new
            {
                content_type = "location",
            },
        },
    });

    return await stepContext.PromptAsync("location", new PromptOptions { Prompt = reply }, cancellationToken);
}

Capture Location

Here you can capture the user location to use how ever you'd like.

private async Task<DialogTurnResult> CaptureLocation(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{

    var activity = stepContext.Context.Activity;
    var location = activity.Entities?.FirstOrDefault(e => e.Type == "Place");
    if (location != null) {
        var latitude = location.Properties["geo"]?["latitude"].ToString();
        var longitude = location.Properties["geo"]?["longitude"].ToString();

        await stepContext.Context.SendActivityAsync($"Latitude: {latitude} Longitude: {longitude}");

    }
    // WaterfallStep always finishes with the end of the Waterfall or with another dialog, here it is the end.
    return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
}

Hope this helps!



来源:https://stackoverflow.com/questions/55331264/botframework-v4-messenger-location-phone-and-email-quick-reply

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