Advertisement
Guest User

Untitled

a guest
Jan 8th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. var dialogflow = require('dialogflow');
  2. var nodemailer = require('nodemailer');
  3.  
  4. var mailPassword = 'ksystem15';
  5. var mailSMTPSever = 'mail.cloud13.de';
  6. var transporter = nodemailer.createTransport({
  7. host: mailSMTPSever,
  8. port: 587,
  9. secure: false,
  10. auth: {
  11. user: mailAccountName,
  12. pass: mailPassword
  13. },
  14. tls: {
  15. // do not fail on invalid certs
  16. rejectUnauthorized: false
  17. }
  18. });
  19.  
  20. //Google
  21. var sessionId = 1;
  22. const projectId = 'accelerator-chatbot';
  23. const languageCode = 'de-DE';
  24.  
  25. const chatBotError = "\n WARNING: Chatbot not working because no GOOGLE_APPLICATION_CREDENTIALS env variable set! To fix it, run this in your command line:\n Win: set GOOGLE_APPLICATION_CREDENTIALS=Accelerator-Chatbot-b863bdff37fd.json\n Linux: GOOGLE_APPLICATION_CREDENTIALS=Accelerator-Chatbot-b863bdff37fd.json\n";
  26. if(!process.env.GOOGLE_APPLICATION_CREDENTIALS) {
  27. console.log(chatBotError);
  28. }
  29.  
  30. module.exports = {
  31. chat1 : function(msg, username, callback) {
  32. if(!process.env.GOOGLE_APPLICATION_CREDENTIALS) {
  33. console.log(chatBotError);
  34. return;
  35. }
  36. chtext = msg.split("/cb ")[1];
  37. if(chtext && chtext != "") {
  38. var sessionId = username+(sessionId++);
  39. const sessionClient = new dialogflow.SessionsClient();
  40. var session = sessionClient.sessionPath(projectId, sessionId);
  41.  
  42. // The text query request.
  43. const request = {
  44. session: session,
  45. queryInput: {
  46. text: {
  47. text: chtext,
  48. languageCode: languageCode,
  49. },
  50. },
  51. };
  52.  
  53. // Send request and log result
  54. sessionClient
  55. .detectIntent(request)
  56. .then(responses => {
  57. console.log('Detected intent');
  58. const result = responses[0].queryResult;
  59. console.log(` Query: ${result.queryText}`);
  60. console.log(` Response: ${result.fulfillmentText}`);
  61. if(result.intent.displayName=="Email...") {
  62. transporter.sendMail({
  63. from: mailAccountName,
  64. to: chtext.split("/cb ")[1],
  65. subject: "Accelerator Invitation",
  66. text: SENDMESSAGETEXT
  67. },
  68. function(err,response){
  69. if(err){
  70. callback("ERROR","chatbot", username, result.intent.displayName);
  71. }
  72. callback(result.fulfillmentText,"chatbot", username, result.intent.displayName);
  73. });
  74. } else {
  75. callback(result.fulfillmentText,"chatbot", username, result.intent.displayName);
  76. }
  77.  
  78. if (result.intent) {
  79. console.log(` Intent: ${result.intent.displayName}`);
  80. } else {
  81. console.log(` No intent matched.`);
  82. }
  83. })
  84. .catch(err => {
  85. console.error('ERROR:', err);
  86. });
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement