Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const {
  2.     BotFrameworkAdapter,
  3.     MemoryStorage,
  4.     ConversationState
  5. } = require('botbuilder');
  6. const botbuilder_dialogs = require('botbuilder-dialogs');
  7. const restify = require('restify');
  8.  
  9. const server = restify.createServer();
  10. const dialogs = new botbuilder_dialogs.DialogSet();
  11.  
  12. const adapter = new BotFrameworkAdapter({
  13.     appId: '', //process.env.MICROSOFT_APP_ID,
  14.     appPassword: '', //process.env.MICROSOFT_APP_PASSWORD
  15. });
  16.  
  17. const conversationState = new ConversationState(new MemoryStorage());
  18.  
  19. adapter.use(conversationState);
  20.  
  21. var reservationInfo = {};
  22.  
  23. dialogs.add('greetings', [
  24.     async function (dc) {
  25.             await dc.prompt('textPrompt', 'What is your name?');
  26.         },
  27.         async function (dc, results) {
  28.             var userName = results;
  29.             await dc.context.sendActivity(`Hello ${userName}!`);
  30.             await dc.end(); // Ends the dialog
  31.         }
  32. ]);
  33.  
  34. dialogs.add('reserveTable', [
  35.     async function(dc, args, next){
  36.         await dc.context.sendActivity("Welcome to the reservation service.");
  37.  
  38.         reservationInfo = {}; // Clears any previous data
  39.         await dc.prompt('dateTimePrompt', "Please provide a reservation date and time.");
  40.     },
  41.     async function(dc, result){
  42.         reservationInfo.dateTime = result[0].value;
  43.  
  44.         // Ask for next info
  45.         await dc.prompt('partySizePrompt', "How many people are in your party?");
  46.     },
  47.     async function(dc, result){
  48.         reservationInfo.partySize = result;
  49.  
  50.         // Ask for next info
  51.         await dc.prompt('textPrompt', "Whose name will this be under?");
  52.     },
  53.     async function(dc, result){
  54.         reservationInfo.reserveName = result;
  55.        
  56.         // Reservation confirmation
  57.         var msg = `Reservation confirmed. Reservation details:
  58.             <br/>Date/Time: ${reservationInfo.dateTime}
  59.             <br/>Party size: ${reservationInfo.partySize}
  60.             <br/>Reservation name: ${reservationInfo.reserveName}`;
  61.         await dc.context.sendActivity(msg);
  62.         await dc.end();
  63.     }
  64. ]);
  65.  
  66. dialogs.add('textPrompt', new botbuilder_dialogs.TextPrompt());
  67. dialogs.add('dateTimePrompt', new botbuilder_dialogs.DatetimePrompt());
  68. dialogs.add('partySizePrompt', new botbuilder_dialogs.NumberPrompt());
  69.  
  70. // Listen for incoming activity
  71. server.post('/api/messages', (req, res) => {
  72.     adapter.processActivity(req, res, async (context) => {
  73.         const isMessage = (context.activity.type === 'message');
  74.         // State will store all of your information
  75.         const convo = conversationState.get(context);
  76.         const dc = dialogs.createContext(context, convo);
  77.  
  78.         if (isMessage) {
  79.             // Check for valid intents
  80.             if(context.activity.text.match(/hi/ig)){
  81.                 await dc.begin('greetings');
  82.             }
  83.             else if(context.activity.text.match(/reserve table/ig)){
  84.                 await dc.begin('reserveTable');
  85.             }
  86.         }
  87.  
  88.         if(!context.responded){
  89.             // Continue executing the "current" dialog, if any.
  90.             await dc.continue();
  91.  
  92.             if(!context.responded && isMessage){
  93.                 // Default message
  94.                 await context.sendActivity("Hi! I'm a simple bot. Please say 'Hi' or 'Reserve table'.");
  95.             }
  96.         }
  97.     });
  98. });
  99.  
  100. // Start server
  101. server.listen(process.env.port || process.env.PORT || 3978, function () {
  102.     console.log(`${server.name} listening to ${server.url}`);
  103. });
  104.  
  105.  
  106. /**
  107. // Listen for incoming requests
  108. server.post('/api/messages', (req, res) => {
  109.  
  110.     // Route received request to adapter for processing
  111.     adapter.processActivity(req, res, (context) => {
  112.  
  113.         if (context.activity.type === 'message') {
  114.  
  115.             let state = conversationState.get(context);
  116.             let count = state.count === undefined ? state.count = 0 : ++state.count;
  117.  
  118.             return context.sendActivity(`"[Message (${count})]: ${context.activity.text}"`); // message count is available
  119.  
  120.         } else {
  121.             return context.sendActivity(`"[Event]: ${context.activity.type}"`);
  122.         }
  123.  
  124.     });
  125.  
  126. });
  127.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement