How to Close a Teams Dialog with a Message in the Microsoft 365 Agents SDK in C#
When a dialog's job is done, a short line such as You're in! tells the person it worked. A Teams dialog can end that way: your agent answers the dialog's button with a message, and Teams shows the text in place of the card.
Syntax
return new TaskModuleResponse
{
Task = new Response
{
Type = TaskModuleResponseTypes.Message,
Value = "<text to show>",
},
};
| Part | What it does |
|---|---|
TaskModuleResponseTypes.Message | "Show a message", instead of Continue, which shows a card |
Value | The text. For a message, it's a plain string |
The handler returning it is an OnSubmit handler: the code behind one of the dialog's buttons. Microsoft's Use dialogs with bots describes the answer: Teams displays a message in a pop-up message box in the dialog.
The Example
The agent for this post runs a book club. Its card's Join the book club button opens a dialog with this month's book and a Join button. Join answers with You're in! See you on Thursday.
Prerequisites
- An agent that already runs in Microsoft Teams, in a personal chat, with the Teams extension added.
Step 1: Answer the Button with a Message
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 HoMessage;
[TeamsExtension]
public partial class BookClubAgent : AgentApplication
{
public BookClubAgent(AgentApplicationOptions options) : base(options)
{
OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("bookclub", OpenAsync);
TeamsExtension.TaskModules.OnSubmit("bookclub", JoinAsync);
}
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"] = "Book club", ["size"] = "Large", ["weight"] = "Bolder" },
new JsonObject { ["type"] = "TextBlock", ["text"] = "One book a month, one hour on Thursdays.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Join the book club",
["data"] = new JsonObject
{
["msteams"] = new JsonObject { ["type"] = "task/fetch" },
["task"] = "bookclub",
},
},
},
};
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)
{
JsonObject card = new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = "This month: The Little Prince", ["weight"] = "Bolder", ["wrap"] = true },
new JsonObject { ["type"] = "TextBlock", ["text"] = "We meet on Thursdays at 17:30 in the library corner.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Join", ["data"] = new JsonObject { ["task"] = "bookclub" } },
},
};
TaskModuleResponse response = new()
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new
{
title = "Join the book club",
width = "medium",
height = "small",
card = new TeamsAttachment { ContentType = AttachmentContentTypes.AdaptiveCard, Content = card },
},
},
};
return Task.FromResult(response);
}
// task/submit: answer with a message. Teams shows it in the dialog, in place of the card.
private Task<TaskModuleResponse> JoinAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
TaskModuleResponse response = new()
{
Task = new Response
{
Type = TaskModuleResponseTypes.Message,
Value = "You're in! See you on Thursday.",
},
};
return Task.FromResult(response);
}
}

OpenAsyncopens the dialog with aContinueanswer, as in How to Open a Teams Dialog from an Adaptive Card in the Microsoft 365 Agents SDK in C#.JoinAsyncanswers the Join button withMessageand the text.
Step 2: Join in Teams
Send the agent any message, select Join the book club, and the dialog opens:

Select Join:

The card went, and the message took its place, centered, with the dialog's title kept. The dialog stays open until the person closes it with its X.
What the agent sent back, from its log:
{
"status": 200,
"body": {
"task": {
"type": "message",
"value": "You're in! See you on Thursday."
}
}
}
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting the dialog to close by itself | It stays open, showing the message | Return no answer when it should close |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The card stays instead of the message | The handler returned Continue | Use TaskModuleResponseTypes.Message |
| Unable to reach app. Please try again. | No route answered, or the agent isn't running | Check the agent's log for the invoke |
FAQ
Can I send a card in the chat as well?
Yes. Call turnContext.SendActivityAsync in the handler before returning: the chat gets the card or text, and the dialog gets the message.
Message or no answer: which should I use?
A message when the person should see that it worked before going back to the chat. No answer when going back to the chat is the confirmation, for example when the agent's reply appears there.
Conclusion
To end a Teams dialog with a message, return TaskModuleResponseTypes.Message with the text from the OnSubmit handler. Teams replaces the card with the text, under the same title, until the person closes the dialog.
Additional Resources
- Use dialogs with bots, on Microsoft Learn: the three answers to a dialog's submit.
- TaskModule.OnSubmit Method, on Microsoft Learn.
