Advertisement
agunq

wa-js-part3

Aug 8th, 2022 (edited)
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Client, LocalAuth } = require('whatsapp-web.js');
  2.  
  3. const dialogflow = require('@google-cloud/dialogflow');
  4. const uuid = require('uuid');
  5.  
  6. const projectId = 'small-talk-xpck';
  7. const sessionId = uuid.v4();
  8.  
  9. const sessionClient = new dialogflow.SessionsClient({
  10.     keyFilename: "small-talk-xpck-1022d41d9e18.json"
  11. });
  12.  
  13.  
  14. const sessionPath = sessionClient.projectAgentSessionPath(
  15.     projectId,
  16.     sessionId
  17. );
  18.  
  19. async function Chatting(inputText) {
  20. const request = {
  21.     session: sessionPath,
  22.     queryInput: {
  23.       text: {
  24.         // The query to send to the dialogflow agent
  25.         text: inputText,
  26.         // The language used by the client (en-US)
  27.         languageCode: 'id-ID',
  28.       },
  29.     },
  30.   };
  31.  
  32.   // Send request and log result
  33.   const responses = await sessionClient.detectIntent(request);
  34.   console.log('Detected intent');
  35.   const result = responses[0].queryResult;
  36.   return result.fulfillmentText;
  37.  
  38. }
  39.  
  40.  
  41. const client = new Client({
  42.     authStrategy: new LocalAuth(),
  43.     puppeteer: { headless: false }
  44.  
  45. });
  46.  
  47. client.on('qr', (qr) => {
  48.     // Generate and scan this code with your phone
  49.     console.log('QR RECEIVED', qr);
  50. });
  51.  
  52. client.on('ready', () => {
  53.     console.log('Client is ready!');
  54. });
  55.  
  56. client.on('message', async msg => {
  57.     try {
  58.         //msg.reply( await Chatting(msg.body))
  59.         client.sendMessage(msg.from, await Chatting(msg.body));
  60.     }
  61.     catch(err) {
  62.         console.log("opsie, " + err.message)
  63.     }
  64. });
  65.  
  66. client.initialize();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement