您好我在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将是用户提供的号码,我将其保存在会话的上下文中以便稍后在查询中使用它,我该如何实现这种行为?
答案 0 :(得分: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);
要达到要求:提示用户在第一条消息中输入数字并将其存储在对话中,您可以尝试以下示例作为解决方法。
在消息控制器中:
//...
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);
}
}
测试结果: