Forum Discussion

mattphillips's avatar
mattphillips
Copper Contributor
Apr 25, 2025

Notification bot create card then add reply to that card

namespace MyTeamsApp5.Controllers
{
    [Route("api/notification")]
    [ApiController]
    public class NotificationController : ControllerBase
    {
        private readonly ConversationBot _conversation;
        private readonly string _adaptiveCardFilePath = Path.Combine(".", "Resources", "NotificationDefault.json");

        public NotificationController(ConversationBot conversation)
        {
            this._conversation = conversation;
        }

        [HttpPost]
        public async Task<ActionResult> PostAsync(CancellationToken cancellationToken = default)
        {
            // Read adaptive card template
            var cardTemplate = await System.IO.File.ReadAllTextAsync(_adaptiveCardFilePath, cancellationToken);

            var pageSize = 100;
            string continuationToken = null;
            do
            {
                var pagedInstallations = await _conversation.Notification.GetPagedInstallationsAsync(pageSize, continuationToken, cancellationToken);
                continuationToken = pagedInstallations.ContinuationToken;
                var installations = pagedInstallations.Data;
                foreach (var installation in installations.Where(o => o.Type == NotificationTargetType.Channel))
                {
                    // Build and send adaptive card
                    var cardContent = new AdaptiveCardTemplate(cardTemplate).Expand
                    (
                        new NotificationDefaultModel
                        {
                            Title = "New Event Occurred!",
                            AppName = "Contoso App Notification",
                            Description = $"This is a sample http-triggered notification to {installation.Type}",
                            NotificationUrl = "https://aka.ms/teamsfx-notification-new",
                        }
                    );
                    var messageResponse = await installation.SendAdaptiveCard(JsonConvert.DeserializeObject(cardContent), cancellationToken);


                    if (messageResponse != null)
                    {

                        var mentionActivity = MessageFactory.Text($"this is a reply.");
                        mentionActivity.ReplyToId = messageResponse.Id;

                        await installation.Adapter.ContinueConversationAsync(
                            installation.BotAppId,
                            installation.ConversationReference,
                            async (turnContext, cancellationToken) =>
                            {
                                turnContext.Activity.ReplyToId = messageResponse.Id;
                                await turnContext.SendActivityAsync(mentionActivity, cancellationToken);
                            },
                            cancellationToken);
                    }


                }

            } while (!string.IsNullOrEmpty(continuationToken));

            return Ok();
        }
    }
}

So I'm trying to create a notification bot for a ticket system. Where when the notification bot is called, it posts the ticket card to the channel , then posts a reply to this card with a message. However the above code creates new threads for each message and not a response for the second one. I think it's related to the turnContext not being the same maybe, so I'm not sure if it's actually possible. 

 

Any help would be great.

Resources