Azure Bot Framework: using URL to send poactivate message (Node.js)

Firstly create a chatbot using Azure Bot framework and then deploy to github and web app (Refer to this article https://github.com/fuselabs/echobot).

Using the function to send poactivate message based on this sample https://github.com/Microsoft/BotBuilder-Samples/blob/master/Node/core-proactiveMessages/simpleSendMessage/index.js
Note: the function "sendProactiveMessage" must placed before the "server.get"code.


 function sendProactiveMessage(address) {
  var msg = new builder.Message().address(address);
  msg.text('Hello, this is a notification triggered by url');
  msg.textLocale('en-US');
  bot.send(msg);
}


server.get('/api/custom', function(req, res, next) {
  sendProactiveMessage(savedAddress);
  res.send('home: ')
  return next();
}); 
Before using above web app,  I used the Azure bot service to create a bot and then modified the code to add poactivate function. However, it didn't work and so I changed to use Azure web app and it works now!

Note: The above code only can send message to the last person who interactive with the bot. To send message to all users you need to get and save all users' channel address, and then using the function app to boost a "trigger" event of the bot to send message (Refer to the MS Bot Poactivate message template document). Below is a sample of the "trigger" event and how to send message to target user based on their channel address. 

bot.on("trigger", function (message) {      
           
            bot.send("trigger");
   var queuedMessage = message.value;
    var address=queuedMessage.address;
           var msg = new builder.Message().address(address);
           
bot.loadSession(address, (error, session) => {
   
          msg.attachments([
        new builder.HeroCard(session)
            .title("XXXX")           
            .text("XXXX")
            .images([builder.CardImage.create(session, "<Image URL>")])
            .buttons([
                builder.CardAction.openUrl(session, '<action URL>', 'XXXX')
            ])           
         ]);          
           
        session.send(msg);     
       });                     

});

Comments

Popular posts from this blog

Top JavaScript courses helping you develop SPFx webPart with ReactJS and AngularJS

SharePoint 2013 error - "Application error when access /_vti_bin/client.svc"

Enable the Microsoft Power BI report file type (.pbix) in SharePoint Search