Skip to main content

How to Read the Values Submitted from a Teams Dialog in the Microsoft 365 Agents SDK in C#

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

A form in a Teams dialog is only useful once your agent reads what was typed. When someone selects a button in the dialog, Teams sends the agent everything in one invoke, and the Microsoft 365 Agents SDK hands your handler each value by its input's id.

Syntax​

The button, in the dialog's card:

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

The route and the values, in the agent:

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

// In the handler:
string value = request.GetDataString("<input id>");
PartWhat it does
"task": "<name>"The name your agent routes on. The dialog's Action.Submit needs no msteams object
OnSubmit("<name>", handler)Calls handler when someone selects that button in a dialog
requestThe TaskModuleRequest the handler gets. request.Data holds the button's data and every input's value
GetDataString("<input id>")One value, by the input's id, as text. An optional second argument is the value to use when it's missing

The handler returns what the dialog does next, as for OnFetch. Microsoft's Use dialogs with bots lists three answers: no response, a message, or another card. This post uses the first: the dialog closes.

The Example​

The agent for this post orders visitor badges. Its card's Order a badge button opens a dialog with one required input, Visitor's name, and an Order button. The agent reads the name, says in the chat that the badge will be ready, and closes the dialog.

Prerequisites​

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

Step 1: Route the Dialog's Button​

BadgeAgent.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 HoSubmit;

[TeamsExtension]
public partial class BadgeAgent : AgentApplication
{
public BadgeAgent(AgentApplicationOptions options) : base(options)
{
OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("badge", OpenBadgeFormAsync);
TeamsExtension.TaskModules.OnSubmit("badge", OrderBadgeAsync);
}

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"] = "Visitor badges", ["size"] = "Large", ["weight"] = "Bolder" },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Expecting a visitor? Order a badge, and the front desk prints it.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Order a badge",
["data"] = new JsonObject
{
["msteams"] = new JsonObject { ["type"] = "task/fetch" },
["task"] = "badge",
},
},
},
};

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

// task/fetch: the dialog with one input. Its button sends "task": "badge" with the input's value.
private Task<TaskModuleResponse> OpenBadgeFormAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
JsonObject card = new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = new JsonArray
{
new JsonObject
{
["type"] = "Input.Text",
["id"] = "visitor",
["label"] = "Visitor's name",
["isRequired"] = true,
["errorMessage"] = "Enter the visitor's name.",
},
},
["actions"] = new JsonArray
{
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Order", ["data"] = new JsonObject { ["task"] = "badge" } },
},
};

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

return Task.FromResult(response);
}

// task/submit: read the input by its id, answer in the chat, and close the dialog.
private async Task<TaskModuleResponse> OrderBadgeAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
string visitor = request.GetDataString("visitor");

await turnContext.SendActivityAsync($"A badge for **{visitor}** will be ready at the front desk.", cancellationToken: cancellationToken);

return null!;
}
}

BadgeAgent.cs in a macOS-style editor window: the constructor routes the badge dialog with OnFetch and its Order button with OnSubmit, and OrderBadgeAsync reads visitor with GetDataString, sends A badge for the visitor will be ready at the front desk, and returns null! to close the dialog

Step 2: Fill In the Dialog​

Send the agent any message, select Order a badge, and type a name:

A Teams dialog titled Order a visitor badge, from Dialog Demo, with a required Visitor's name box holding Ana Silva and an Order button

Select Order. The dialog closed, and the agent's message appeared under the card:

The Visitor badges card in the chat, with the agent's message under it: A badge for Ana Silva will be ready at the front desk.

What Teams Sent​

The task/submit invoke's value, from the agent's log:

task/submit value
{
"data": {
"task": "badge",
"visitor": "Ana Silva"
},
"context": {
"theme": "default"
}
}

data holds the button's own data, "task": "badge", merged with each input under its id. That's what GetDataString reads. The agent's answer was an empty {"status":200}, the "no response" answer.

Read Everything into a Class​

GetDataAs<T>() turns the whole data object into a type of your own. With a record that has a property for each key:

C#
public record BadgeOrder(string Task, string Visitor);

BadgeOrder order = request.GetDataAs<BadgeOrder>();

In this post's agent, that gave BadgeOrder { Task = badge, Visitor = Ana Silva }: the lowercase keys matched the properties' names.

Common Mistakes​

MistakeWhat happensDo this instead
Reading a key that isn't in dataGetDataString returns an empty stringPass a default as the second argument, or check for ""
Expecting numbers or true/falseCard inputs send their values as textParse the text, for example with int.TryParse

Troubleshooting​

SymptomLikely causeFix
The dialog doesn't closeThe handler returned a Continue answerReturn null!, or a message
A value is emptyThe input's id and the key you read differUse the same id

FAQ​

Can I post in the chat and keep the dialog open?​

Yes: SendActivityAsync posts in the chat, and what the dialog does next depends only on what the handler returns. A handler that posts in the chat and answers with a message leaves the dialog open, showing that message, until the person closes it.

What if the person closes the dialog with X?​

The agent gets nothing. Only the dialog's own buttons send task/submit.

Does Teams check required inputs first?​

Yes. In my tests, a required drop-down list left empty showed its errorMessage in the dialog, and Teams sent the agent nothing until it was filled in.

Conclusion​

Route the dialog's button with TeamsExtension.TaskModules.OnSubmit, read each input with request.GetDataString("<id>"), or all of them with GetDataAs<T>(), and return what the dialog does next. To just close it, return null!.

Additional Resources​