从Teams接收信息的方法导致后续方法崩溃

3
我有一个从MS Teams收集用户数据的方法,虽然此代码按预期工作,但添加了此方法后,先前正常工作的方法崩溃了。
public async void GetUsers()
{
    string teamId = "TeamsID";
    string tenantId = "TenantID";
    var connector = new ConnectorClient(new Uri(Instance.Activity.ServiceUrl));
    members = await connector.Conversations.GetTeamsConversationMembersAsync(teamId, tenantId);
    Instance.EmailList.Clear();
    foreach (var member in members)
    {
        Instance.EmailList.Add(member.Email);
    }

}

我相信这一行代码:

members = await connector.Conversations.GetTeamsConversationMembersAsync(teamId, tenantId);

在接收用户信息时,使机器人认为有用户在输入,这会导致后续方法在没有用户输入的情况下触发,从而崩溃或因没有输入或输入是用户数据块而导致崩溃。

这只是我的理论,我可能是错误的。 以下是导致崩溃的方法:

async Task ReplyToQuestions(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
    var AnswerHolder = await argument;
    Answers.Add(AnswerHolder.Text);
    Answers[AnswerCounter] = Answers[AnswerCounter].First().ToString().ToUpper() + Answers[AnswerCounter].Substring(1);

    var isQuit = AnswerHolder.Text;
    var isQuit2 = Regex.Match(isQuit, @"\b(Quit|quit|QUIT)\b").Value;
    Regex rgx = new Regex(@"\b(Quit|quit|QUIT)\b");

    if (rgx.IsMatch(isQuit2)) // checks for the user trying to quit/restart bot
    {
        await context.PostAsync(string.Format("Exiting current process. Restarting."));
        context.Done(isQuit); // returns to the start of dialog (StartAsync)
    }
    else
    {
        if (QuestionCounter < 5)
        {
            await context.PostAsync(string.Format($"Question {QuestionCounter + 1}: {Question[QuestionCounter]}"));
        }
        AnswerCounter += 1;
        QuestionCounter += 1;
        if (AnswerCounter < 5)
        {
            context.Wait(ReplyToQuestions);
        }
        else if (AnswerCounter == 5)
        {
            PostToChannel($"{Name}'s answers to the questions are as follows: \n\nQuestion1 Answer: {Answers[0]} \n\nQuestion2 Answer: {Answers[1]} \n\n" +
            $"Question3 Answer: {Answers[2]} \n\nQuestion4 Answer: {Answers[3]} \n\nQuestion5 Answer: {Answers[4]} \n\n", context);

            await context.PostAsync(string.Format("Your answers have been posted to the 'What's Up' channel."));
            AnswerCounter = 0;
            QuestionCounter = 1;
            context.Done(Answers[4]);
        }
        else
        {
            await context.PostAsync($"{AnswerCounter}");
            await context.PostAsync(string.Format("Oh no! it appears something has gone wrong. please try re-entering your answers"));
            AnswerCounter = 0;
            QuestionCounter = 1;
            context.Wait(ReplyToQuestions);
        }
    }
}

调用它的代码:

 async Task Choice(IDialogContext context, IAwaitable<IMessageActivity> argument) // this method recives and validates a question
        {
            var Choice = await argument;

            var isQuit = Choice.Text;
            var isQuit2 = Regex.Match(isQuit, @"\b(Quit|quit|QUIT)\b").Value;
            Regex rgx = new Regex(@"\b(Quit|quit|QUIT)\b");
            var isEnter = Regex.Match(isQuit, @"\b(Enter|ENTER|enter)\b").Value;
            Regex rgx2 = new Regex(@"\b(Enter|ENTER|enter)\b");
            var isReply = Regex.Match(isQuit, @"\b(Reply|REPLY|reply)\b").Value;
            Regex rgx3 = new Regex(@"\b(Reply|REPLY|reply)\b");

            GetUsers();

            if (rgx.IsMatch(isQuit2)) // if the user choses to quit
            {
                await context.PostAsync(string.Format("Exiting current process. Restarting."));
                context.Done(isQuit); // restarts the program, calls the first method
            }
            else if (rgx2.IsMatch(isEnter)) // if the user choses to quit
            {
                await context.PostAsync(string.Format("Please enter your custom question."));
                context.Wait(EnterQuestion);
            }
            else if (rgx3.IsMatch(isReply)) // if the user choses to quit
            {
                Answers.Clear();
                await context.PostAsync(string.Format("Please answer the following questions:"));
                await context.PostAsync(string.Format($"Question 1: {Question[0]}"));
                context.Wait(ReplyToQuestions);
            }
            else
            {
                await context.PostAsync(string.Format("sorry this was not a choice, try again."));
            }
        }

有人知道我可以如何修复这个问题吗?我已经花费了两天的时间,但仍然没有成功。

由于机器人托管在Azure上,而测试是通过Teams进行的(因为代码是与Teams交互的),所以我无法接收这些内容。 - Zigy
@Zigy,你可以在Azure上使用AppInsights来记录错误和日志跟踪。此外,你应该添加更多的代码,因为我们目前不知道你如何调用GetUsers方法,这可能是你遇到问题的原因,正如你所说的。 - Nicolas R
我已经添加了整个方法,调用了ReplyToQuestions和GetUsers。现在会查看AppInsights以获取错误和日志跟踪,并尽快提供它们。 - Zigy
我已经尝试使用AppInsights并成功设置了错误捕获(我甚至抛出了一个错误来测试它),但是当发生上述问题时,似乎没有触发错误处理,因此我没有收到错误消息,只有一个失败的提交消息。 - Zigy
1个回答

2

我不确定您看到了什么错误。但是,用于检索对话成员的方法已被弃用:https://msdn.microsoft.com/en-us/microsoft-teams/botapis#net-example该页面上的注释应说明:

您应将代码迁移到使用GetConversationMembersAsync(conversationId)

var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
var members = await connector.Conversations.GetConversationMembersAsync(activity.Conversation.Id);

谢谢Eric,我已经看到了这个,但是我似乎无法使用它获取用户的电子邮件,而这正是我需要的。除非有一种我不知道且无法通过我的(缺乏)搜索技能找到的方法来获取电子邮件? - Zigy
member.AsTeamsChannelAccount() 具有扩展属性。 - Eric Dahlvang
谢谢Eric,这帮助我解决了崩溃的问题,但现在我的程序正在使用用户的最后一个输入来回答第一个问题...虽然是不同的问题,但我认为它仍然与程序将团队信息作为用户输入有关。 - Zigy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接