How to Implement Proactive Notifications Using Agents SDK #253
-
Hi team, I'm currently exploring the Microsoft Copilot Agents SDK and would like to implement proactive notifications, such as sending a message to a Teams user triggered by an external API call or scheduled event. Could you please guide me on how to achieve this using the Agents SDK? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@Pavani-NP this is mainly a Teams Extension function, To help you get through this though, let me highlight a few things.
In general, You should use When you need to initiate a conversation, you would use the
an example of this (focused to the specific tasks) looks like this: var newReference = new ConversationReference()
{
Agent = new ChannelAccount()
{
Id = "28:------" //conversationReference.Agent.Id
},
Conversation = new ConversationAccount()
{
Id = "a:------" //conversationReference.Conversation.Id
},
ServiceUrl = "https://smba.trafficmanager.net/<regioncode>/<tenantId>/"//conversationReference.ServiceUrl,
};
var cardString = (generated and populated adaptive card)
await adapter.ContinueConversationAsync(
AgentId, // ClientID of the agent you are communicating with
newReference,
async (ITurnContext turnContext, CancellationToken cancellationToken) =>
{
var proactiveMessage = Activity.CreateMessageActivity();
var adaptiveCardAttachment = new Attachment
{
ContentType = "application/vnd.microsoft.card.adaptive",
Content = cardString // here is an adaptive card that is not shown
};
proactiveMessage.Attachments = [adaptiveCardAttachment];
await turnContext.SendActivityAsync(proactiveMessage, cancellationToken);
},
default); That will create a proactive message to the user. If you do not have an existing conversation reference to work with, you will need to use
An example of this would look like: var Cp = new ConversationParameters(false,
agent: new ChannelAccount(id: "28:-----"), // ID of your agent registered in teams.
tenantId: "<tenantId>",
members: new List<ChannelAccount>()
{
// List of targets
new ChannelAccount(
id: "<AADOID of target user OR userID from previous conversation>"
)
}
);
var cardString = (generated and populated adaptive card)
await adapter.CreateConversationAsync(
AgentId, // ID of the agent your sending too.
"msteams", // ChannelID
TeamsProactiveServiceEndpoints.publicGlobal, // Endpoint for the global teams service.
"https://api.botframework.com/.default", // Scope for creating an access token for Teams
Cp, // ConversationParameters Created above.
async (ITurnContext turnContext, CancellationToken cancellationToken) =>
{
var proactiveMessage = Activity.CreateMessageActivity();
var adaptiveCardAttachment = new Attachment
{
ContentType = "application/vnd.microsoft.card.adaptive",
Content = cardString // here is an adaptive card that is not shown
};
proactiveMessage.Attachments = [adaptiveCardAttachment];
await turnContext.SendActivityAsync(proactiveMessage, cancellationToken);
},
default); This will also create a proactive notification, which you would then continue in the normal message loop. hope that helps a bit. |
Beta Was this translation helpful? Give feedback.
@Pavani-NP this is mainly a Teams Extension function,
You can read up on this here: https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/send-proactive-messages?tabs=dotnet
To help you get through this though, let me highlight a few things.
There are two concepts to keep in mind>
CreateConversationAsync
, you use this when you only know the Agent ID, and the target Users.ContinueConversationAsync
, you use this when you have already interacted with a user once and have captured the incoming conversation reference.In general, You should use
ContinueConversationAsync
for this job. You can use after the user as added or installed the agent into to a channel o…