Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. // Example 4: implements app actions.
  2.  
  3. var prompt = require('prompt-sync')();
  4. var ConversationV1 = require('watson-developer-cloud/conversation/v1');
  5.  
  6. // Set up Conversation service.
  7. var conversation = new ConversationV1({
  8. username: '05f5fc2e-a0a6-4264-8463-f0d3bfba6b55', // replace with username from service key
  9. password: 'tp4IlCiv8jI2', // replace with password from service key
  10. path: { workspace_id: 'f84b28e4-3ef2-4b5a-9908-7e47ecfbdfb2' }, // replace with workspace ID
  11. version_date: '2017-05-26'
  12. });
  13.  
  14. // Start conversation with empty message.
  15. conversation.message({}, processResponse);
  16.  
  17. // Process the conversation response.
  18. function processResponse(err, response) {
  19. if (err) {
  20. console.error(err); // something went wrong
  21. return;
  22. }
  23.  
  24. var endConversation = false;
  25.  
  26. // Check for action flags.
  27. if (response.output.action === 'display_time') {
  28. // User asked what time it is, so we output the local system time.
  29. console.log('The current time is ' + new Date().toLocaleTimeString());
  30. } else if (response.output.action === 'end_conversation') {
  31. // User said goodbye, so we're done.
  32. console.log(response.output.text[0]);
  33. endConversation = true;
  34. } else {
  35. // Display the output from dialog, if any.
  36. if (response.output.text.length != 0) {
  37. console.log(response.output.text[0]);
  38. }
  39. }
  40.  
  41. // If we're not done, prompt for the next round of input.
  42. if (!endConversation) {
  43. var newMessageFromUser = prompt('>> ');
  44. conversation.message({
  45. input: { text: newMessageFromUser },
  46. // Send back the context to maintain state.
  47. context : response.context,
  48. }, processResponse)
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement