gpt4 book ai didi

c# - 使用第一条消息的提示保存用户提供的号码?

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:04 24 4
gpt4 key购买 nike

您好,我在 Visual Studio 中使用 C# 和 Microsoft 的机器人模拟器,我想知道如何提示用户在第一条消息中输入数字并将其存储在对话中。到目前为止,这是我的 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 将是用户提供的数字,我将其保存在对话的上下文中以便稍后在查询中使用它,我如何才能实现这种行为?

最佳答案

how can I prompt the user for a number input in the first message and store it in the conversation.

PromptDialog.Number() 方法接受 IDialogContext 对象作为参数,因此我们不能直接从 Messages Controller (外部对话框)提示输入一个数字。

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

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

在消息 Controller 中:

//...

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

关于c# - 使用第一条消息的提示保存用户提供的号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49837007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com