Building News Bot with Microsoft BotBuilder and News API

Dibyendu Roy
RingCentral Developers

--

Bots are ubiquitous and are transforming the way we interact with software platforms and this change is for good.

Bots are taking over a significant portion of user interfaces across software platforms and products. Customers are interacting with bots to book restaurants seats, get customer support, book event tickets, schedule a haircut, book doctor’s appointment and examples go on. This is a welcome change and transforming the ways how business operate and how customers interact with businesses. Companies are dedicating pools of resources to build these bots. This new wave of Bot technologies has resulted in the creation of numerous open source frameworks to make the building of bots easier and streamlined. In this article with I discuss, one such framework built by Microsoft and how I have utilized it to build a cool bot that delivers top of the hour news headlines to me whenever I need. This app is also available in the RingCentral public App Gallery.

Link to the App: https://www.ringcentral.com/apps/scottynewsbot

What does the Bot do: Introducing “ScottyNewsBot”. ScottyNewsBot is a simple bot that can deliver you top of hour news headlines from your favorite news sources. This bot is integrated to work in RingCentral’s Glip, which is a unified messaging and collaboration platform. Once the bot is installed in Glip, you can search it as you would search for a person and once the bot is added to your conversation, you can type “Help”, which will show you the news sources from which it can deliver news to you. Once you type in one of the news sources it will provide you top of the hour news from that news source. A short video depicting it in action is below

Demonstration of the News Bot in action

The technology behind the bot: At a basic level, I have built this bot using Node.js and it is hosted in Heroku. The key frameworks and node packages used are

  1. botbuilder : This is the base package for building bots built by Microsoft
  2. botbuilder-glip : This is the extended package built on top of botbuilder that takes care of OAuth , support for public and private flow.
  3. newsapi : This is the node module provided by NewsAPI team that takes care of scraping the news.

I am also using the MongoDB to store the user Auth and glip mapping data , so that the user does not need to auth, again and again, this helps me to get the data saved in case the Heroku dyno restarts. I will put a note later about the issues on using Heroku.

You create the 2 key objects

const builder = require('botbuilder')
const NewsAPI = require('newsapi');

Create a Glip Connector object from botbuilder that will help to manage the Glip connectivity. I also used a MongoDB to store that.

const { GlipConnector } = require('botbuilder-glip')                       const { MongoStorage } = require('./mongoStorage');                                               const storage = new MongoStorage({ url: process.env.MONGODB_URI, db: process.env.MONGODB_DB });

The below code takes care of OAuth and Subscription

const connector = new GlipConnector({                         botLookup: async (botId) => {                           const botEntry = await storage.find('bots', botId)                           return botEntry;                         },                         verificationToken: process.env.GLIP_BOT_VERIFICATION_TOKEN,                         clientId: process.env.GLIP_CLIENT_ID,                         clientSecret: process.env.GLIP_CLIENT_SECRET,                         server: process.env.GLIP_API_SERVER,                         redirectUrl: `${process.env.GLIP_BOT_SERVER}/oauth`,                         webhookUrl: `${process.env.GLIP_BOT_SERVER}/webhook`,                         disableSubscribe: true                       });// For public glip bot                       server.get('/oauth', connector.listenOAuth())                                               //For private glip bot                       server.post('/oauth', connector.listenOAuth())
server.post('/webhook', connector.listen())

Note : disableSubscribe: true which means for webhook, I am enabling Webhook from our Developer Portal Frontend not using the code. Which can also be done.

Enabling Webhook from RingCentral Developer Portal

Once the bot is installed, we insert the auth data securely into backend MongoDB to make sure we do not need the same user to re-login in again

bot.on('installationUpdate', (event) => {  const botId = event.sourceEvent.TokenData.owner_id;  console.log(`New bot installed: ${botId}`);   const botData = {    identity: event.address.bot,    token: event.sourceEvent.TokenData  };  storage.insert('bots', botId, botData);});

and then under the below function is used to capture the webhook and reply back when user types something

bot.dialog('/', function (session){Your Business logicSend back to the Glip 
}

I send back message to the user using cards which are also supported as seen below

session.send({text: 'Current News Headlines from : - ' + source ,                attachments: [{                  type: 'Card',                  fallback: 'Text',                  color: "#00ff2a",                  text: newsList,                  title: "Top Headlines",                    footnote: {                      "text": "News brought to you by News API",                      "iconUri": "",                      "time": ""                    }                }]              })

That's all, and based on what logic you write the bot will respond accordingly.

Note: In Heroku, you must use a Persistence data store like MongoDB etc else your cache or data stored in local files gets deleted as and when Heroku dyno gets restarted.

The GitHub Code can be found here: https://github.com/dibyenduroy/botbuilder-glip-newsbot

The Bot is available for public usage on RingCentral App Gallery: https://www.ringcentral.com/apps/scottynewsbot

Hope you have found this useful, if you want to build bots on our RingCentral Glip platform, please visit https://developer.ringcentral.com/ and SignUp.

Please feel free to leave back your questions and comments. You can also use Microsoft botbuilder framework to build bots on other platforms.

--

--

Dibyendu Roy
RingCentral Developers

Sr Product Manager RingCentral, passionate about technology and building products that can bring a step function change, improving peoples lives significantly