Search code examples
botframeworkmicrosoft-teams

onTurn error for Teams bot on certain Teams system messages


I have a bot (based on the old core-bot-sample) that is deployed to Microsoft Teams. Sometimes, when there is a notification event in Teams such as New Channel Added or Channel Deleted (or possible User Added/Removed), I am getting the following onTurn Error: "error":"Cannot read property 'length' of undefined". By looking at the code, it seems the Welcome Message code is to blame. The only length property is on context.activity.membersAdded, so that must be causing the issue. But I don't understand exactly what is happening. Based on the statement below, the event must be triggering a ConversationUpdate activity, but without the membersAdded property. Can anyone shed some light on what this activity is that Teams is triggering, and what I should add to this welcome message statement to prevent the error message from occuring? To clarify, the error message is coming in the Posts channel of the Team/Channel where the event such as channel removal message is coming.

Code section where I think the error is occurring:

        } 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!')
                    }
                }
            }
        }

Solution

  • It looks like that would fail for any activity where the type was ConversationUpdate, but the JSON payload doesn't contain the membersAdded object. The list of those events can be found here:

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

    You could test this by firing one of the non-membersAdded events (for example, add a new channel to the team, or remove a member). You could probably fix this by doing a null check on the membersAdded object.