0

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?

Maxim
  • 3,836
  • 6
  • 42
  • 64
  • Why won't it work? A few months ago, I was able to initiate a message from the bot to a Slack user, even without previous conversation. I haven't tested it again recently, but that should work: you are able to create the conversation using IDs which are Slack values that you can query from Slack API (in fact it is composed of the User ID, the Team ID...) – Nicolas R Aug 27 '19 at 08:31
  • It throws an exception on the sdk the botbuilder is using. This is only thrown when the user has not yet conversated after boot – Maxim Aug 27 '19 at 08:52
  • What kind of exception? – Nicolas R Aug 27 '19 at 09:21
  • I updated my original answer with the exception – Maxim Aug 27 '19 at 17:43

1 Answers1

1

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.

mdrichardson
  • 7,141
  • 1
  • 7
  • 21