billoverton
billoverton

Reputation: 2885

Proactive message not working in MS Teams after bot is restarted

I have a proactive messaging endpoint that works fine when a user is actively engaged in a conversation. For example, one use case where user asks for invoice data to be retrieved, which happens asynchronously and is sent back via proactive message. This works fine. But if a try to continue a conversation weeks later, the continueConversation action is failing. If I go into teams and initiate a new conversation with the bot, then resending the proactive message (without changing anything) works again.

In one of my use cases, the bot needs to follow up with the user 1+ weeks in the future. So I need to figure out how to send a proactive message to a teams user even if they haven't recently conversed with the bot. I'm not sure why continueConversation isn't working because the conversation ID doesn't change (and hasn't changed in months, probably not ever).

Here is the function I am using to send the proactive message.

server.post('/api/notify', async (req, res) => {
    //console.log(req.body);

    try {
        const conversationReference = req.body.conversationReference;
        await adapter.continueConversation(conversationReference, async turnContext => {
            // If you encounter permission-related errors when sending this message, see
            // https://aka.ms/BotTrustServiceUrl
            await turnContext.sendActivity(req.body.message);
        });

        res.setHeader('Content-Type', 'text/html');
        res.writeHead(200);
        res.write('<html><body><h1>Proactive messages have been sent.</h1></body></html>');
        res.end();
    } catch (error) {
        console.log('Bad Request. Please ensure your message contains the conversation reference and message text.');
        console.log(error);
        appInsightsClient.trackTrace({
            message: `${error.name} - ${path.basename(__filename)}`,
            severity: 4,
            properties: {'error':error.message,'callStack':error.stack, 'botName': process.env.botName}
        });

        res.setHeader('Content-Type', 'text/html');
        res.writeHead(400);
        res.write('<html><body><p>Bad Request. Please ensure your message contains the conversation reference and message text.</p></body></html>');
        res.end();
    }
});

Upvotes: 0

Views: 862

Answers (1)

billoverton
billoverton

Reputation: 2885

As the link in my own code says,

If your bot is restarted, a user awaiting a proactive message cannot receive it unless they have messaged the bot again after it restarted.

So that was exactly what the issue was. But the instructions on that page aren't giving the full details. You have to add the class via const { MicrosoftAppCredentials } = require('botframework-connector') as well as set the serviceUrl to pass (which is already available at conversationReference.serviceUrl).

So with these changes I added MicrosoftAppCredentials.trustServiceUrl(conversationReference.serviceUrl); before I send the proactive message and it started working fine even after bot was restarted.

Upvotes: 2

Related Questions