【发布时间】:2017-03-09 14:38:06
【问题描述】:
我是 Azure Bot 编程 (C#) 的新手,找不到关于此主题的好文章。
我想让用户使用机器人“订阅”邮件列表。我构建了一个表单对话框和表单流。它要求用户提供他们的电子邮件地址。
我需要做的是将其发布到外部 WebAPI (json) 并获得响应并处理响应。
谁能给我一些关于如何从 Bot 调用 WebAPI 的指示?
public async Task Subscribe(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
{
context.Call(SubscribeForm.BuildFormDialog(FormOptions.PromptInStart), SubscribeFormComplete);
}
private async Task SubscribeFormComplete(IDialogContext context, IAwaitable<SubscribeForm> result)
{
try
{
var form = await result;
if (form != null)
{
await context.PostAsync("Thanks for subscribing! You can always remove yourself by typing unsubscribe.");
}
else
{
await context.PostAsync("Form returned empty response!");
}
}
catch (OperationCanceledException)
{
await context.PostAsync("I am sorry you decided not to subscribe! If you change your mind just type 'subscribe' again.");
}
context.Wait(this.MessageReceived);
}
[Serializable]
public class SubscribeForm
{
[Prompt("What is your email address?")]
[Required()]
[DataType(DataType.EmailAddress)]
public string EmailAddress { get; set; }
public static IForm<SubscribeForm> BuildForm()
{
// Builds an IForm<T> based on BasicForm
return new FormBuilder<SubscribeForm>()
.Message("We often send out updates on new features. We don't spam. You can type 'quit' to cancel this.")
.Build();
}
public static IFormDialog<SubscribeForm> BuildFormDialog(FormOptions options = FormOptions.PromptInStart)
{
// Generated a new FormDialog<T> based on IForm<BasicForm>
return FormDialog.FromForm(BuildForm, options);
}
}
【问题讨论】:
-
为什么从机器人完成时会有所不同?您可以像往常一样提出请求。
-
机器人和表单流对它们有“堆栈”和父/子细微差别。只是寻找有关在何处放置 WebAPI 调用代码的建议。比如在formcompleted方法或者其他地方,以及如何处理异步和回调。
标签: c# azure asp.net-web-api bots botframework