-
Notifications
You must be signed in to change notification settings - Fork 16
Prompt Preview Support #433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Microsoft.Teams.Api.Entities; | ||
|
|
||
| public class TargetedMessageInfoEntity : Entity | ||
| { | ||
| [JsonPropertyName("messageId")] | ||
| [JsonPropertyOrder(3)] | ||
| public required string MessageId { get; set; } | ||
|
|
||
| public TargetedMessageInfoEntity() : base("targetedMessageInfo") { } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Teams.Api.Activities; | ||
| using Microsoft.Teams.Api.Entities; | ||
|
|
||
| namespace Microsoft.Teams.Apps; | ||
|
|
||
|
|
@@ -67,6 +68,20 @@ public partial class Context<TActivity> : IContext<TActivity> | |
| { | ||
| public async Task<T> Send<T>(T activity, CancellationToken cancellationToken = default) where T : IActivity | ||
| { | ||
| // Auto-populate targetedMessageInfo entity for prompt preview | ||
| // when the incoming activity was a targeted message (reactive flow). | ||
| #pragma warning disable ExperimentalTeamsTargeted | ||
| if (Activity.Recipient?.IsTargeted == true && Activity.Id is not null) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The guard is inside the generic Not a correctness bug, but wasted payload + log noise. Consider narrowing the guard: if (activity is MessageActivity && Activity.Recipient?.IsTargeted == true && Activity.Id is not null)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
| { | ||
| var hasEntity = activity.Entities?.Any(e => e is TargetedMessageInfoEntity) ?? false; | ||
| if (!hasEntity) | ||
| { | ||
| activity.Entities ??= new List<IEntity>(); | ||
|
ShanmathiMayuramKrithivasan marked this conversation as resolved.
Outdated
|
||
| activity.Entities.Add(new TargetedMessageInfoEntity { MessageId = Activity.Id }); | ||
| } | ||
| } | ||
| #pragma warning restore ExperimentalTeamsTargeted | ||
|
|
||
| var res = await Sender.Send(activity, Ref, CancellationToken); | ||
|
ShanmathiMayuramKrithivasan marked this conversation as resolved.
|
||
| await OnActivitySent(res, ToActivityType<IActivity>()); | ||
| return res; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| using System.Text.Json; | ||
|
|
||
| using Microsoft.Teams.Api.Entities; | ||
|
|
||
| namespace Microsoft.Teams.Api.Tests.Entities; | ||
|
|
||
| public class TargetedMessageInfoEntityTests | ||
| { | ||
| [Fact] | ||
| public void TargetedMessageInfoEntity_JsonSerialize() | ||
| { | ||
| var entity = new TargetedMessageInfoEntity() | ||
| { | ||
| MessageId = "1772129782775" | ||
| }; | ||
|
|
||
| var json = JsonSerializer.Serialize(entity, new JsonSerializerOptions() | ||
| { | ||
| WriteIndented = true, | ||
| IndentSize = 2, | ||
| DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull | ||
| }); | ||
|
|
||
| Assert.Equal(File.ReadAllText( | ||
| @"../../../Json/Entities/TargetedMessageInfoEntity.json" | ||
| ), json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TargetedMessageInfoEntity_JsonSerialize_Derived() | ||
| { | ||
| Entity entity = new TargetedMessageInfoEntity() | ||
| { | ||
| MessageId = "1772129782775" | ||
| }; | ||
|
|
||
| var json = JsonSerializer.Serialize(entity, new JsonSerializerOptions() | ||
| { | ||
| WriteIndented = true, | ||
| IndentSize = 2, | ||
| DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull | ||
| }); | ||
|
|
||
| Assert.Equal(File.ReadAllText( | ||
| @"../../../Json/Entities/TargetedMessageInfoEntity.json" | ||
| ), json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TargetedMessageInfoEntity_JsonDeserialize() | ||
| { | ||
| var json = File.ReadAllText(@"../../../Json/Entities/TargetedMessageInfoEntity.json"); | ||
| var entity = JsonSerializer.Deserialize<TargetedMessageInfoEntity>(json); | ||
|
|
||
| Assert.NotNull(entity); | ||
| Assert.Equal("targetedMessageInfo", entity.Type); | ||
| Assert.Equal("1772129782775", entity.MessageId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TargetedMessageInfoEntity_JsonDeserialize_Derived() | ||
| { | ||
| var json = File.ReadAllText(@"../../../Json/Entities/TargetedMessageInfoEntity.json"); | ||
| var entity = JsonSerializer.Deserialize<Entity>(json); | ||
|
|
||
| Assert.NotNull(entity); | ||
| Assert.IsType<TargetedMessageInfoEntity>(entity); | ||
|
|
||
| var targeted = (TargetedMessageInfoEntity)entity; | ||
| Assert.Equal("targetedMessageInfo", targeted.Type); | ||
| Assert.Equal("1772129782775", targeted.MessageId); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "type": "targetedMessageInfo", | ||
| "messageId": "1772129782775" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| using Microsoft.Teams.Api; | ||
| using Microsoft.Teams.Api.Activities; | ||
| using Microsoft.Teams.Api.Auth; | ||
| using Microsoft.Teams.Api.Entities; | ||
| using Microsoft.Teams.Apps.Activities; | ||
| using Microsoft.Teams.Apps.Testing.Plugins; | ||
|
|
||
| namespace Microsoft.Teams.Apps.Tests.Activities; | ||
|
|
||
| public class PromptPreviewTests | ||
| { | ||
| private readonly App _app = new(); | ||
| private readonly IToken _token = Globals.Token; | ||
|
|
||
| public PromptPreviewTests() | ||
| { | ||
| _app.AddPlugin(new TestPlugin()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Send_AutoPopulates_TargetedMessageInfoEntity_WhenIncomingIsTargeted() | ||
| { | ||
| IActivity? sentActivity = null; | ||
|
|
||
| _app.OnMessage(async (context, cancellationToken) => | ||
| { | ||
| sentActivity = await context.Send("Here is the result!", cancellationToken); | ||
| }); | ||
|
|
||
| // Simulate an incoming targeted message (bot's Recipient.IsTargeted = true) | ||
| var incomingActivity = new MessageActivity("summarize") | ||
| .WithId("1772129782775") | ||
| .WithFrom(new Account() { Id = "user1", Name = "User" }) | ||
| .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }, true); | ||
|
|
||
| await _app.Process<TestPlugin>(_token, incomingActivity); | ||
|
|
||
| Assert.NotNull(sentActivity); | ||
| Assert.NotNull(sentActivity!.Entities); | ||
|
|
||
| var targetedEntity = sentActivity.Entities!.OfType<TargetedMessageInfoEntity>().SingleOrDefault(); | ||
| Assert.NotNull(targetedEntity); | ||
| Assert.Equal("1772129782775", targetedEntity!.MessageId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Reply_AutoPopulates_TargetedMessageInfoEntity_WhenIncomingIsTargeted() | ||
| { | ||
| IActivity? sentActivity = null; | ||
|
|
||
| _app.OnMessage(async (context, cancellationToken) => | ||
| { | ||
| sentActivity = await context.Reply("Here is the result!", cancellationToken); | ||
| }); | ||
|
|
||
| var incomingActivity = new MessageActivity("summarize") | ||
| .WithId("1772129782775") | ||
| .WithFrom(new Account() { Id = "user1", Name = "User" }) | ||
| .WithConversation(new Api.Conversation() { Id = "conv1" }) | ||
| .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }, true); | ||
|
|
||
| await _app.Process<TestPlugin>(_token, incomingActivity); | ||
|
|
||
| Assert.NotNull(sentActivity); | ||
| Assert.NotNull(sentActivity!.Entities); | ||
|
|
||
| var targetedEntity = sentActivity.Entities!.OfType<TargetedMessageInfoEntity>().SingleOrDefault(); | ||
| Assert.NotNull(targetedEntity); | ||
| Assert.Equal("1772129782775", targetedEntity!.MessageId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Send_DoesNotAdd_TargetedMessageInfoEntity_WhenNotTargeted() | ||
| { | ||
| IActivity? sentActivity = null; | ||
|
|
||
| _app.OnMessage(async (context, cancellationToken) => | ||
| { | ||
| sentActivity = await context.Send("Hello!", cancellationToken); | ||
| }); | ||
|
|
||
| // Normal (non-targeted) incoming message | ||
| var incomingActivity = new MessageActivity("hello") | ||
| .WithId("123456") | ||
| .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }); | ||
|
|
||
| await _app.Process<TestPlugin>(_token, incomingActivity); | ||
|
|
||
| Assert.NotNull(sentActivity); | ||
| var targetedEntity = sentActivity!.Entities?.OfType<TargetedMessageInfoEntity>().SingleOrDefault(); | ||
| Assert.Null(targetedEntity); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Send_DoesNotDuplicate_TargetedMessageInfoEntity_WhenAlreadyPresent() | ||
| { | ||
| IActivity? sentActivity = null; | ||
|
|
||
| _app.OnMessage(async (context, cancellationToken) => | ||
| { | ||
| // Developer manually adds the entity (proactive-like scenario) | ||
| var activity = new MessageActivity("Result") | ||
| .AddEntity(new TargetedMessageInfoEntity { MessageId = "9999" }); | ||
|
|
||
| sentActivity = await context.Send(activity, cancellationToken); | ||
| }); | ||
|
|
||
| // Incoming activity is targeted | ||
| var incomingActivity = new MessageActivity("summarize") | ||
| .WithId("1772129782775") | ||
| .WithFrom(new Account() { Id = "user1", Name = "User" }) | ||
| .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }, true); | ||
|
|
||
| await _app.Process<TestPlugin>(_token, incomingActivity); | ||
|
|
||
| Assert.NotNull(sentActivity); | ||
| Assert.NotNull(sentActivity!.Entities); | ||
|
|
||
| var targetedEntities = sentActivity.Entities!.OfType<TargetedMessageInfoEntity>().ToList(); | ||
| Assert.Single(targetedEntities); | ||
| // The developer-provided entity should be preserved, not overwritten | ||
| Assert.Equal("9999", targetedEntities[0].MessageId); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new entity depends on
Account.IsTargeted, which is marked[Experimental("ExperimentalTeamsTargeted")](Account.cs:40). Other Targeted-Message surfaces carry the same attribute —ActivityClient.cs:103,128,151,MessageActivity.cs:155,Activity.cs:234. The auto-populate call site inContext.Send.cswraps the usage in#pragma warning disable ExperimentalTeamsTargeted, which itself indicates the feature is still experimental.For consistency with the rest of the Targeted-Message API surface, consider marking the class (and/or
MessageId)[Experimental("ExperimentalTeamsTargeted")]so consumers opt in explicitly until the feature stabilizes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.