Skip to main content

Action commands

Action commands allow you to present your users with a modal pop-up called a dialog in Teams. The dialog collects or displays information, processes the interaction, and sends the information back to Teams compose box.

Action command invocation locations

There are three different areas action commands can be invoked from:

  1. Compose Area
  2. Compose Box
  3. Message

Compose Area and Box

Screenshot of Teams with outlines around the 'Compose Box' (for typing messages) and the 'Compose Area' (the menu option next to the compose box that provides a search bar for actions and apps).

Message action command

Screenshot of message extension response in Teams. By selecting the '...' button, a menu has opened with 'More actions' option in which they can select from a list of available message extension actions.

tip

See the Invoke Locations guide to learn more about the different entry points for action commands.

Setting up your Teams app manifest

To use action commands you have define them in the Teams app manifest. Here is an example:

"composeExtensions": [
{
"botId": "${{BOT_ID}}",
"commands": [
{
"id": "createCard",
"type": "action",
"context": [
"compose",
"commandBox"
],
"description": "Command to run action to create a card from the compose box.",
"title": "Create Card",
"parameters": [
{
"name": "title",
"title": "Card title",
"description": "Title for the card",
"inputType": "text"
},
{
"name": "subTitle",
"title": "Subtitle",
"description": "Subtitle for the card",
"inputType": "text"
},
{
"name": "text",
"title": "Text",
"description": "Text for the card",
"inputType": "textarea"
}
]
},
{
"id": "getMessageDetails",
"type": "action",
"context": [
"message"
],
"description": "Command to run action on message context.",
"title": "Get Message Details"
},
{
"id": "fetchConversationMembers",
"description": "Fetch the conversation members",
"title": "Fetch Conversation Members",
"type": "action",
"fetchTask": true,
"context": [
"compose"
]
},
]
}
]

Here we have defining three different commands:

  1. createCard - that can be invoked from either the compose or commandBox areas. Upon invocation a dialog will popup asking the user to fill the title, subTitle, and text.

Screenshot of a message extension dialog with the editable fields 'Card title', 'Subtitle', and 'Text'.

  1. getMessageDetails - It is invoked from the message overflow menu. Upon invocation the message payload will be sent to the app which will then return the details like createdDate, etc.

Screenshot of the 'More actions' message extension menu expanded with 'Get Message Details' option selected.

  1. fetchConversationMembers - It is invoked from the compose area. Upon invocation the app will return an adaptive card in the form of a dialog with the conversation roster.

Screenshot of the 'Fetch Conversation Members' option exposed from the message extension menu '...' option.

Handle submission

Handle submission when the createCard or getMessageDetails actions commands are invoked.

using System.Text.Json;
using Microsoft.Teams.Apps.MessageExtensions;

//...

bot.OnSubmitAction(async (context, cancellationToken) =>
{
MessageExtensionAction? action = context.Activity.Value;
string? commandId = action?.CommandId;
JsonElement? data = action?.Data as JsonElement?;

return commandId switch
{
"createCard" => HandleCreateCard(data),
"getMessageDetails" => HandleGetMessageDetails(action),
_ => MessageExtensionActionResponse.CreateBuilder()
.WithComposeExtension(MessageExtensionResponse.CreateBuilder()
.WithType(MessageExtensionResponseTypes.Message)
.WithText($"Unknown command: {commandId}"))
.Build()
};
});

Create card

HandleCreateCard() method

using System.Text.Json;
using Microsoft.Teams.Apps.MessageExtensions;
using Microsoft.Teams.Apps.Schema;
using Microsoft.Teams.Cards;

//...

private static InvokeResponse<MessageExtensionActionResponse> HandleCreateCard(JsonElement? data)
{
var title = GetJsonValue(data, "title") ?? "Default Title";
var description = GetJsonValue(data, "description") ?? "Default Description";

var card = new AdaptiveCard
{
Body = new List<CardElement>
{
new TextBlock("Custom Card Created")
{
Weight = TextWeight.Bolder,
Size = TextSize.Large,
Color = TextColor.Good
},
new TextBlock(title) { Weight = TextWeight.Bolder, Size = TextSize.Medium },
new TextBlock(description) { Wrap = true, IsSubtle = true }
}
};

TeamsAttachment attachment = TeamsAttachment.CreateBuilder()
.WithAdaptiveCard(JsonSerializer.SerializeToElement(card))
.Build();

return MessageExtensionActionResponse.CreateBuilder()
.WithComposeExtension(MessageExtensionResponse.CreateBuilder()
.WithType(MessageExtensionResponseTypes.Result)
.WithAttachmentLayout(TeamsAttachmentLayouts.List)
.WithAttachments(attachment))
.Build();
}

Create message details card

HandleGetMessageDetails() method

using System.Text.Json;
using Microsoft.Teams.Apps.MessageExtensions;
using Microsoft.Teams.Apps.Schema;
using Microsoft.Teams.Cards;

//...

private static InvokeResponse<MessageExtensionActionResponse> HandleGetMessageDetails(MessageExtensionAction? action)
{
var messageText = action?.MessagePayload?.Body?.Content ?? "No message content";
var messageId = action?.MessagePayload?.Id ?? "Unknown";

var card = new AdaptiveCard
{
Body = new List<CardElement>
{
new TextBlock("Message Details")
{
Weight = TextWeight.Bolder,
Size = TextSize.Large,
Color = TextColor.Accent
},
new TextBlock($"Message ID: {messageId}") { Wrap = true },
new TextBlock($"Content: {messageText}") { Wrap = true }
}
};

TeamsAttachment attachment = TeamsAttachment.CreateBuilder()
.WithAdaptiveCard(JsonSerializer.SerializeToElement(card))
.Build();

return MessageExtensionActionResponse.CreateBuilder()
.WithComposeExtension(MessageExtensionResponse.CreateBuilder()
.WithType(MessageExtensionResponseTypes.Result)
.WithAttachmentLayout(TeamsAttachmentLayouts.List)
.WithAttachments(attachment))
.Build();
}

Handle opening adaptive card dialog

Handle opening adaptive card dialog when the fetchConversationMembers command is invoked.

using Microsoft.Teams.Apps.MessageExtensions;

//...

bot.OnFetchTask(async (context, cancellationToken) =>
{
MessageExtensionAction? action = context.Activity.Value;
string? commandId = action?.CommandId;

return CreateFetchTaskResponse(commandId);
});

Create conversation members card

CreateFetchTaskResponse() method

using System.Text.Json;
using Microsoft.Teams.Apps.MessageExtensions;
using Microsoft.Teams.Apps.Schema;
using Microsoft.Teams.Apps.TaskModules;
using Microsoft.Teams.Cards;

//...

private static InvokeResponse<MessageExtensionActionResponse> CreateFetchTaskResponse(string? commandId)
{
var card = new AdaptiveCard
{
Body = new List<CardElement>
{
new TextBlock("Conversation Members is not implemented in C# yet :(")
{
Weight = TextWeight.Bolder,
Color = TextColor.Accent
}
}
};

TeamsAttachment attachment = TeamsAttachment.CreateBuilder()
.WithAdaptiveCard(JsonSerializer.SerializeToElement(card))
.Build();

return MessageExtensionActionResponse.CreateBuilder()
.WithTask(TaskModuleResponse.CreateBuilder()
.WithType(TaskModuleResponseTypes.Continue)
.WithTitle("Fetch Task Dialog")
.WithHeight(TaskModuleSizes.Small)
.WithWidth(TaskModuleSizes.Small)
.WithCard(attachment))
.Build();
}

// Helper method to extract JSON values
private static string? GetJsonValue(JsonElement? data, string key)
{
if (data?.ValueKind == JsonValueKind.Object && data.Value.TryGetProperty(key, out var value))
return value.GetString();
return null;
}

Resources