某些 Teams 系统消息上 Teams 机器人的 onTurn 错误

onTurn error for Teams bot on certain Teams system messages

我有一个部署到 Microsoft Teams 的机器人(基于旧的 core-bot-sample)。有时,当 Teams 中出现新频道添加或频道删除(或可能的用户 Added/Removed)等通知事件时,我会收到以下 onTurn 错误:"error":"Cannot read property 'length' of undefined"。通过查看代码,欢迎消息代码似乎是罪魁祸首。唯一的长度 属性 在 context.activity.membersAdded 上,所以这一定是导致问题的原因。但我不明白到底发生了什么。根据下面的陈述,事件必须触发 ConversationUpdate activity,但没有 membersAdded 属性。任何人都可以阐明 Teams 触发的 activity 是什么,以及我应该添加到此欢迎消息声明中以防止出现错误消息的内容吗?澄清一下,错误消息来自 Team/Channel 的帖子频道,频道删除消息等事件即将到来。

我认为发生错误的代码部分:

        } else if (context.activity.type === ActivityTypes.ConversationUpdate) {
            // Handle ConversationUpdate activity type, which is used to indicates new members add to
            // the conversation.
            // see https://aka.ms/about-bot-activity-message to learn more about the message and other activity types

            // Do we have any new members added to the conversation?
            if (context.activity.membersAdded.length !== 0) {

                // Iterate over all new members added to the conversation
                for (var idx in context.activity.membersAdded) {
                    // Greet anyone that was not the target (recipient) of this message
                    // the 'bot' is the recipient for events from the channel,
                    // context.activity.membersAdded == context.activity.recipient.Id indicates the
                    // bot was added to the conversation.
                    if (context.activity.membersAdded[idx].id === context.activity.recipient.id) {
                        // Welcome user.
                        await context.sendActivity('Hi! I\'m the IT Innovation Bot. I can answer questions about the innovation team and capture your innovation ideas. Let me know how I can help!')
                    }
                }
            }
        }

对于类型为 ConversationUpdate 的任何 activity,这似乎都会失败,但 JSON 负载不包含 membersAdded 对象。这些事件的列表可以在这里找到:

https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/subscribe-to-conversation-events?tabs=json

您可以通过触发一个非 membersAdded 事件(例如,向团队添加新频道或删除成员)来对此进行测试。您可能可以通过对 membersAdded 对象进行空检查来解决此问题。