context.Wait,消息仍然传递到端点而不是等待的方法
context.Wait, messages are are still delivered to endpoint rather than the method to wait on
下面是我的对话框。
[Serializable]
public class EmailDialog : IDialog<object>
{
async Task IDialog<object>.StartAsync(IDialogContext context)
{
context?.Wait(RequestEmailAddress);
}
private async Task RequestEmailAddress(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
context.Wait(RequestEmailAddress);
var thumbNailCard = new ThumbnailCard
{
Title = "BotFramework Thumbnail Card",
Subtitle = "Your bots — wherever your users are talking",
Text = "Build and connect intelligent bots to interact with your users naturally wherever they are, from text/sms to Skype, Slack, Office 365 mail and other popular services.",
Images = new List<CardImage> { new CardImage("https://sec.ch9.ms/ch9/7ff5/e07cfef0-aa3b-40bb-9baa-7c9ef8ff7ff5/buildreactionbotframework_960.jpg") },
Buttons = new List<CardAction> { new CardAction(ActionTypes.OpenUrl, "Get Started", value: "https://docs.botframework.com/en-us/") }
};
var resultMessage = context.MakeMessage();
resultMessage.AttachmentLayout = AttachmentLayoutTypes.Carousel;
resultMessage.Attachments = new List<Attachment> { thumbNailCard.ToAttachment() };
await context.PostAsync(resultMessage);
}
}
发送卡片后,任何用户输入都会到达 REST API 端点并且根本不会调用方法 RequestEmailAddress。
注意:最初我忘记添加 context.Wait(RequestEmailAddress); 并且只是在几次 运行 之后才这样做。难道是机器人已经运行进入不知道上下文的死循环了?
我什至尝试清理堆栈并清理机器人状态。没有任何帮助。
编辑:添加消息控制器部分
private async Task<Activity> OnHandleActivityType(Activity activity)
{
switch (activity.Type)
{
case ActivityTypes.ContactRelationUpdate:
OnContactRelationUpdate(activity);
break;
case ActivityTypes.ConversationUpdate:
OnConversationUpdate(activity);
break;
case ActivityTypes.DeleteUserData:
break;
case ActivityTypes.EndOfConversation:
break;
case ActivityTypes.Event:
break;
case ActivityTypes.Invoke:
break;
case ActivityTypes.Message:
OnMessageReceived(activity);
break;
case ActivityTypes.Ping:
break;
case ActivityTypes.Typing:
break;
default:
throw new NotImplementedException();
}
return null;
}
private async void OnMessageReceived(Activity activity)
{
var message = activity.Text;
if (message == AppConstants.RegisterEmail))
{
await Conversation.SendAsync(activity, () => new EmailDialog());
}
}
context.Wait(RequestEmailAddress);
调用应该在 RequestEmailAddress
方法的末尾。
private async Task RequestEmailAddress(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
var thumbNailCard = new ThumbnailCard
{
Title = "BotFramework Thumbnail Card",
Subtitle = "Your bots — wherever your users are talking",
Text = "Build and connect intelligent bots to interact with your users naturally wherever they are, from text/sms to Skype, Slack, Office 365 mail and other popular services.",
Images = new List<CardImage> { new CardImage("https://sec.ch9.ms/ch9/7ff5/e07cfef0-aa3b-40bb-9baa-7c9ef8ff7ff5/buildreactionbotframework_960.jpg") },
Buttons = new List<CardAction> { new CardAction(ActionTypes.OpenUrl, "Get Started", value: "https://docs.botframework.com/en-us/") }
};
var resultMessage = context.MakeMessage();
resultMessage.AttachmentLayout = AttachmentLayoutTypes.Carousel;
resultMessage.Attachments = new List<Attachment> { thumbNailCard.ToAttachment() };
await context.PostAsync(resultMessage);
context.Wait(RequestEmailAddress);
}
此外,传入的消息将始终到达您的端点(因为它是入口点)。然后它们将被传送到对话框。这意味着您需要更新控制器上的 OnMessageReceived
方法并删除 if
消息文本匹配检查,否则用户消息将不会传递到任何地方:
private async void OnMessageReceived(Activity activity)
{
await Conversation.SendAsync(activity, () => new EmailDialog());
}
下面是我的对话框。
[Serializable]
public class EmailDialog : IDialog<object>
{
async Task IDialog<object>.StartAsync(IDialogContext context)
{
context?.Wait(RequestEmailAddress);
}
private async Task RequestEmailAddress(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
context.Wait(RequestEmailAddress);
var thumbNailCard = new ThumbnailCard
{
Title = "BotFramework Thumbnail Card",
Subtitle = "Your bots — wherever your users are talking",
Text = "Build and connect intelligent bots to interact with your users naturally wherever they are, from text/sms to Skype, Slack, Office 365 mail and other popular services.",
Images = new List<CardImage> { new CardImage("https://sec.ch9.ms/ch9/7ff5/e07cfef0-aa3b-40bb-9baa-7c9ef8ff7ff5/buildreactionbotframework_960.jpg") },
Buttons = new List<CardAction> { new CardAction(ActionTypes.OpenUrl, "Get Started", value: "https://docs.botframework.com/en-us/") }
};
var resultMessage = context.MakeMessage();
resultMessage.AttachmentLayout = AttachmentLayoutTypes.Carousel;
resultMessage.Attachments = new List<Attachment> { thumbNailCard.ToAttachment() };
await context.PostAsync(resultMessage);
}
}
发送卡片后,任何用户输入都会到达 REST API 端点并且根本不会调用方法 RequestEmailAddress。
注意:最初我忘记添加 context.Wait(RequestEmailAddress); 并且只是在几次 运行 之后才这样做。难道是机器人已经运行进入不知道上下文的死循环了?
我什至尝试清理堆栈并清理机器人状态。没有任何帮助。
编辑:添加消息控制器部分
private async Task<Activity> OnHandleActivityType(Activity activity)
{
switch (activity.Type)
{
case ActivityTypes.ContactRelationUpdate:
OnContactRelationUpdate(activity);
break;
case ActivityTypes.ConversationUpdate:
OnConversationUpdate(activity);
break;
case ActivityTypes.DeleteUserData:
break;
case ActivityTypes.EndOfConversation:
break;
case ActivityTypes.Event:
break;
case ActivityTypes.Invoke:
break;
case ActivityTypes.Message:
OnMessageReceived(activity);
break;
case ActivityTypes.Ping:
break;
case ActivityTypes.Typing:
break;
default:
throw new NotImplementedException();
}
return null;
}
private async void OnMessageReceived(Activity activity)
{
var message = activity.Text;
if (message == AppConstants.RegisterEmail))
{
await Conversation.SendAsync(activity, () => new EmailDialog());
}
}
context.Wait(RequestEmailAddress);
调用应该在 RequestEmailAddress
方法的末尾。
private async Task RequestEmailAddress(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
var thumbNailCard = new ThumbnailCard
{
Title = "BotFramework Thumbnail Card",
Subtitle = "Your bots — wherever your users are talking",
Text = "Build and connect intelligent bots to interact with your users naturally wherever they are, from text/sms to Skype, Slack, Office 365 mail and other popular services.",
Images = new List<CardImage> { new CardImage("https://sec.ch9.ms/ch9/7ff5/e07cfef0-aa3b-40bb-9baa-7c9ef8ff7ff5/buildreactionbotframework_960.jpg") },
Buttons = new List<CardAction> { new CardAction(ActionTypes.OpenUrl, "Get Started", value: "https://docs.botframework.com/en-us/") }
};
var resultMessage = context.MakeMessage();
resultMessage.AttachmentLayout = AttachmentLayoutTypes.Carousel;
resultMessage.Attachments = new List<Attachment> { thumbNailCard.ToAttachment() };
await context.PostAsync(resultMessage);
context.Wait(RequestEmailAddress);
}
此外,传入的消息将始终到达您的端点(因为它是入口点)。然后它们将被传送到对话框。这意味着您需要更新控制器上的 OnMessageReceived
方法并删除 if
消息文本匹配检查,否则用户消息将不会传递到任何地方:
private async void OnMessageReceived(Activity activity)
{
await Conversation.SendAsync(activity, () => new EmailDialog());
}