Search code examples
javascriptnode.jsbotframeworkrestify

sending pro active messages to slack using bot framework


I have a use case where i would like to send a slack user a message of which i know the id of a notification once and a while using the bot framework.

Right now i have the following:

server.get("/api/notify", async (req, res) => {
  await adapter.createConversation(conversationReference, async turnContext => {
    await turnContext.sendActivity("proactive hello");
  });

  res.setHeader("Content-Type", "text/html");
  res.writeHead(200);
  res.write(
    "<html><body><h1>Proactive messages have been sent.</h1></body></html>"
  );
  res.end();
});

where a conversation reference looks like:

const conversationReference = {
  user: { id: "ID3:ID2", name: "user1" },
  bot: { id: "ID1:ID2", name: "bot1" },
  conversation: {
    isGroup: false,
    id: "ID1:ID2:ID3",
    conversationType: "slack",
    tenantId: "",
    name: ""
  },
  channelId: "slack",
  serviceUrl: "https://slack.botframework.com/"
};

But it only works if the user has talked to the bot since the bot has booted. But after a restart this won't work anymore until the user initiates a conversation.

When I try to send a pro active message after the bot rebooted and the user hasn't started a conversation after that i get the following exception:

UnhandledPromiseRejectionWarning: Error
  at new RestError (/usr/app/node_modules/@azure/ms-rest-js/dist/msRest.node.js:1397:28)
  at /usr/app/node_modules/@azure/ms-rest-js/dist/msRest.node.js:1849:37
  at process._tickCallback (internal/process/next_tick.js:68:7)

My question is: How can i persist this state, so i can still send pro active messages after a reboot?


Solution

  • Aha! This part of your question is the key:

    But it only works if the user has talked to the bot since the bot has booted. But after a restart this won't work anymore until the user initiates a conversation.

    This is almost definitely a TrustServiceUrl Issue. Please see this answer for additional context.

    Basically, on reboot, the bot forgets that it's okay to talk to that user. You need to "Trust" the ServiceUrl of the user/activity to ensure the bot knows it's okay to message them.