OfficeDev / teams-toolkit

Developer tools for building Teams apps
Other
453 stars 185 forks source link

Unable to Update Adaptive Card After Sending It #12398

Open tirumaleshYeligar opened 1 week ago

tirumaleshYeligar commented 1 week ago

I'm working on a Node.js application using Restify to send and update Microsoft Teams Adaptive Cards via the Teams API. I can send the initial Adaptive Card successfully, but when I try to update the card using the response/activity ID of the previously sent card, the update fails.

server.post( "/api/call", restify.plugins.queryParser(), restify.plugins.bodyParser(), checkAccessToken, async (req, res) => { const payload = req.body as OnCallNotificationPayload; console.log("Received on-call 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);
  })) as Channel;

  if (channel) {
    cache.set(payload.channelId, channel);
  } else {
    res.send(400, { message: "Invalid Channel Id - " + payload.channelId });
    return;
  }
}

const updatedTemplate = structuredClone(onCallNotificationTemplate);
let activityId;
let conversationReference;

try {
  const cardDataOnCall = {
    shiftDate: payload.shiftDate,
    scheduleName: payload.scheduleName,
    data: payload.data,
    hedwigUrl: payload.hedwigUrl,
    scheduleId: payload.scheduleId,
    userId: payload.userId,
    userName: payload.userName,
  };

  const resp = await channel?.sendAdaptiveCard(
    AdaptiveCards.declare<CardDataOnCall>(updatedTemplate).render(cardDataOnCall)
  );

  activityId = resp?.id;
  conversationReference = req.body.conversation;

  if (!activityId) {
    throw new Error("Failed to get activity ID");
  }

  res.send(200, {
    message: "On-call notification sent successfully",
    "message-id": resp.id,
  });

} catch (error) {
  console.error("Error sending on-call adaptive card:", error);
  res.send(500, { message: "Failed to send on-call notification" });
  return;
}

// Introduce a delay before updating the message
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
await delay(5000);

try {
  const updatedCardDataOnCall = {
    shiftDate: payload.shiftDate,
    scheduleName: "Updated Schedule Name",
    data: payload.data,
    hedwigUrl: payload.hedwigUrl,
    scheduleId: payload.scheduleId,
    userId: payload.userId,
    userName: "Updated User Name",
  };

  const updatedCard = AdaptiveCards.declare<CardDataOnCall>(updatedTemplate).render(updatedCardDataOnCall);

  // Trying to update the card with the same activity ID
  const message = MessageFactory.attachment(updatedCard);
  message.id = context.activity.replyToId;

  await context.updateActivity(message);

} catch (error) {
  console.error("Error updating on-call adaptive card:", error);
  res.send(500, { message: "Failed to update on-call notification" });
}

} );

microsoft-github-policy-service[bot] commented 1 week ago

Thank you for contacting us! Any issue or feedback from you is quite important to us. We will do our best to fully respond to your issue as soon as possible. Sometimes additional investigations may be needed, we will usually get back to you within 2 days by adding comments to this issue. Please stay tuned.

dooriya commented 1 week ago

@tirumaleshYeligar Could you share the error details you encountered when attempting to update the notification card using the Teams Message Edit mechanism?

dooriya commented 1 week ago

@tirumaleshYeligar Based on your code snippet, it seems the replyToId is null, likely because it's retrieved from an activity involving sending a notification card. Could you please double-check this?

  // Trying to update the card with the same activity ID
  const message = MessageFactory.attachment(updatedCard);
  message.id = context.activity.replyToId;

  await context.updateActivity(message);