Skip to main content

How to Close a Teams Dialog with a Message in the Microsoft 365 Agents SDK in C#

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

C#
return new TaskModuleResponse
{
Task = new Response
{
Type = TaskModuleResponseTypes.Message,
Value = "<text to show>",
},
};
PartWhat it does
TaskModuleResponseTypes.Message"Show a message", instead of Continue, which shows a card
ValueThe 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​

BookClubAgent.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 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);
}
}

BookClubAgent.cs in a macOS-style editor window: the constructor routes the bookclub dialog with OnFetch and its Join button with OnSubmit, and JoinAsync returns a TaskModuleResponse of type Message with the value You're in! See you on Thursday.

Step 2: Join in Teams​

Send the agent any message, select Join the book club, and the dialog opens:

A Teams dialog titled Join the book club, from Dialog Demo: This month: The Little Prince, We meet on Thursdays at 17:30 in the library corner, and a Join button

Select Join:

The same dialog, still titled Join the book club, now showing only the text You're in! See you on Thursday. in its middle

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:

Invoke response
{
"status": 200,
"body": {
"task": {
"type": "message",
"value": "You're in! See you on Thursday."
}
}
}

Common Mistakes​

MistakeWhat happensDo this instead
Expecting the dialog to close by itselfIt stays open, showing the messageReturn no answer when it should close

Troubleshooting​

SymptomLikely causeFix
The card stays instead of the messageThe handler returned ContinueUse TaskModuleResponseTypes.Message
Unable to reach app. Please try again.No route answered, or the agent isn't runningCheck 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​