lilos404

Untitled

May 1st, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. 'use strict';
  2.  
  3. let Wit = null;
  4. let interactive = null;
  5. try {
  6. // if running from repo
  7. Wit = require('../').Wit;
  8. interactive = require('../').interactive;
  9. } catch (e) {
  10. Wit = require('node-wit').Wit;
  11. interactive = require('node-wit').interactive;
  12. }
  13.  
  14. const firstEntityValue = (entities, entity) => {
  15. const val = entities && entities[entity] &&
  16. Array.isArray(entities[entity]) &&
  17. entities[entity].length > 0 &&
  18. entities[entity][0].value
  19. ;
  20. if (!val) {
  21. return null;
  22. }
  23. return typeof val === 'object' ? val.value : val;
  24. };
  25.  
  26.  
  27. const accessToken = (() => {
  28. if (process.argv.length !== 3) {
  29. console.log('usage: node examples/basic.js <wit-access-token>');
  30. process.exit(1);
  31. }
  32. return process.argv[2];
  33. })();
  34.  
  35. const actions = {
  36. send(request, response) {
  37. const {sessionId, context, entities} = request;
  38. const {text, quickreplies} = response;
  39. console.log('user said...', request.text);
  40. console.log('sending...', JSON.stringify(response));
  41. },
  42. getPrayerTimes({context, entities}) {
  43.  
  44. var location = firstEntityValue(entities, 'location');
  45.  
  46.  
  47. if (location) {
  48. console.log(location);
  49.  
  50.  
  51. var request = require("request")
  52.  
  53. var url = "http://muslimsalat.com/london/daily.json?key=16a741273a18579bdb7abdbae61b46d6&jsoncallback=?"
  54.  
  55. request({
  56. url: url,
  57. json: true
  58. }, function (error, response, body) {
  59.  
  60. if (!error && response.statusCode === 200) {
  61. console.log(body) // Print the json response
  62. }
  63. })
  64. console.log(location);
  65.  
  66. // context.forecast = 'sunny in ' + location; // we should call a weather API here
  67.  
  68. delete context.missingLocation;
  69. } else {
  70. context.missingLocation = true;
  71. delete context.forecast;
  72. }
  73. return context;
  74. },
  75. };
  76.  
  77. const client = new Wit({accessToken, actions});
  78.  
  79.  
  80.  
  81. interactive(client);
Add Comment
Please, Sign In to add comment