Skip to content

Simplify TargetedMessages with an overload in WithRecipient()#318

Merged
rido-min merged 15 commits into
mainfrom
rido/fix-fluent-TM
Feb 18, 2026
Merged

Simplify TargetedMessages with an overload in WithRecipient()#318
rido-min merged 15 commits into
mainfrom
rido/fix-fluent-TM

Conversation

@rido-min

Copy link
Copy Markdown
Contributor

This pull request refactors how targeted messages are handled in the Teams API SDK, consolidating targeted message logic into the WithRecipient method and removing the older WithTargetedRecipient methods. The changes streamline the API, improve clarity, and update documentation and tests to reflect the new approach.

Targeted message API refactor:

  • The WithTargetedRecipient methods in MessageActivity are removed and replaced with an updated WithRecipient method that accepts an Account and an optional isTargeted parameter. This centralizes targeted message logic and simplifies usage. [1] [2]
  • The IsTargeted property is moved from MessageActivity to the base Activity class and is now a non-nullable bool, ensuring consistent handling across activity types. [1] [2]
  • The WithRecipient method in both Activity and MessageActivity is updated to support the isTargeted flag, replacing previous targeted recipient APIs. [1] [2]

Documentation and sample updates:

  • All code samples and documentation in Samples.TargetedMessages are updated to use the new WithRecipient(..., true) pattern instead of WithTargetedRecipient. [1] [2] [3] [4] [5] [6] [7] [8]

Test suite improvements:

  • Unit tests are refactored to validate the new WithRecipient API for targeted messages, replacing tests for WithTargetedRecipient. New tests confirm fluent chaining and correct property assignment. [1] [2]
  • The test for sending a targeted message without a recipient proactively is removed, as the new API enforces recipient assignment.

Validation and error handling:

  • Error messages are updated to clarify that targeted messages sent proactively must specify an explicit recipient ID.
  • Logic for inferring recipients in reactive contexts is removed, as the new API requires explicit recipient assignment for targeted messages.

Added WithRecipient(Account) method to MessageActivity for fluent recipient assignment. Introduced two unit tests to validate fluent API chaining and recipient property behavior.
Replaces WithTargetedRecipient with WithRecipient(account, isTargeted).
Moves IsTargeted to base Activity as a non-nullable bool.
Updates all code, tests, and docs to use new targeted messaging pattern.
Adds IsRecipientMentioned property to MessageActivity.
Improves proactive targeted message validation and error messaging.
Removes ambiguity and simplifies targeted message creation.
Copilot AI review requested due to automatic review settings February 11, 2026 01:30

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 refactors the targeted-message API in the Teams SDK by consolidating targeted-recipient behavior into WithRecipient(...) and moving IsTargeted to the base Activity, updating samples and tests accordingly.

Changes:

  • Removed MessageActivity.WithTargetedRecipient(...) and added WithRecipient(Account, bool isTargeted = false) to set both Recipient and IsTargeted.
  • Moved IsTargeted from MessageActivity to Activity as a non-nullable bool (still [JsonIgnore]), and updated merge behavior.
  • Updated targeted-message samples and unit tests to use .WithRecipient(..., true).

Reviewed changes

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

Show a summary per file
File Description
Tests/Microsoft.Teams.Apps.Tests/AppTests.cs Updates app-level targeted message validation test to use the new API.
Tests/Microsoft.Teams.Api.Tests/Activities/Message/MessageActivityTests.cs Refactors tests to cover WithRecipient(..., true) and fluent chaining.
Samples/Samples.TargetedMessages/README.md Updates docs/snippets to the new targeted-recipient pattern.
Samples/Samples.TargetedMessages/Program.cs Updates sample code to call WithRecipient(..., true) instead of removed APIs.
Libraries/Microsoft.Teams.Apps/Contexts/Context.Send.cs Removes prior recipient-inference logic in Context.Send.
Libraries/Microsoft.Teams.Apps/App.cs Updates proactive targeted-message validation error message.
Libraries/Microsoft.Teams.Api/Activities/Message/MessageActivity.cs Removes WithTargetedRecipient overloads; introduces WithRecipient(Account, bool).
Libraries/Microsoft.Teams.Api/Activities/Activity.cs Introduces IsTargeted on Activity, updates WithRecipient signature and merge behavior.

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

Comment thread Samples/Samples.TargetedMessages/Program.cs Outdated
Comment thread Samples/Samples.TargetedMessages/README.md Outdated
Comment thread Samples/Samples.TargetedMessages/README.md Outdated
Comment thread Tests/Microsoft.Teams.Apps.Tests/AppTests.cs
Comment thread Tests/Microsoft.Teams.Api.Tests/Activities/Message/MessageActivityTests.cs Outdated
Comment thread Libraries/Microsoft.Teams.Api/Activities/Message/MessageActivity.cs Outdated
Comment thread Libraries/Microsoft.Teams.Api/Activities/Activity.cs Outdated
Comment thread Libraries/Microsoft.Teams.Apps/App.cs Outdated
- Make IsTargeted assignment conditional to prevent overwriting
- Mark MessageActivity.WithRecipient as override for correctness
- Clarify exception message for missing recipient in targeted messages
- Remove unused variable in targeted message example
- Update README with explicit targeted message usage examples
- Add test to ensure exception is thrown if recipient is missing
@rido-min

Copy link
Copy Markdown
Contributor Author

@copilot check the last commit and resolve that conversations that have been fixed

Copilot AI commented Feb 11, 2026

Copy link
Copy Markdown
Contributor

@rido-min I've opened a new pull request, #319, to work on those changes. Once the pull request is ready, I'll request review from you.

…resolved (#319)

All 8 bot review comments on the targeted messaging API refactor have
been resolved. The previous commit addressed 7 issues; this commit fixes
the remaining test naming concern.

**Changes:**
- Renamed test `WithRecipient_DoNotSetTargeted` →
`WithRecipient_DefaultsToNotTargeted` for clarity
- Verified all other review feedback was already addressed:
  - Removed unused variable in sample
  - Updated documentation to reflect explicit recipient passing
  - Fixed hard-coded placeholder to use variable
  - Added validation test for missing recipient
- Changed `MessageActivity.WithRecipient` from `new` to `override` for
correct polymorphism
- Made `IsTargeted` assignment conditional in merge to preserve existing
`true` values
  - Enhanced exception message with actionable guidance

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 We'd love your input! Share your thoughts on Copilot coding agent in
our [2 minute survey](https://gh.io/copilot-coding-agent-survey).

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: rido-min <[email protected]>
Copilot AI review requested due to automatic review settings February 11, 2026 01:54

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 8 out of 8 changed files in this pull request and generated 1 comment.


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

Comment thread Libraries/Microsoft.Teams.Api/Activities/Activity.cs Outdated
Copilot AI review requested due to automatic review settings February 11, 2026 02:04

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 8 out of 8 changed files in this pull request and generated 4 comments.


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

Comment thread Libraries/Microsoft.Teams.Api/Activities/Activity.cs
Comment thread Libraries/Microsoft.Teams.Apps/App.cs
Comment thread Samples/Samples.TargetedMessages/README.md
@rido-min

Copy link
Copy Markdown
Contributor Author

@copilot open a new pull request to apply changes based on the comments in this thread

Copilot AI commented Feb 11, 2026

Copy link
Copy Markdown
Contributor

@rido-min I've opened a new pull request, #320, to work on those changes. Once the pull request is ready, I'll request review from you.

…ds (#320)

Addresses four issues from PR review feedback where the `WithRecipient`
API refactoring introduced a fluent chaining regression, incorrect error
messaging, and outdated documentation.

**Fluent API fix:**
- Added `MessageActivity.WithRecipient(Account)` override returning
`MessageActivity` to preserve fluent chaining when calling the 1-arg
overload
- Without this, `new MessageActivity().WithRecipient(account)` resolved
to base class method and returned `Activity`, breaking chains like
`.WithRecipient(account).AddText(...)`

**Error message correction:**
- Updated exception in `App.cs` from `WithRecipient(recipientId, true)`
to `WithRecipient(new Account { Id = recipientId }, true)` to reflect
actual parameter types

**Documentation updates:**
- Replaced remaining `WithTargetedRecipient` references in README
comparison table with current `WithRecipient` API

**Test coverage:**
- Added `WithRecipient_MaintainsFluentChaining` test that chains
`.AddText()` after `.WithRecipient()` to catch future regressions

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 We'd love your input! Share your thoughts on Copilot coding agent in
our [2 minute survey](https://gh.io/copilot-coding-agent-survey).

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: rido-min <[email protected]>
@rido-min

Copy link
Copy Markdown
Contributor Author

@copilot take another pass and resolve the comments that have been addressed in latest's commits

Copilot AI commented Feb 11, 2026

Copy link
Copy Markdown
Contributor

@rido-min I've opened a new pull request, #321, to work on those changes. Once the pull request is ready, I'll request review from you.

Refactored message handling to retrieve all conversation members and send each a personalized targeted message. Added logging for each member's name and ID during message dispatch.
Copilot AI review requested due to automatic review settings February 11, 2026 15:35

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 8 out of 8 changed files in this pull request and generated 4 comments.


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

Comment thread Libraries/Microsoft.Teams.Api/Activities/Message/MessageActivity.cs Outdated
Comment thread Samples/Samples.TargetedMessages/Program.cs Outdated
Comment thread Tests/Microsoft.Teams.Apps.Tests/AppTests.cs
Comment thread Tests/Microsoft.Teams.Apps.Tests/AppTests.cs Outdated
Copilot AI review requested due to automatic review settings February 11, 2026 15:56

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 8 out of 8 changed files in this pull request and generated 1 comment.


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

Comment thread Samples/Samples.TargetedMessages/Program.cs
singhk97
singhk97 previously approved these changes Feb 12, 2026
Comment thread Samples/Samples.TargetedMessages/Program.cs Outdated
All Teams app handler delegates and context send/reply methods now support CancellationToken parameters for improved async cancellation. This includes new overloads for handler registration (e.g., OnMessage, OnActivity, OnMeetingStart, etc.), context methods (Send, Reply, Typing, SignIn, SignOut), and the Streamer interface. Sample handlers and app registrations have been updated to use the new signatures. Existing overloads remain for backward compatibility. Also added `.claude/settings.local.json` to .gitignore. This enables graceful cancellation of long-running operations and aligns with modern .NET async best practices.
Previously, updated messages were created with the recipient explicitly set to the original sender using .WithRecipient. This change removes the .WithRecipient call, so updated messages are now created with only the message text and no explicit recipient.
Copilot AI review requested due to automatic review settings February 13, 2026 04:25

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 8 out of 8 changed files in this pull request and generated 4 comments.


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

Comment thread Samples/Samples.TargetedMessages/Program.cs
Comment thread Tests/Microsoft.Teams.Apps.Tests/AppTests.cs
Comment thread Libraries/Microsoft.Teams.Api/Activities/Message/MessageActivity.cs
Comment thread Libraries/Microsoft.Teams.Apps/Contexts/Context.Send.cs
Consistently pass cancellationToken to all async messaging methods in the Teams bot handler for better reliability. Simplify member retrieval logic. Update README: remove ngrok instructions, add TenantId to config example, and clarify the send command behavior.
Copilot AI review requested due to automatic review settings February 17, 2026 15:49

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 8 out of 8 changed files in this pull request and generated 2 comments.


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

Comment thread Tests/Microsoft.Teams.Api.Tests/Activities/Message/MessageActivityTests.cs Outdated
Copilot AI review requested due to automatic review settings February 17, 2026 19:57

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 8 out of 8 changed files in this pull request and generated no new comments.


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

corinagum pushed a commit to microsoft/teams.ts that referenced this pull request Feb 18, 2026
…g flags (#449)

use skip-test-verification

Applies the targeted messaging API refactor from
[microsoft/teams.net#318](microsoft/teams.net#318).
The change consolidates `withTargetedRecipient()` into an enhanced
`withRecipient()` method, making targeted message creation more explicit
and reducing API surface area.

## API Changes

**Before:**
```typescript
// Implicit recipient inference
new MessageActivity('message').withTargetedRecipient(true)

// Recipient by ID only
new MessageActivity('message').withTargetedRecipient('user-123')
```

**After:**
```typescript
// Explicit recipient with targeting flag
new MessageActivity('message').withRecipient(activity.from, true)

// Full account object
new MessageActivity('message').withRecipient(
  { id: 'user-123', name: 'User', role: 'user' },
  true
)
```

## Implementation

- Moved `isTargeted` from `MessageActivity` to base `Activity` class as
non-nullable boolean (default `false`)
- Enhanced `Activity.withRecipient(account, isTargeted?)` to accept
optional targeting flag
- Removed `MessageActivity.withTargetedRecipient()` method
- Updated interface definitions to reflect property relocation
- Updated examples and tests to use new API pattern
- Updated validation error messages

## Breaking Changes

The `withTargetedRecipient()` method is removed. Callers must migrate to
`withRecipient(account, true)`.

<!-- START COPILOT CODING AGENT TIPS -->
---

✨ Let Copilot coding agent [set things up for
you](https://github.com/microsoft/teams.ts/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: rido-min <[email protected]>
Co-authored-by: Rido <[email protected]>
@rido-min
rido-min merged commit 87729f7 into main Feb 18, 2026
13 checks passed
@rido-min
rido-min deleted the rido/fix-fluent-TM branch February 18, 2026 17:26
lilyydu added a commit to microsoft/teams.py that referenced this pull request Feb 19, 2026
## Summary
Applied changes from
[microsoft/teams.net#318](microsoft/teams.net#318)
to simplify targeted messages API.

## Changes Completed
- [x] Move `is_targeted` from `_MessageBase` to base `ActivityInput`
class (as `Optional[bool] = None`)
- [x] Add `is_targeted` parameter to `ActivityInput.with_recipient()`
method (optional, only sets when specified)
- [x] Override `with_recipient()` in `MessageActivityInput` with
`is_targeted` parameter
- [x] Remove `with_targeted_recipient()` method from
`MessageActivityInput`
- [x] Remove automatic recipient inference logic from
`activity_context.py`
- [x] Add validation in `ActivityContext.send()` for targeted messages
without recipients
- [x] Update error messages to reference "account" instead of "ID"
- [x] Update unit tests to use new `with_recipient(...,
is_targeted=True)` API
- [x] Update targeted-messages example to use new API
- [x] Remove unused imports
- [x] Fix linting issues
- [x] Fix test error message regex

## API Migration
**Before:** `message.with_targeted_recipient(True)` or
`message.with_targeted_recipient("user-id")`
**After:** `message.with_recipient(account, is_targeted=True)`

## Key Design Decisions
1. **`is_targeted` is `Optional[bool] = None`**: Avoids serializing
`isTargeted: false` for all activities, preventing potential wire-format
compatibility issues with Bot Framework/Teams endpoints
2. **`with_recipient()` only sets `is_targeted` when explicitly
specified**: Prevents silently clearing the flag when just updating
recipient. Pass `is_targeted=True` to enable targeting,
`is_targeted=False` to explicitly clear it, or omit/pass `None` to leave
unchanged
3. **Validation at send time**: Clear error messages when targeted
messages lack recipients, preventing 4xx errors from the service

## Breaking Change
This is a breaking change requiring updates to all code using targeted
messages. Recipients must now be explicitly set using Account objects.

Co-authored-by: rido-min <[email protected]>

<!-- START COPILOT CODING AGENT TIPS -->
---

💡 You can make Copilot smarter by setting up custom instructions,
customizing its development environment and configuring Model Context
Protocol (MCP) servers. Learn more [Copilot coding agent
tips](https://gh.io/copilot-coding-agent-tips) in the docs.

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: rido-min <[email protected]>
Co-authored-by: Rido <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Lily Du <[email protected]>
Co-authored-by: lilydu <[email protected]>
ShanmathiMayuramKrithivasan pushed a commit to ShanmathiMayuramKrithivasan/teams.net that referenced this pull request Mar 10, 2026
…oft#318)

This pull request refactors how targeted messages are handled in the
Teams API SDK, consolidating targeted message logic into the
`WithRecipient` method and removing the older `WithTargetedRecipient`
methods. The changes streamline the API, improve clarity, and update
documentation and tests to reflect the new approach.

**Targeted message API refactor:**

* The `WithTargetedRecipient` methods in `MessageActivity` are removed
and replaced with an updated `WithRecipient` method that accepts an
`Account` and an optional `isTargeted` parameter. This centralizes
targeted message logic and simplifies usage.
[[1]](diffhunk://#diff-05c926d7e09179d91c9c54a68c6b5efffb854bfddb70b431df77d6b2d280c44dL221-L247)
[[2]](diffhunk://#diff-05c926d7e09179d91c9c54a68c6b5efffb854bfddb70b431df77d6b2d280c44dR149-R155)
* The `IsTargeted` property is moved from `MessageActivity` to the base
`Activity` class and is now a non-nullable `bool`, ensuring consistent
handling across activity types.
[[1]](diffhunk://#diff-875c3804dbdc89735553c417d023929c307dcfbead113a1b7a1e47871cab636aR148-R150)
[[2]](diffhunk://#diff-05c926d7e09179d91c9c54a68c6b5efffb854bfddb70b431df77d6b2d280c44dL67-L73)
* The `WithRecipient` method in both `Activity` and `MessageActivity` is
updated to support the `isTargeted` flag, replacing previous targeted
recipient APIs.
[[1]](diffhunk://#diff-875c3804dbdc89735553c417d023929c307dcfbead113a1b7a1e47871cab636aL224-R230)
[[2]](diffhunk://#diff-05c926d7e09179d91c9c54a68c6b5efffb854bfddb70b431df77d6b2d280c44dR149-R155)

**Documentation and sample updates:**

* All code samples and documentation in `Samples.TargetedMessages` are
updated to use the new `WithRecipient(..., true)` pattern instead of
`WithTargetedRecipient`.
[[1]](diffhunk://#diff-fb8b297c63493ae97cc825e8ca804ff337fbdd797c7bfda4d466fa7d87b1a78dL32-R32)
[[2]](diffhunk://#diff-fb8b297c63493ae97cc825e8ca804ff337fbdd797c7bfda4d466fa7d87b1a78dL45-R45)
[[3]](diffhunk://#diff-fb8b297c63493ae97cc825e8ca804ff337fbdd797c7bfda4d466fa7d87b1a78dL59-R59)
[[4]](diffhunk://#diff-fb8b297c63493ae97cc825e8ca804ff337fbdd797c7bfda4d466fa7d87b1a78dL81-R81)
[[5]](diffhunk://#diff-fb8b297c63493ae97cc825e8ca804ff337fbdd797c7bfda4d466fa7d87b1a78dL112-R112)
[[6]](diffhunk://#diff-75e89258d7593bb275cee67b5b18f50627f33899143d6913ae44dc5dcf9bdcdaL100-R100)
[[7]](diffhunk://#diff-75e89258d7593bb275cee67b5b18f50627f33899143d6913ae44dc5dcf9bdcdaL113-R113)
[[8]](diffhunk://#diff-75e89258d7593bb275cee67b5b18f50627f33899143d6913ae44dc5dcf9bdcdaL123-R123)

**Test suite improvements:**

* Unit tests are refactored to validate the new `WithRecipient` API for
targeted messages, replacing tests for `WithTargetedRecipient`. New
tests confirm fluent chaining and correct property assignment.
[[1]](diffhunk://#diff-9893d0b4b2ecbdda9027a251e08702b2922792adfa9f3c369674bfc6e099392aL478-R510)
[[2]](diffhunk://#diff-9893d0b4b2ecbdda9027a251e08702b2922792adfa9f3c369674bfc6e099392aR528-R543)
* The test for sending a targeted message without a recipient
proactively is removed, as the new API enforces recipient assignment.

**Validation and error handling:**

* Error messages are updated to clarify that targeted messages sent
proactively must specify an explicit recipient ID.
* Logic for inferring recipients in reactive contexts is removed, as the
new API requires explicit recipient assignment for targeted messages.

---------

Co-authored-by: Copilot <[email protected]>
Co-authored-by: rido-min <[email protected]>
Co-authored-by: Copilot <[email protected]>
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.

5 participants