Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. var prompt = require('prompt-sync')();
  2. var ConversationV1 = require('watson-developer-cloud/conversation/v1');
  3. var conversation = new ConversationV1({
  4. username: 'USERNAME',
  5. password: 'PASSWORD',
  6. path: { workspace_id: 'WORKSPACE_ID' },
  7. version_date: '2016-07-11'
  8. });
  9.  
  10. /**
  11. * Process the conversation response and decides if it needs to
  12. * continue the conversation or not based on the context flags.
  13. */
  14. function processMessage(err, response) {
  15. if (err) {
  16. console.error(err); // something went wrong
  17. return;
  18. }
  19.  
  20. var endConversation = false;
  21.  
  22. // If an intent was detected, log it out to the console.
  23. if (response.intents.length > 0) {
  24. console.log('Detected intent: #' + response.intents[0].intent);
  25. }
  26.  
  27. // Check for action flags.
  28. if (response.output.action === 'display_time') {
  29. // User asked what time it is, so we output the local system time.
  30. console.log('The current time is ' + new Date().toLocaleTimeString() + '.');
  31. } else if (response.output.action === 'end_conversation') {
  32. // User said goodbye, so we're done.
  33. console.log(response.output.text[0]);
  34. endConversation = true;
  35. } else {
  36. // Display the output from dialog, if any.
  37. console.log(response.output.text[0]);
  38. }
  39.  
  40. if (!endConversation) {
  41. // Prompt for the new user message
  42. var newMessageFromUser = prompt('Type a message: ');
  43. conversation.message({
  44. input: { text: newMessageFromUser },
  45. context : response.context, // keep the conversation context
  46. }, processMessage)
  47. }
  48. }
  49.  
  50. // Start conversation. This preloads the action flags in the context with
  51. // false, and then triggers the conversation_start node.
  52. var initialMessage = {
  53. context: {
  54. display_time: false,
  55. end_conversation: false
  56. }
  57. };
  58.  
  59. // send initial message
  60. conversation.message(initialMessage, processMessage);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement