Skip to main content

How to Show an Error in a Teams Dialog in the Microsoft 365 Agents SDK in C#

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

Teams checks a dialog's required fields by itself, but only your agent knows its own rules: a room that holds 8 people, a budget, a date that's already taken. When a rule fails, the dialog should stay open and say what to fix, without making the person type everything again. In the Microsoft 365 Agents SDK, your agent answers the submit with the same form, the problem and the values.

Syntax​

In the dialog's OnSubmit handler:

C#
string value = request.GetDataString("<input id>");

if (<the value breaks a rule>)
{
// The same form again, with the problem and the typed value.
return Task.FromResult(<Form>(value, "<what to fix>"));
}

// Otherwise, finish: a message, no answer, or the next step.
PartWhat it does
request.GetDataString("<input id>")The value as the person typed it, as text
<Form>(value, "<what to fix>")Your method that builds the form, with each input's value set to what was typed, and the problem in a TextBlock above
Returning it as ContinueTeams replaces the dialog's card with this one, so the dialog stays open

A Continue answer to a submit is how Microsoft's Use dialogs with bots chains cards in a dialog. Sending the same form back is the shortest chain: the person stays on the same step.

The Example​

The agent for this post books meeting rooms that hold up to 8 people. Its dialog asks How many people? and has a Book button. For 12 people, the form comes back with Our rooms hold up to 8 people. Book two rooms, or the auditorium. in red, and 12 still in the box. For 6, the dialog ends with Room 3.14 is yours, for 6 people.

Prerequisites​

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

Step 1: Check the Value, and Send the Form Back​

RoomAgent.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 HoError;

[TeamsExtension]
public partial class RoomAgent : AgentApplication
{
private const int MaxPeople = 8;

public RoomAgent(AgentApplicationOptions options) : base(options)
{
OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("room", OpenAsync);
TeamsExtension.TaskModules.OnSubmit("room", BookAsync);
}

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"] = "Meeting rooms", ["size"] = "Large", ["weight"] = "Bolder" },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Book a room on the third floor.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Book a room",
["data"] = new JsonObject
{
["msteams"] = new JsonObject { ["type"] = "task/fetch" },
["task"] = "room",
},
},
},
};

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

private Task<TaskModuleResponse> OpenAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(RoomForm(people: "2", error: null));
}

// task/submit: check the number. Too many people: the same form again, with the problem and the number they typed.
private Task<TaskModuleResponse> BookAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
string people = request.GetDataString("people");

if (!int.TryParse(people, out int count) || count > MaxPeople)
{
return Task.FromResult(RoomForm(people, $"Our rooms hold up to {MaxPeople} people. Book two rooms, or the auditorium."));
}

TaskModuleResponse booked = new()
{
Task = new Response { Type = TaskModuleResponseTypes.Message, Value = $"Room 3.14 is yours, for {count} people." },
};

return Task.FromResult(booked);
}

// The form, showing people as the number and error, when there is one, in red above it.
private static TaskModuleResponse RoomForm(string people, string? error)
{
JsonArray body = new();

if (error is not null)
{
body.Add(new JsonObject { ["type"] = "TextBlock", ["text"] = error, ["color"] = "Attention", ["wrap"] = true });
}

body.Add(new JsonObject
{
["type"] = "Input.Number",
["id"] = "people",
["label"] = "How many people?",
["min"] = 1,
["isRequired"] = true,
["errorMessage"] = "Enter how many people, 1 or more.",
["value"] = int.TryParse(people, out int value) ? value : 2,
});

JsonObject card = new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = body,
["actions"] = new JsonArray
{
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Book", ["data"] = new JsonObject { ["task"] = "room" } },
},
};

return new TaskModuleResponse
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new
{
title = "Book a meeting room",
width = "medium",
height = "small",
card = new TeamsAttachment { ContentType = AttachmentContentTypes.AdaptiveCard, Content = card },
},
},
};
}
}

RoomAgent.cs in a macOS-style editor window: BookAsync reads people with GetDataString, and when it isn't a number or is more than MaxPeople returns RoomForm with the number and the message Our rooms hold up to 8 people. Book two rooms, or the auditorium.; otherwise it returns a Message answer Room 3.14 is yours, for the number of people

  • RoomForm(people, error) builds the form every time: when the dialog opens, with no error, and after a problem, with it.
  • ["value"] = ... puts the number back in the box. Card values arrive as text, so it's parsed back to a number for the Input.Number.
  • ["color"] = "Attention" shows the problem in the host's warning color: red, in Teams.
  • BookAsync checks the number with int.TryParse and the limit, and returns either the form again or the final message.

Step 2: Book for Too Many People​

Send the agent any message, select Book a room, and enter 12:

A Teams dialog titled Book a meeting room, from Dialog Demo, with a required How many people? box holding 12 and a Book button

Select Book:

The same dialog after Book: the red message Our rooms hold up to 8 people. Book two rooms, or the auditorium. at the top, and 12 still in the How many people? box

The dialog stayed open, under the same title, with the problem on top and 12 still in the box. The submit Teams sent was {"task": "room", "people": "12"}: the number arrived as text.

Step 3: Book a Room That Fits​

Change the number to 6, and select Book:

The dialog now showing only the message Room 3.14 is yours, for 6 people.

Common Mistakes​

MistakeWhat happensDo this instead
Sending back the empty formThe person types everything againSet each input's value to what was typed
Answering a problem with a MessageThe form is gone, and the person has to start overAnswer with the form, as Continue
Trusting Teams' checks aloneRules only the agent knows go uncheckedCheck in the agent too

Troubleshooting​

SymptomLikely causeFix
The error shows, but the box is emptyThe form wasn't given the typed valuePass it to the form and set value
The number box shows the old defaultThe text value didn't parse, so the default was usedParse it with int.TryParse, as RoomForm does
The dialog closes insteadThe handler returned a message or nullReturn the form with Continue

FAQ​

Why not use the input's errorMessage?​

errorMessage is shown by Teams for its own checks, such as a required input left empty, before anything reaches your agent. A rule only your agent knows, such as the room size, has to come back from the agent.

Can I check the number with max instead?​

For a fixed limit, max on the Input.Number keeps the number in range in Teams, which changes a bigger number to the maximum when the person leaves the box. Your agent should still check: Fixed: An Adaptive Card Number Input in Teams Changes to Its Maximum Instead of Showing the Error Message.

Conclusion​

To show an error in a Teams dialog, check the submitted values in the OnSubmit handler, and when a rule fails, answer with the same form as Continue: the problem in a red TextBlock on top, and each input's value set to what was typed.

Additional Resources​