Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example 4: implements app actions.
- var prompt = require('prompt-sync')();
- var ConversationV1 = require('watson-developer-cloud/conversation/v1');
- // Set up Conversation service.
- var conversation = new ConversationV1({
- username: '05f5fc2e-a0a6-4264-8463-f0d3bfba6b55', // replace with username from service key
- password: 'tp4IlCiv8jI2', // replace with password from service key
- path: { workspace_id: 'f84b28e4-3ef2-4b5a-9908-7e47ecfbdfb2' }, // replace with workspace ID
- version_date: '2017-05-26'
- });
- // Start conversation with empty message.
- conversation.message({}, processResponse);
- // Process the conversation response.
- function processResponse(err, response) {
- if (err) {
- console.error(err); // something went wrong
- return;
- }
- var endConversation = false;
- // Check for action flags.
- if (response.output.action === 'display_time') {
- // User asked what time it is, so we output the local system time.
- console.log('The current time is ' + new Date().toLocaleTimeString());
- } else if (response.output.action === 'end_conversation') {
- // User said goodbye, so we're done.
- console.log(response.output.text[0]);
- endConversation = true;
- } else {
- // Display the output from dialog, if any.
- if (response.output.text.length != 0) {
- console.log(response.output.text[0]);
- }
- }
- // If we're not done, prompt for the next round of input.
- if (!endConversation) {
- var newMessageFromUser = prompt('>> ');
- conversation.message({
- input: { text: newMessageFromUser },
- // Send back the context to maintain state.
- context : response.context,
- }, processResponse)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement