Botframework V4:Messenger位置,电话和电子邮件快速回复 [英] Botframework V4: Messenger location, phone and email quick reply
问题描述
您好,我在此代码上向用户发送了带有位置的快速回复. 我将其放在文本提示中,以等待用户输入.但是在用户发送位置后,它在Messenger上产生了错误.我尝试输入文字和附件提示,但无法正常工作.
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,
});
}
我正在使用C#和Botframework V4
I am using C# and Botframework V4
推荐答案
如果要通过文本或附件提示中的Facebook Messenger位置快速回复来捕获用户的位置,则需要提供自定义验证器-我建议您使用文字提示.
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.
构造函数
创建瀑布并将提示添加到构造函数中的对话框堆栈中.确保将自定义验证器添加到文本提示;否则,该机器人会反复提示用户输入他们的位置,因为它期望快速回复无法提供的文本值.
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));
}
位置验证器
在自定义验证器中,您可以检查位置对象的传入活动,该活动位于活动的实体属性中.如果活动没有位置,则可以返回false,提示将再次询问用户其位置;否则,它将继续进行下一步.
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);
}
提示输入位置
与上面的代码段一样,您可以将Facebook Messenger快速回复添加到回复的频道数据.
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);
}
捕获位置
您可以在此处捕获用户位置以使用所需的位置.
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);
}
希望这会有所帮助!
这篇关于Botframework V4:Messenger位置,电话和电子邮件快速回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!