Bot Framework Context.Wait不等待输入

时间:2018-10-24 17:41:51

标签: .net botframework bots luis

我有三个对话框,如下所述

我有一个“收据对话框”,但是当调用该对话框时,它首先进入“入职”对话框,以检查用户是否已入职,如果用户必须入职,则启动入职过程,然后调用“ OTP对话框”以完成交易。但是,当进入“ OTP对话框”时,它不会等待用户输入生成的令牌。

下面是代码

登机对话框

    private async Task AccountNumberEnteredAsync(IDialogContext context, IAwaitable<object> result)
    {
        try
        {
            if (await result is IMessageActivity resp)
            {
                var userID = context.Activity.From.Id;
                var channelID = context.Activity.ChannelId;

                var customerEnquiry = DialogFactory.Create<ICustomerEnquiry>();
                string phoneNum = await customerEnquiry.GetCustomerPhoneNumberAsync(resp.Text);

                context.PrivateConversationData.SetValue(StringConstants.TEMP_ACCOUNT_NUMBER, resp.Text);
                context.PrivateConversationData.SetValue(StringConstants.PHONE_NUMBER, phoneNum);
                context.Call(DialogFactory.Create<OTPDialog>(), ResumeAfterOTPAsync);
            }
        }
        catch (Exception ex)
        {
            Telemetry.TrackException(ex);
            await context.PostAsync(L("FORM_ERROR"));
            context.Done(false);
        }
    }

OTP对话框

    private async Task InitiateOTP(IDialogContext context)
    {
        try
        {
            if (!context.UserData.TryGetValue(StringConstants.USER_OTP_KEY, out this.secret))
            {
                var username = context.UserData.GetValueOrDefault<string>(StringConstants.ACCESS_MOBILE_USERNAME);
                context.Fail(new AppException(L("OTPGENERATIONERROR", new object[] { username })));
            }
            else
            {
                context.PrivateConversationData.TryGetValue(StringConstants.PHONE_NUMBER, out string phoneNum);
                var otp = await TwoFactorAuth.GetOTPAsync(this.secret);
                await SendTokenSMSAsync(otp, new List<string>() { phoneNum });
                await context.PostAsync(L("OTP_SENT_MESSAGE") + " " + otp);
                context.Wait(this.OTPEnteredAsync);
            }
        }
        catch (TooManyAttemptsException ex)
        {
            Telemetry.TrackException(ex);
            await context.PostAsync(L("HERO_CARD_TRIALS_EXCEEDED"));
            context.Done(false);
        }
        catch (InvalidOperationException ex)
        {
            Telemetry.TrackException(ex);
            await context.PostAsync(L("FORM_ERROR"));
            context.Done(false);
        }
        finally
        {
            Telemetry.TrackTrace(nameof(StartAsync));
        }
    }

在输入OTP之后恢复(但它不会等待用户输入OTP)

     private async Task OTPEnteredAsync(IDialogContext context, IAwaitable<object> result)
    {
        try
        {
            var response = await result as Activity;
            var yesy = await result;

            if (response != null && !string.IsNullOrEmpty(response.Text) && response.Text.Equals(RESEND, StringComparison.InvariantCultureIgnoreCase))
            {
                context.PrivateConversationData.TryGetValue(StringConstants.PHONE_NUMBER, out string phoneNum);
                var otp = await TwoFactorAuth.GetOTPAsync(this.secret);
                await SendTokenSMSAsync(otp, new List<string>() { phoneNum });
                await context.PostAsync(L("OTP_SENT_MESSAGE"));
                context.Wait(OTPEnteredAsync);
            }
            else
            {
                if (response != null &&  !response.Text.IsNullOrEmpty())
                {
                    var isValidated = await TwoFactorAuth.ValidateOTPAsync(this.secret, response.Text);

                    if (!isValidated && authTrialCount++ < int.Parse(await SettingManager.GetSettingValueAsync(BotEngineConsts.SettingNames.AuthTrialLimit)))
                    {
                        await context.PostAsync(L("OTP_INVALID"));
                        context.Wait(OTPEnteredAsync);
                    }
                    else
                    {
                        context.Done(isValidated);
                    } 
                }
                else
                {
                    context.Wait(OTPEnteredAsync);
                }
            }
        }
        catch (Exception ex)
        {

            throw;
        }

        context.Wait(OTPEnteredAsync);
    }

如何使它等待用户输入OTP才能继续。我正在利用Autofac进行DI

0 个答案:

没有答案