Skip to main content

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

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Sometimes a dialog's button should simply close it: Got it, Cancel, or a form whose result your agent posts in the chat. Teams closes a dialog when the agent's answer to the button is no answer at all, and in the Microsoft 365 Agents SDK that's a handler that returns null.

Syntax​

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

// In the handler:
return Task.FromResult<TaskModuleResponse>(null!);
PartWhat it does
OnSubmit("<name>", handler)Routes the dialog button whose data has "task": "<name>"
nullNo answer: the SDK sends Teams an HTTP 200 with no body, and Teams closes the dialog
! after nullTells the C# compiler the null is intended. The SDK's handler type says the answer isn't null, so without it the build warns

Microsoft's Use dialogs with bots lists it first among the answers: The simplest response is no response at all. Your bot isn't required to respond when the user finishes with the dialog. In an async handler, write return null!;.

The Example​

The agent for this post announces a new version of an app. Its card's What's new button opens a dialog with three changes and a Got it button, which closes the dialog.

Prerequisites​

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

Step 1: Return No Answer​

ReleaseNotesAgent.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 HoClose;

[TeamsExtension]
public partial class ReleaseNotesAgent : AgentApplication
{
public ReleaseNotesAgent(AgentApplicationOptions options) : base(options)
{
OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("whatsnew", OpenAsync);
TeamsExtension.TaskModules.OnSubmit("whatsnew", CloseAsync);
}

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"] = "Expense app 2.4", ["size"] = "Large", ["weight"] = "Bolder" },
new JsonObject { ["type"] = "TextBlock", ["text"] = "The new version is out.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "What's new",
["data"] = new JsonObject
{
["msteams"] = new JsonObject { ["type"] = "task/fetch" },
["task"] = "whatsnew",
},
},
},
};

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"] = "- Scan a receipt with your phone's camera.\n- See your last ten claims on the start page.\n- Claims under 20 dollars are approved at once.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Got it", ["data"] = new JsonObject { ["task"] = "whatsnew" } },
},
};

TaskModuleResponse response = new()
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new
{
title = "What's new in 2.4",
width = "medium",
height = "small",
card = new TeamsAttachment { ContentType = AttachmentContentTypes.AdaptiveCard, Content = card },
},
},
};

return Task.FromResult(response);
}

// task/submit: no answer at all, so Teams closes the dialog and shows nothing.
// The handler's type promises a response; the SDK accepts null as "no answer", and null! tells C# that's intended.
private Task<TaskModuleResponse> CloseAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult<TaskModuleResponse>(null!);
}
}

ReleaseNotesAgent.cs in a macOS-style editor window: the constructor routes the whatsnew dialog with OnFetch and its Got it button with OnSubmit, and CloseAsync returns Task.FromResult of TaskModuleResponse with null!, with comments saying that no answer closes the dialog

The ! is there because the SDK's TaskSubmitHandler returns Task<TaskModuleResponse>, without ?. Plain null builds with warning CS8625: Cannot convert null literal to non-nullable reference type, and the SDK's code accepts the null anyway.

Step 2: Close It in Teams​

Send the agent any message, and select What's new:

A Teams dialog titled What's new in 2.4, from Dialog Demo, with three bullet points about scanning receipts, the last ten claims and claims under 20 dollars, and a Got it button

Select Got it: the dialog closed, and nothing else appeared. The agent's log showed its answer to the button:

Invoke response
{
"status": 200
}

A 200 with no body: the SDK's code turns a null answer into exactly this, and Teams closes the dialog.

Common Mistakes​

MistakeWhat happensDo this instead
return null; without !Warning CS8625null!
Changing the handler's return type to Task<TaskModuleResponse?>Warning CS8621 where the handler is registeredKeep Task<TaskModuleResponse>, and return null!
A Cancel button whose inputs are still checkedTeams asks for the required inputs before sending anythingAdd "associatedInputs": "none" to the button

Troubleshooting​

SymptomLikely causeFix
The dialog stays openThe handler returned a Continue or Message answerReturn null!
Unable to reach app. Please try again.No route matched the button's task, or the agent isn't runningCheck the agent's log for the invoke

FAQ​

Does the X send anything to the agent?​

No. In my tests, closing a dialog with its X sent the agent nothing at all. Only the dialog's own buttons reach the agent.

Can I do something before closing?​

Yes. Anything in the handler runs first, such as posting a message in the chat with turnContext.SendActivityAsync. The dialog closes when the handler returns null.

Conclusion​

To close a Teams dialog from your agent with nothing more to show, return null! from the button's OnSubmit handler. The SDK sends Teams an empty 200 answer, and Teams closes the dialog.

Additional Resources​