Botframework V4:Messenger 位置、phone 和电子邮件快速回复

Botframework V4: Messenger location, phone and email quick reply

您好,我有这个代码,可以向用户发送包含位置的快速回复。 我把它放在文本提示中等待用户输入。但是在用户向其发送位置后,它会在 Messenger 上产生错误。我尝试了文本和附件提示,但它不起作用。

           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,
               });
        }

我正在使用 C# 和 Botframework V4

如果您想在文本或附件提示中使用 Facebook Messenger 的位置快速回复来捕获用户的位置,则需要提供自定义验证器 - 我建议使用文本提示。

构造函数

创建瀑布并将提示添加到构造函数中的对话框堆栈。请务必在文本提示中添加自定义验证器;否则,机器人会反复提示用户位置,因为它需要快速回复未提供的文本值。

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));

}

位置验证器

在自定义验证器中,您可以检查传入的 activity 位置对象,它位于 activity 的实体 属性 中。如果 activity 没有位置,您可以 return false 并且提示将再次询问用户他们的位置;否则,它将继续下一步。

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);
}  

提示位置

正如您在上面的代码片段中所做的那样,您可以将 Facebook Messenger 快速回复添加到回复的频道数据。

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);
}

捕获位置

您可以在此处捕获用户位置,以随心所欲地使用。

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);
}

希望对您有所帮助!