Skip to main content

How to Open a Teams Dialog from an Adaptive Card in the Microsoft 365 Agents SDK in C#

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

A Teams dialog is a window that opens over the chat, with its own Adaptive Card. Your agent opens one from a card button: Teams asks the agent what the dialog should show, and the agent answers with a title, a size and a card.

Syntax​

The button, in the card the agent sends to the chat:

Card JSON
{
"type": "Action.Submit",
"title": "<button text>",
"data": {
"msteams": { "type": "task/fetch" },
"task": "<dialog name>"
}
}

The route and the answer, in the agent:

C#
TeamsExtension.TaskModules.OnFetch("<dialog name>", handler);

// In the handler:
return new TaskModuleResponse
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new { title = "<title>", width = "<size>", height = "<size>", card = <the dialog's card> },
},
};
PartWhat it does
"msteams": { "type": "task/fetch" }Tells Teams this button opens a dialog, instead of sending a message
"task": "<dialog name>"The name your agent routes on
OnFetch("<dialog name>", handler)Calls handler when Teams asks for that dialog. The SDK reads the task value from the button's data
TaskModuleResponseTypes.Continue"Show this in the dialog"
titleThe dialog's title, under the app's name
width, heightsmall, medium or large, or a number of pixels
cardThe Adaptive Card the dialog shows, as a TeamsAttachment

Microsoft's Use dialogs with bots explains the button: The data object in Action.Submit contains a msteams object so it doesn't interfere with other properties in the card. When someone selects it, Teams sends the agent an invoke named task/fetch, and the answer decides what opens.

TeamsExtension is the Teams extension's property, which needs [TeamsExtension] on the agent class: How to Add the Teams Extension to an Agent in the Microsoft 365 Agents SDK in C# sets it up.

The Example​

The agent for this post is a front desk. Any message gets a card with a Show opening hours button, which opens a dialog titled Opening hours with the desk's hours. The dialog has no buttons of its own: you close it with its X.

Prerequisites​

  • An agent that already runs in Microsoft Teams, in a personal chat, with the Teams extension added.

Step 1: Send the Card and Answer the Dialog Request​

FrontDeskAgent.cs
using System.Text.Json.Nodes;
using Microsoft.Agents.Builder;
using Microsoft.Agents.Builder.App;
using Microsoft.Agents.Builder.State;
using Microsoft.Agents.Core.Models;
using Microsoft.Agents.Extensions.MSTeams;
using Microsoft.Teams.Apps.Schema;
using Microsoft.Teams.Apps.TaskModules;

namespace HoOpen;

[TeamsExtension]
public partial class FrontDeskAgent : AgentApplication
{
public FrontDeskAgent(AgentApplicationOptions options) : base(options)
{
OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("hours", OpenHoursAsync);
}

// A card with a button that opens a dialog.
private Task SendCardAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken)
{
JsonObject card = new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = "Front desk", ["size"] = "Large", ["weight"] = "Bolder" },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Visitors, parcels and parking: the front desk helps with all three.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Show opening hours",
["data"] = new JsonObject
{
["msteams"] = new JsonObject { ["type"] = "task/fetch" },
["task"] = "hours",
},
},
},
};

return turnContext.SendActivityAsync(
MessageFactory.Attachment(new Attachment { ContentType = ContentTypes.AdaptiveCard, Content = card }),
cancellationToken);
}

// Teams asks what to show in the dialog: a card with the opening hours.
private Task<TaskModuleResponse> OpenHoursAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
JsonObject card = new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = "Monday to Thursday: 8:00 to 18:00", ["wrap"] = true },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Friday: 8:00 to 16:00", ["wrap"] = true },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Saturday and Sunday: closed", ["wrap"] = true },
},
};

TaskModuleResponse response = new()
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new
{
title = "Opening hours",
width = "medium",
height = "small",
card = new TeamsAttachment { ContentType = AttachmentContentTypes.AdaptiveCard, Content = card },
},
},
};

return Task.FromResult(response);
}
}

FrontDeskAgent.cs in a macOS-style editor window: the constructor routes messages to SendCardAsync and the hours dialog to OpenHoursAsync with TeamsExtension.TaskModules.OnFetch, and OpenHoursAsync returns a continue response with the title Opening hours, width medium, height small and a card listing the hours

  • SendCardAsync sends the card with the button. Its data carries "task": "hours".
  • OnFetch("hours", OpenHoursAsync) routes the dialog request for hours.
  • OpenHoursAsync answers with Continue: the dialog's title, its size, and its card, wrapped in a TeamsAttachment with the Adaptive Card content type.

Step 2: Open the Dialog in Teams​

Send the agent any message. The card arrives:

The Front desk card in Teams: Visitors, parcels and parking: the front desk helps with all three, and a Show opening hours button

Select Show opening hours:

A Teams dialog with the app icon, Dialog Demo and the title Opening hours at the top, an X to close it, and three lines: Monday to Thursday: 8:00 to 18:00, Friday: 8:00 to 16:00, Saturday and Sunday: closed

Teams put the app's name and icon above the title, and an X to close it. Closing it sends the agent nothing.

What Teams Sent and What the Agent Answered​

The agent's log showed the invoke. Its value:

task/fetch value
{
"data": {
"task": "hours",
"type": "task/fetch"
},
"context": {
"theme": "default"
}
}

The button's data arrived under data, with the msteams object turned into "type": "task/fetch", and context.theme says which Teams theme the person uses. The SDK matched "task": "hours" and sent the handler's answer back as the invoke's response:

Invoke response
{
"status": 200,
"body": {
"task": {
"type": "continue",
"value": {
"title": "Opening hours",
"width": "medium",
"height": "small",
"card": {
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"type": "AdaptiveCard",
"version": "1.5",
"body": [
{
"type": "TextBlock",
"text": "Monday to Thursday: 8:00 to 18:00",
"wrap": true
},
{
"type": "TextBlock",
"text": "Friday: 8:00 to 16:00",
"wrap": true
},
{
"type": "TextBlock",
"text": "Saturday and Sunday: closed",
"wrap": true
}
]
}
}
}
}
}
}

When No Route Matches​

A typo in the task name is easy to make. With the route registered as opening-hours and the button still sending hours, selecting the button showed this:

A Teams dialog with only the app icon, Dialog Demo, and a red message: Unable to reach app. Please try again.

The agent's log showed the invoke arriving, and no answer from a handler. When no route answers an invoke, the SDK responds with HTTP 501, Not Implemented, and Teams shows Unable to reach app. Please try again., the same message as when the agent isn't running at all. So when you see it, check the agent's log before the network: if the invoke is there, compare its task value with your routes.

Common Mistakes​

MistakeWhat happensDo this instead
No msteams object in the button's dataThe button sends the agent a message with the card's values, and no dialog opensAdd "msteams": { "type": "task/fetch" }
A different task value in the card and in OnFetchUnable to reach app. Please try again.Use one name in both places

Troubleshooting​

SymptomLikely causeFix
Unable to reach app. Please try again. with the invoke in the logNo route matched the task valueFix the name, or the OnFetch route
Unable to reach app. Please try again. with nothing in the logThe agent or its tunnel isn't runningStart them, and check the bot's endpoint
CS0103 for TeamsThe extension's property is called TeamsExtensionWrite TeamsExtension.TaskModules

FAQ​

Can a dialog open without a card button?​

From a bot, Microsoft's guide names two ways: a card button with task/fetch, as here, and a deep link to a dialog. Message extensions open dialogs too.

How do I add buttons that work inside the dialog?​

Give the dialog's card Action.Submit buttons, and route them with OnSubmit: How to Read the Values Submitted from a Teams Dialog in the Microsoft 365 Agents SDK in C#.

What sizes can a dialog have?​

small, medium, large, or pixels: How to Set the Size of a Teams Dialog in the Microsoft 365 Agents SDK in C#.

Conclusion​

To open a Teams dialog, give a card's Action.Submit the data "msteams": { "type": "task/fetch" } and a task name, route that name with TeamsExtension.TaskModules.OnFetch, and answer with a Continue response holding the title, the size and the card.

Additional Resources​