context.Wait,消息仍然传递给端点而不是等待的方法

时间:2017-03-17 17:25:04

标签: c# botframework

以下是我的对话。

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

1 个答案:

答案 0 :(得分:0)

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