daily pastebin goal
17%
SHARE
TWEET

Untitled

a guest Jun 20th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Reference the packages we require so that we can use them in creating the bot
  2. var restify = require('restify');
  3. var builder = require('botbuilder');
  4. var rp = require('request-promise');
  5.  
  6. // Static variables that we can use anywhere in app.js
  7. var BINGNEWSKEY = '318f1f213c3d41f98b3a320a411ec8b9';
  8. //=========================================================
  9. // Bot Setup
  10. //=========================================================
  11.  
  12. // Setup Restify Server
  13. // Listen for any activity on port 3978 of our local server
  14. var server = restify.createServer();
  15. server.listen(process.env.port || process.env.PORT || 3978, function () {
  16.     console.log('%s listening to %s', server.name, server.url);
  17. });
  18.  
  19. // Create chat bot
  20. var connector = new builder.ChatConnector({
  21.     appId: process.env.MICROSOFT_APP_ID,
  22.     appPassword: process.env.MICROSOFT_APP_PASSWORD
  23. });
  24. var bot = new builder.UniversalBot(connector);
  25. // If a Post request is made to /api/messages on port 3978 of our local server, then we pass it to the bot connector to handle
  26. server.post('/api/messages', connector.listen());
  27.  
  28.  
  29. //=========================================================
  30. // Bots Dialogs
  31. //=========================================================
  32.  
  33. //This is called the root dialog. it is the first point of entry for any message the bot receives.
  34.  
  35. var luisRecognizer = new builder.LuisRecognizer('https://westeurope.api.cognitive.microsoft.com/luis/v2.0/apps/ab2e52b5-4d5a-4883-9016-1f9c004999d0?subscription-key=5c9af06590e743bcb692e917eb0fe310&verbose=true&timezoneOffset=0&q=');
  36. var intentDialog = new builder.IntentDialog({ recognizers: [luisRecognizer] });
  37.  
  38. intentDialog.matches('GetNews', '/getNews')
  39.     .matches('AnalyzeImage', '/analyzeImage')
  40.     .onDefault(builder.DialogAction.send("Sorry, I didn't understand what you said."));
  41.  
  42.  
  43. // This function processes the results from the API call to category news and sends it as cards
  44. function sendTopNews(session, results, body) {
  45.     session.send("Top news in " + results.response.entity + ": ");
  46.     //Show user that we're processing by sending the typing indicator
  47.     session.sendTyping();
  48.     // The value property in body contains an array of all the returned articles
  49.     var allArticles = body.value;
  50.     var cards = [];
  51.     // Iterate through all 10 articles returned by the API
  52.     for (var i = 0; i < 10; i++) {
  53.         var article = allArticles[i];
  54.         // Create a card for the article and add it to the list of cards we want to send
  55.         cards.push(new builder.HeroCard(session)
  56.             .title(article.name)
  57.             .subtitle(article.datePublished)
  58.             .images([
  59.                 //handle if thumbnail is empty
  60.                 builder.CardImage.create(session, article.image.contentUrl)
  61.             ])
  62.             .buttons([
  63.                 // Pressing this button opens a url to the actual article
  64.                 builder.CardAction.openUrl(session, article.url, "Full article")
  65.             ]));
  66.     }
  67.     var msg = new builder.Message(session)
  68.         .textFormat(builder.TextFormat.xml)
  69.         .attachmentLayout(builder.AttachmentLayout.carousel)
  70.         .attachments(cards);
  71.     session.send(msg);
  72. }
  73.  
  74.  
  75.  
  76. bot.dialog('/', intentDialog);
  77.  
  78.  
  79. bot.dialog('/getNews', [
  80.     function (session) {
  81.         // Ask the user which category they would like
  82.         // Choices are separated by |
  83.         builder.Prompts.choice(session, "Which category would you like?", "Technology|Science|Sports|Business|Entertainment|Politics|Health|World|(quit)");
  84.     }, function (session, results, next) {
  85.         // The user chose a category
  86.         if (results.response && results.response.entity !== '(quit)') {
  87.             //Show user that we're processing their request by sending the typing indicator
  88.             session.sendTyping();
  89.             // Build the url we'll be calling to get top news
  90.             var url = "https://api.cognitive.microsoft.com/bing/v7.0/news/?"
  91.                 + "category=" + results.response.entity + "&count=10&mkt=en-US&originalImg=true";
  92.             // Build options for the request
  93.             var options = {
  94.                 uri: url,
  95.                 headers: {
  96.                     'Ocp-Apim-Subscription-Key': BINGNEWSKEY
  97.                 },
  98.                 json: true // Returns the response in json
  99.             }
  100.             //Make the call
  101.             rp(options).then(function (body) {
  102.                 // The request is successful
  103.                 sendTopNews(session, results, body);
  104.             }).catch(function (err) {
  105.                 // An error occurred and the request failed
  106.                 console.log(err.message);
  107.                 session.send("Argh, something went wrong. :( Try again?");
  108.             }).finally(function () {
  109.                 // This is executed at the end, regardless of whether the request is successful or not
  110.                 session.endDialog();
  111.             });
  112.         } else {
  113.             // The user choses to quit
  114.             session.endDialog("Ok. Mission Aborted.");
  115.         }
  116.     }
  117. ]);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top