Reputation: 1
I'm working with a Node.js REST API using Restify to send notifications via Microsoft Teams Adaptive Cards. I can successfully send an Adaptive Card using the Teams API, but I'm struggling to update an existing Adaptive Card that was previously sent.
Here's a simplified version of my code:
server.post(
"/api/notification",
restify.plugins.queryParser(),
restify.plugins.bodyParser(),
checkAccessToken,
async (req, res) => {
const payload = req.body as NotificationPayload;
console.log("Received notification request for channel", payload.channelId);
let channel = cache.get<Channel>(payload.channelId);
if (!channel) {
channel = await notificationApp.notification.findChannel((c: any) => {
return Promise.resolve((c as Channel).info.id === payload.channelId);
});
if (channel) {
cache.set(payload.channelId, channel);
} else {
res.send(400, { message: "Invalid Channel Id - " + payload.channelId });
return;
}
}
const updatedTemplate = structuredClone(notificationTemplate);
const cardData = {
abc: payload.abc,
dev: payload.dev,
};
// Sending the adaptive card
const resp = await channel?.sendAdaptiveCard(
AdaptiveCards.declare<CardDataOnCall>(updatedTemplate).render(cardDataOnCall)
);
);
I tried using the following code to update the Adaptive Card:
const message = MessageFactory.attachment(updatedCard);
message.id = resp.id;
if (req.context) {
await req.context.updateActivity(message);
} else {
console.log("No context found to update the adaptive card");
}
I expected that by setting the message.id to the ID of the previously sent Adaptive Card and using req.context.updateActivity(message), the Adaptive Card would be updated in Microsoft Teams with the new data. However, it's not working as expected, and I receive an error saying that no context is available for updating the activity.
and also tried with the below code but still it is not working (https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/update-and-delete-bot-messages?tabs=typescript)
MessageFactory.attachment():
const message = MessageFactory.attachment(card);
message.id = context.activity.replyToId;
await context.updateActivity(message);
I tried even adding the response id of the previous message like below but still it is not working
await notificationApp.requestHandler(req, res, async (context) => {
console.log("Received request ", context);
await teamsBot.run(context);
const message = MessageFactory.attachment(updatedCard);
message.id = "1726633003955";
await context.updateActivity(message);
Upvotes: 0
Views: 155