Skip to content

Passing in missing arguments to TeamsActivityHandler method hooks#450

Merged
rodrigobr-msft merged 8 commits into
mainfrom
users/robrandao/teams-ah
Jul 10, 2026
Merged

Passing in missing arguments to TeamsActivityHandler method hooks#450
rodrigobr-msft merged 8 commits into
mainfrom
users/robrandao/teams-ah

Conversation

@rodrigobr-msft

Copy link
Copy Markdown
Contributor

This pull request introduces several improvements to the Teams activity handling logic, focusing on stricter type validation, enhanced event dispatching, and improved handling of Teams-specific activities. The most significant changes involve validating incoming payloads with Pydantic models, refactoring event and member dispatch methods for clarity and correctness, and expanding the TeamsChannelAccount model to support more fields.

Type validation and payload handling improvements:

  • Updated the handling of Teams invoke activities (such as file consent, actionable messages, messaging extension actions, tab fetch/submit, etc.) to validate incoming payloads using the appropriate Pydantic models before passing them to handler methods. This ensures type safety and clearer contracts for handler methods. [1] [2] [3]
  • Enhanced event activity handling to parse and validate Teams event payloads (like read receipts and meeting events) using specific Pydantic models before invoking their handlers.

Teams member and conversation update handling:

  • Refactored the conversation update activity logic to consistently use validated models, and updated the dispatch methods for member added/removed events to call both generic and Teams-specific handlers in the correct order. [1] [2] [3] [4] [5]
  • Added a static method _get_list_team_members to convert a list of ChannelAccount objects to TeamsChannelAccount objects, centralizing and simplifying this conversion logic.

Model enhancements:

  • Expanded the TeamsChannelAccount model to include aad_object_id and role fields, allowing for richer representation of Teams user accounts.

Minor code improvements:

  • Updated the _create_invoke_response method signature for better type hinting and optional argument handling.
  • Cleaned up unused imports in activity_handler.py.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR tightens the Microsoft Teams activity handling layer by validating incoming Teams invoke/event payloads into specific Pydantic models before dispatching to handler hooks, and by adjusting Teams member/conversation update dispatch so handler hooks receive the intended arguments consistently.

Changes:

  • Validate various Teams invoke payloads (file consent, O365 connector actions, link queries, messaging extension actions/queries, tab fetch/submit, etc.) with Pydantic models before calling hook methods.
  • Refactor Teams conversation update handling to pass additional context (e.g., channel_info / team_info) into Teams-specific hooks, and ensure generic member-added/removed hooks are invoked from the dispatchers.
  • Expand TeamsChannelAccount to include aad_object_id and role, and refine invoke response typing in the core activity handler.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
libraries/microsoft-agents-hosting-teams/microsoft_agents/hosting/teams/teams_activity_handler.py Adds Pydantic validation for Teams invoke/event payloads and refactors Teams dispatch/hook signatures.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/activity_handler.py Tweaks _create_invoke_response typing and removes an unused import.
libraries/microsoft-agents-activity/microsoft_agents/activity/teams/teams_channel_account.py Adds aad_object_id and role fields to the Teams channel account model.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings July 8, 2026 15:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

libraries/microsoft-agents-hosting-teams/microsoft_agents/hosting/teams/teams_activity_handler.py:176

  • model_validate(...) calls above can raise Pydantic ValidationError for malformed/empty invoke payloads, but the current except block only maps custom Teams exceptions. That means a bad client payload can surface as an unhandled exception (likely 500) instead of returning a 400.

Consider mapping Pydantic validation errors to HTTPStatus.BAD_REQUEST here.

        except Exception as err:
            if str(err) == str(teams_errors.TeamsNotImplemented):
                return InvokeResponse(status=int(HTTPStatus.NOT_IMPLEMENTED))
            elif str(err) == str(teams_errors.TeamsBadRequest):
                return InvokeResponse(status=int(HTTPStatus.BAD_REQUEST))

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 8, 2026 19:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

Copilot AI review requested due to automatic review settings July 9, 2026 18:48
@rodrigobr-msft
rodrigobr-msft enabled auto-merge (squash) July 9, 2026 18:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

Copilot AI review requested due to automatic review settings July 10, 2026 17:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

Copilot AI review requested due to automatic review settings July 10, 2026 17:31
@rodrigobr-msft
rodrigobr-msft merged commit b3edfc5 into main Jul 10, 2026
10 of 11 checks passed
@rodrigobr-msft
rodrigobr-msft deleted the users/robrandao/teams-ah branch July 10, 2026 17:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

Comment on lines 509 to 512
if turn_context.activity.channel_id == "msteams":
channel_data = (
TeamsChannelData.model_validate(turn_context.activity.channel_data)
if turn_context.activity.channel_data
else None
channel_data = TeamsChannelData.model_validate(
turn_context.activity.channel_data
)
Comment on lines 76 to +83
elif name == "fileConsent/invoke":
card_response = FileConsentCardResponse.model_validate(value)
return self._create_invoke_response(
await self.on_teams_file_consent(turn_context, value)
await self.on_teams_file_consent(turn_context, card_response)
)
elif name == "actionableMessage/executeAction":
await self.on_teams_o365_connector_card_action(turn_context, value)
query = O365ConnectorCardActionQuery.model_validate(value)
await self.on_teams_o365_connector_card_action(turn_context, query)
Comment on lines 110 to 126
elif name == "composeExtension/submitAction":
action = MessagingExtensionAction.model_validate(value)
return self._create_invoke_response(
await self.on_teams_messaging_extension_submit_action_dispatch(
turn_context, value
turn_context, action
)
)
elif name == "composeExtension/fetchTask":
action = MessagingExtensionAction.model_validate(value)
return self._create_invoke_response(
await self.on_teams_messaging_extension_fetch_task(
turn_context, value
turn_context, action
)
)
elif name == "composeExtension/querySettingUrl":
query = MessagingExtensionQuery.model_validate(value)
return self._create_invoke_response(
Comment on lines 155 to 164
elif name == "tab/fetch":
tab_request = TabRequest.model_validate(value)
return self._create_invoke_response(
await self.on_teams_tab_fetch(turn_context, value)
await self.on_teams_tab_fetch(turn_context, tab_request)
)
elif name == "tab/submit":
tab_submit = TabSubmit.model_validate(value)
return self._create_invoke_response(
await self.on_teams_tab_submit(turn_context, value)
await self.on_teams_tab_submit(turn_context, tab_submit)
)
Comment on lines 884 to +889
if turn_context.activity.channel_id == "msteams":
if turn_context.activity.name == "application/vnd.microsoft.readReceipt":
return await self.on_teams_read_receipt(turn_context)
return await self.on_teams_read_receipt(
ReadReceiptInfo.model_validate(turn_context.activity.value),
turn_context,
)
Copilot AI review requested due to automatic review settings July 10, 2026 17:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment on lines 509 to 513
if turn_context.activity.channel_id == "msteams":
channel_data = (
TeamsChannelData.model_validate(turn_context.activity.channel_data)
if turn_context.activity.channel_data
else None
channel_data = TeamsChannelData.model_validate(
turn_context.activity.channel_data
)

Comment on lines 76 to 80
elif name == "fileConsent/invoke":
card_response = FileConsentCardResponse.model_validate(value)
return self._create_invoke_response(
await self.on_teams_file_consent(turn_context, value)
await self.on_teams_file_consent(turn_context, card_response)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] TeamsActivityHandler channel/team event dispatchers call handlers with wrong number of arguments (TypeError on channelCreated and related events)

3 participants