SHARE
TWEET

Untitled

a guest Aug 18th, 2018 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Example 1: sets up service wrapper, sends initial message, and
  2. // receives response.
  3. var prompt = require('prompt-sync')();
  4. var AssistantV1 = require('watson-developer-cloud/assistant/v1');
  5. var fs = require('fs');
  6.  
  7.  
  8. // Set up Assistant service wrapper.
  9. var service = new AssistantV1({
  10.   username: '1a4304ce-e595-4dad-8dc9-e69c4af8d4bb', // replace with service username
  11.   password: 'iSoVPwQDdYYR', // replace with service password
  12.   version: '2018-02-16'
  13. });
  14.  
  15. var workspace_id = 'd048be97-c7b6-4718-8b95-7b087125512f'; // replace with workspace ID
  16.  
  17. var event, day, time, ev_id, date;
  18.  
  19. // Start conversation with empty message.
  20. service.message({
  21.   workspace_id: workspace_id
  22.   }, processResponse);
  23.  
  24. // Process the service response.
  25. function processResponse(err, response) {
  26.   if (err) {
  27.     console.error(err); // something went wrong
  28.     return;
  29.   }
  30.  
  31. //read config file of Json DB
  32. var data = fs.readFileSync('./db.json'),
  33. db;
  34. db = JSON.parse(data);
  35.  
  36. var endConversation = false;
  37.  
  38. // If an intent was detected, log it out to the console.
  39. if (response.intents.length > 0) {
  40.         console.log('Detected intent: #' + response.intents[0].intent);
  41.     }
  42.  
  43. if (response.entities.length > 0) {
  44.         console.log('Detected entity: @' + response.entities[0].entity);
  45.         if(response.entities[0].entity === 'event_type'){
  46.         event = response.context.event;
  47.         console.log(event)
  48.         }
  49.      /*   if(response.entities[0].entity === 'rehabilitation'){
  50.           event = response.context.part+' (rehabilitation)';
  51.           console.log(event);
  52.         }
  53.        */
  54.  
  55.     }
  56.  
  57.   // Check for action flags.
  58.   /*if (response.output.action === 'display_time') {
  59.     // User asked what time it is, so we output the local system time.
  60.     console.log('The current time is ' + new Date().toLocaleTimeString() + '.');
  61.   } */
  62.   /*if(response.entities[0].entity==true){
  63.     console.log(response.output.text[0]);
  64.   }*/
  65.  
  66.     if (response.output.action === 'date_trigger') {
  67.  
  68.     // User asked what time it is, so we output the local system time.
  69.     console.log('Id_chosen:'+response.input.text);
  70.     date = response.input.text;
  71.     console.log(date);
  72.  
  73.     //control of duplicates of id value
  74.     for (i = 0; i < db.Events.length; i++){
  75.  
  76.       if (date === db.Events[i].Event_id){
  77.       console.log('Done... This is your event:');
  78.       console.log('Event Id: '+db.Events[i].Event_id);
  79.       console.log('Event Type: '+db.Events[i].Event_type);
  80.       console.log('Day: '+db.Events[i].day);
  81.       console.log('Time: '+db.Events[i].time);
  82.       break;
  83.  
  84.       }
  85.     }
  86.     if(i===db.Events.length){
  87.     console.log('Id not found, please check it better!');
  88.     }
  89.  
  90.   }
  91.  
  92.   if (response.output.action === 'days_trigger') {
  93.     // User asked what time it is, so we output the local system time.
  94.     console.log('Day:'+response.context.day);
  95.     console.log('Please, digit the time. (format must be HH:MM)');
  96.     day = response.context.day;
  97.     console.log(day);
  98.  
  99.   }
  100.   if (response.output.action === 'time_trigger') {
  101.  
  102.     // User asked what time it is, so we output the local system time.
  103.     console.log('Time:'+response.context.time);
  104.     time = response.context.time;
  105.     ev_id = (Math.floor(Math.random() * 1000) + 1).toString();
  106.     console.log(time);
  107.    
  108.     //control of same exam at same time
  109.     for (j = 0; j < db.Events.length; j++){
  110.  
  111.       if ((event === db.Events[j].Event_type ) && day === db.Events[j].day && time === db.Events[j].time){
  112.        
  113.       console.log('There is already a date for this date. Try to change it. (We receive from 09:00 to 12:00 and from 16:00 to 19:00)');
  114.       flag = 1;
  115.       break;
  116.  
  117.       }
  118.     }
  119.  
  120.     if(flag===0){
  121.     console.log('Event Id: '+ev_id);
  122.     console.log('Congratulation, you have ordered '+event+ ' event on '+ day+ ' at '+ time);
  123.  
  124.     //control of duplicates of id value
  125.     for (i = 0; i < db.Events.length; i++){
  126.  
  127.       if (ev_id === db.Events[i].Event_id){
  128.       ev_id= (Math.floor(Math.random() * 1000) + 1).toString();
  129.       i=0;
  130.  
  131.       }
  132.     }
  133.    
  134.  
  135.     db['Events'].push(
  136.       {
  137.         "Event_id": ev_id,
  138.         "Event_type": event,
  139.         "day": day,
  140.         "time": time
  141.           }
  142.   )
  143.  
  144.   var json = JSON.stringify(db);
  145.   fs.writeFile('./db.json', json, 'utf8');
  146.    
  147. }
  148.   }
  149.   if (response.output.action === 'end_conversation') {
  150.     // User said goodbye, so we're done.
  151.     //console.log(response.output.text[0]);
  152.     endConversation = true;
  153.   }
  154.   // Display the output from dialog, if any.
  155.   if (response.output.text.length != 0) {
  156.       console.log(response.output.text[0]);
  157.   }
  158.  
  159.  
  160.  
  161. if (!endConversation) {
  162. flag=0;
  163. // Prompt for the next round of input.
  164. var newMessageFromUser = prompt('>> ');
  165. service.message({
  166.     workspace_id: workspace_id,
  167.     input: { text: newMessageFromUser },
  168.     // Send back the context to maintain state.
  169.     context: response.context,
  170.     }, processResponse)
  171. }
  172. }
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