Skip to main content

How to Build a Multi-Step Teams Dialog in the Microsoft 365 Agents SDK in C#

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

Some forms read better in steps, especially when step two depends on step one: pick a workshop, then pick one of that workshop's sessions. A Teams dialog can hold several steps. Each step's button gets the next step's card from your agent, and the card carries what was chosen so far.

Syntax​

C#
TeamsExtension.TaskModules.OnSubmit("<step 1 button>", handler); // returns step 2's card, as Continue
TeamsExtension.TaskModules.OnSubmit("<step 2 button>", handler); // returns the next card, or ends the dialog
TeamsExtension.TaskModules.OnSubmit("back", handler); // returns step 1's card, filled in
A later step's button
{
"type": "Action.Submit",
"title": "<button text>",
"data": { "task": "<step name>", "<earlier answer>": "<its value>" }
}
PartWhat it does
A Continue answer to a submitPuts the next card in the dialog, with its own title
The earlier answers in dataKeep step 1's choice once its input is gone: Teams sends a button's data along with the card's inputs
"associatedInputs": "none" on BackSends no inputs, so Back works even when the current step isn't filled in
The last step's answerA Message, or no answer, to end the dialog

Microsoft's Use dialogs with bots describes it: You can chain Adaptive Cards into a multi-step wizard by returning a ContinueTask response from the submit handler. Each step returns a new card, and the final step returns a MessageTask to close the dialog. In the Agents SDK, those are TaskModuleResponseTypes.Continue and TaskModuleResponseTypes.Message. In my tests, the final message took the card's place in the dialog, which stayed open until the person closed it.

The Example​

The agent for this post signs people up for one of two workshops, each with two sessions. Step 1 asks for the workshop, with Next. Step 2 lists that workshop's sessions, with Sign up and Back. Sign up ends with You're signed up for Presentation skills, Friday 11:00.

Prerequisites​

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

Step 1: Write the Steps​

WorkshopAgent.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 HoSteps;

[TeamsExtension]
public partial class WorkshopAgent : AgentApplication
{
// Each workshop and its sessions.
private static readonly Dictionary<string, string[]> Sessions = new()
{
["Excel basics"] = ["Tuesday 10:00", "Thursday 14:00"],
["Presentation skills"] = ["Wednesday 09:00", "Friday 11:00"],
};

public WorkshopAgent(AgentApplicationOptions options) : base(options)
{
OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("workshop", OpenAsync);
TeamsExtension.TaskModules.OnSubmit("pick-workshop", PickWorkshopAsync);
TeamsExtension.TaskModules.OnSubmit("pick-session", PickSessionAsync);
TeamsExtension.TaskModules.OnSubmit("back", BackAsync);
}

private Task SendCardAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken)
{
JsonObject card = Card(
new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = "Workshops", ["size"] = "Large", ["weight"] = "Bolder" },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Two workshops this week, two sessions each.", ["wrap"] = true },
},
new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Sign up",
["data"] = new JsonObject
{
["msteams"] = new JsonObject { ["type"] = "task/fetch" },
["task"] = "workshop",
},
},
});

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

// Step 1 opens with the workshop list.
private Task<TaskModuleResponse> OpenAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(StepOne(workshop: ""));
}

// Step 1's Next: show step 2, with the sessions of the workshop they picked.
private Task<TaskModuleResponse> PickWorkshopAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(StepTwo(request.GetDataString("workshop")));
}

// Step 2's Back: step 1 again, with their workshop still picked.
private Task<TaskModuleResponse> BackAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(StepOne(request.GetDataString("workshop")));
}

// Step 2's Sign up: the last step, so the dialog ends with a message.
private Task<TaskModuleResponse> PickSessionAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
string workshop = request.GetDataString("workshop");
string session = request.GetDataString("session");

TaskModuleResponse done = new()
{
Task = new Response { Type = TaskModuleResponseTypes.Message, Value = $"You're signed up for {workshop}, {session}." },
};

return Task.FromResult(done);
}

private static TaskModuleResponse StepOne(string workshop)
{
JsonObject card = Card(
new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = "Step 1 of 2: pick a workshop.", ["wrap"] = true },
Choices("workshop", "Workshop", Sessions.Keys, workshop),
},
new JsonArray
{
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Next", ["data"] = new JsonObject { ["task"] = "pick-workshop" } },
});

return Show("Sign up for a workshop", card);
}

// Step 2 carries the workshop in its buttons' data: the card has no workshop input any more.
private static TaskModuleResponse StepTwo(string workshop)
{
JsonObject card = Card(
new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = $"Step 2 of 2: pick a session of {workshop}.", ["wrap"] = true },
Choices("session", "Session", Sessions[workshop], ""),
},
new JsonArray
{
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Sign up", ["data"] = new JsonObject { ["task"] = "pick-session", ["workshop"] = workshop } },
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Back", ["associatedInputs"] = "none", ["data"] = new JsonObject { ["task"] = "back", ["workshop"] = workshop } },
});

return Show($"Sign up for {workshop}", card);
}

private static JsonObject Choices(string id, string label, IEnumerable<string> options, string value)
{
JsonArray choices = new();

foreach (string option in options)
{
choices.Add(new JsonObject { ["title"] = option, ["value"] = option });
}

return new JsonObject
{
["type"] = "Input.ChoiceSet",
["id"] = id,
["label"] = label,
["style"] = "expanded",
["isRequired"] = true,
["errorMessage"] = $"Pick a {label.ToLowerInvariant()}.",
["value"] = value,
["choices"] = choices,
};
}

private static JsonObject Card(JsonArray body, JsonArray actions) => new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = body,
["actions"] = actions,
};

private static TaskModuleResponse Show(string title, JsonObject card) => new()
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new
{
title,
width = "medium",
height = "small",
card = new TeamsAttachment { ContentType = AttachmentContentTypes.AdaptiveCard, Content = card },
},
},
};
}

WorkshopAgent.cs in a macOS-style editor window: the constructor routes workshop with OnFetch and pick-workshop, pick-session and back with OnSubmit; PickWorkshopAsync returns StepTwo with the chosen workshop; BackAsync returns StepOne with it; StepTwo builds a card whose Sign up and Back buttons carry the workshop in their data, with associatedInputs none on Back

  • StepOne(workshop) builds step 1, with the workshop picked when it's given, through the choice list's value.
  • PickWorkshopAsync answers Next with step 2 for the workshop that was picked.
  • StepTwo(workshop) builds step 2 from that workshop's sessions. Step 2 has no workshop input, so both its buttons carry workshop in their data.
  • BackAsync answers Back with step 1, the workshop still picked.
  • PickSessionAsync ends the dialog with a message, using both answers.

Step 2: Go Through the Steps in Teams​

Send the agent any message, select Sign up, and pick a workshop:

Step 1 in a Teams dialog titled Sign up for a workshop: Step 1 of 2: pick a workshop., a required Workshop list with Excel basics and Presentation skills, Presentation skills selected, and a Next button

Select Next. The dialog's title and card changed to step 2, with that workshop's sessions:

Step 2 in the same dialog, now titled Sign up for Presentation skills: Step 2 of 2: pick a session of Presentation skills., a required Session list with Wednesday 09:00 and Friday 11:00, and Sign up and Back buttons

Back brought step 1 back with Presentation skills still selected. Next again, Friday 11:00, and Sign up:

The dialog, still titled Sign up for Presentation skills, showing only the message You're signed up for Presentation skills, Friday 11:00.

What Each Button Sent​

The data of each task/submit, from the agent's log, in order:

task/submit data
{"task": "pick-workshop", "workshop": "Presentation skills"}
{"task": "back", "workshop": "Presentation skills"}
{"task": "pick-workshop", "workshop": "Presentation skills"}
{"task": "pick-session", "workshop": "Presentation skills", "session": "Friday 11:00"}
  • Next sent step 1's input, workshop.
  • Back sent only its own data: with "associatedInputs": "none", no inputs.
  • Sign up sent its data, with the carried workshop, plus step 2's input, session.

Common Mistakes​

MistakeWhat happensDo this instead
Not carrying step 1's answer in step 2's buttonsThe last step doesn't know itPut it in each button's data
Back without "associatedInputs": "none"Teams wants step 2's required inputs before going backAdd "associatedInputs": "none"
One task value for every step's buttonOne handler has to work out which step it's onGive each step's button its own task value

Troubleshooting​

SymptomLikely causeFix
Back shows step 1 emptyStep 1 wasn't given the earlier answer as its inputs' valuePass it in, and set value
The title doesn't change between stepsEach step returned the same titleGive each step's answer its own title
The dialog closes after step 1Step 1's handler returned null or a messageReturn the next step as Continue

FAQ​

Can I keep the answers in state instead of the buttons?​

Yes, conversation or user state works too, and it keeps values out of the card. Values in a button's data come back from the client, so check them again on the last step before you use them.

How many steps can a dialog have?​

Each step is one more Continue answer, so the code sets the number. Two or three steps read well, and each title can say where the person is.

Conclusion​

A multi-step Teams dialog is a chain of Continue answers: each step's button returns the next card, later steps carry earlier answers in their buttons' data, and Back with "associatedInputs": "none" returns the previous card filled in. The last step ends with a message or no answer.

Additional Resources​