Search code examples
c#botframeworkmessenger

Botframework V4: Messenger location, phone and email quick reply


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


Solution

  • 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!