You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C#在Visual Studio与Bot Emulator中实现首次消息数字输入存储

嘿,我来帮你搞定这个需求!要实现首次对话提示用户输入数字并存储到对话中,咱们分两步来操作:

第一步:在用户首次加入对话时发送提示消息

你已经在MessagesController里写了ConversationUpdate的分支,现在需要补全逻辑——当用户第一次进入对话时(排除机器人自己),主动发送欢迎并提示输入数字的消息。补全后的代码如下:

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)
    {
        // 初始化ConnectorClient用来发送消息
        var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
        
        if (activity.MembersAdded != null)
        {
            foreach (var member in activity.MembersAdded)
            {
                // 只对新加入的用户发送消息,排除机器人自身
                if (member.Id != activity.Recipient.Id)
                {
                    var reply = activity.CreateReply("欢迎!请输入一个数字,我会帮你把它存储起来~");
                    await connector.Conversations.ReplyToActivityAsync(reply);
                }
            }
        }
    }
    // 返回成功响应
    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}
第二步:处理用户输入并存储数字

接下来在你的LuisHandler对话框里,需要判断用户输入是否是有效数字,然后存储到对话状态中。这里提供两种实用方式:

方式1:直接解析用户输入(结合LUIS)

如果用户输入的内容刚好是数字,我们直接解析并存储;如果不是,就提示重新输入:

[Serializable]
public class LuisHandler : LuisDialog<object>
{
    public LuisHandler() : base(new LuisService(new LuisModelAttribute("你的LUIS应用ID", "你的LUIS订阅密钥")))
    {
    }

    // 处理未匹配到特定Intent的情况(比如用户直接输入数字)
    [LuisIntent("")]
    [LuisIntent("None")]
    public async Task HandleNumberInput(IDialogContext context, LuisResult result)
    {
        // 尝试将用户输入解析为整数
        if (int.TryParse(result.Query.Trim(), out int userNumber))
        {
            // 存储到用户对话数据中(UserData会绑定当前用户,跨对话保留)
            context.UserData.SetValue("StoredNumber", userNumber);
            await context.PostAsync($"搞定啦!我已经把数字 {userNumber} 存起来了😉");
        }
        else
        {
            await context.PostAsync("不好意思,我没认出这是有效的数字,请重新输入一个数字哦~");
        }
    }
}

方式2:用PromptDialog强制引导输入有效数字

如果想确保用户输入的一定是数字,可以用PromptDialog来发起输入提示,直到用户输入有效内容为止:

[Serializable]
public class LuisHandler : LuisDialog<object>
{
    public LuisHandler() : base(new LuisService(new LuisModelAttribute("你的LUIS应用ID", "你的LUIS订阅密钥")))
    {
    }

    [LuisIntent("")]
    [LuisIntent("None")]
    public async Task PromptForNumber(IDialogContext context, LuisResult result)
    {
        // 发起数字输入提示,指定回调方法处理结果
        PromptDialog.Number(context, ResumeAfterNumberInput, "请输入一个有效的数字:", "哎呀,这不是有效的数字哦,请重新输入~");
    }

    // 处理用户输入后的回调方法
    private async Task ResumeAfterNumberInput(IDialogContext context, IAwaitable<int> result)
    {
        try
        {
            var userNumber = await result;
            // 存储数字到对话数据中
            context.UserData.SetValue("StoredNumber", userNumber);
            await context.PostAsync($"完美!数字 {userNumber} 已经成功存储啦🥳");
        }
        catch (TooManyAttemptsException)
        {
            await context.PostAsync("尝试次数太多啦,咱们稍后再试吧~");
        }
    }
}

关于存储的小说明

  • context.UserData:绑定当前用户,即使用户开启新对话,数据依然保留
  • context.ConversationData:绑定当前对话,对话结束后数据会被清除
  • context.PrivateConversationData:绑定用户和机器人的私有对话,适合多用户场景

你可以根据自己的需求选择合适的存储方式~

内容的提问来源于stack exchange,提问作者IvanHid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:53:10