从第一条消息中提示保存用户提供的号码?

问题描述 投票:2回答:1

嗨我在Visual Studio中使用C#和Microsoft的bot模拟器,我想知道如何在第一条消息中提示用户输入数字并将其存储在对话中。这是我在MessagesController中到目前为止的内容:

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        if (activity.Type == ActivityTypes.Message)
        {
            await Conversation.SendAsync(activity, () => new Dialogs.LuisHandler());
        }
        else
        if (activity.Type == ActivityTypes.ConversationUpdate)
        {
            if (activity.MembersAdded.Any(o => o.Id == activity.Recipient.Id))
            {
                PromptDialog.Number(activity,setUserID,"Please enter your number code","Error please enter the number again",3,"",0,999);
            }
        }
        else
        {
            HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

这显然不是正确的方法,因为activity和setUserId属性都显示错误,我该如何解决这个问题?

另外,这是我的SetUserId方法:

public void setUserID(int id, Activity act)
    {
        IDialogContext cxt = act;
        cxt.UserData.SetValue("userId", id);
    }

其中id将是用户提供的数字,我将其保存在会话的上下文中,以便稍后在查询中使用它,我该如何实现这种行为?

c# botframework
1个回答
0
投票

如何在第一条消息中提示用户输入数字并将其存储在对话中。

PromptDialog.Number()方法接受IDialogContext对象作为参数,因此我们不能直接从Messages控制器(在Dialog之外)调用它来提示输入数字。

public static void Number(IDialogContext context, ResumeAfter<long> resume, string prompt, string retry = null, int attempts = 3, string speak = null, long? min = null, long? max = null);

要实现此要求:提示用户输入第一条消息中的数字并将其存储在对话中,您可以尝试以下示例作为解决方法。

在Messages控制器中:

//...

else if (message.Type == ActivityTypes.ConversationUpdate)
{
    // Handle conversation state changes, like members being added and removed
    // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
    // Not available in all channels

    IConversationUpdateActivity update = message;
    var client = new ConnectorClient(new System.Uri(message.ServiceUrl), new MicrosoftAppCredentials());
    if (update.MembersAdded != null && update.MembersAdded.Any())
    {
        foreach (var newMember in update.MembersAdded)
        {
            if (newMember.Id != message.Recipient.Id)
            {
                var reply = message.CreateReply();
                reply.Text = $"Welcome {newMember.Name}! Please enter your number code!";
                client.Conversations.ReplyToActivityAsync(reply);

            }
        }
    }

}

在对话框中:

[Serializable]
public class RootDialog : IDialog<object>
{
bool thenumberisentered = false;

    public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;

        if (!thenumberisentered)
        {
            string pattern = @"^(?:0|[1-9]\d{0,2})$";
            System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(pattern);

            if (rgx.IsMatch(activity.Text))
            {
                //save the number code as user data here 

                thenumberisentered = true;

                await context.PostAsync($"The number code you entered is: {activity.Text}");
            }
            else
            {
                PromptDialog.Number(context, setUserID, "Please enter your number code", "Error please enter the number again", 2, "", 0, 999);
                return;
            }

        }
        else
        {
            await context.PostAsync($"You sent {activity.Text}");
        }

        context.Wait(MessageReceivedAsync);
    }

    private async Task setUserID(IDialogContext context, IAwaitable<long> result)
    {
        long numcode = await result;

        await context.PostAsync($"The number code you entered is: {numcode}");

        thenumberisentered = true;

        //save the number code as user data here 

        context.Wait(MessageReceivedAsync);
    }

}

测试结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.