Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Update Adaptive Card After Sending It #12398

Open
tirumaleshYeligar opened this issue Sep 16, 2024 · 3 comments
Open

Unable to Update Adaptive Card After Sending It #12398

tirumaleshYeligar opened this issue Sep 16, 2024 · 3 comments
Assignees
Labels

Comments

@tirumaleshYeligar
Copy link

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" });
}

}
);

Copy link
Contributor

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.

@microsoft-github-policy-service microsoft-github-policy-service bot added the needs attention This issue needs the attention of a contributor. label Sep 16, 2024
@adashen adashen added investigating TA:Infra Team Area: Infra labels Sep 18, 2024
@dooriya
Copy link
Contributor

dooriya commented Sep 19, 2024

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

@dooriya dooriya removed the needs attention This issue needs the attention of a contributor. label Sep 19, 2024
@dooriya
Copy link
Contributor

dooriya commented Sep 19, 2024

@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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants