agunq

wa-js-part2

Jul 23rd, 2021 (edited)
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Client,  MessageMedia } = require('whatsapp-web.js');
  2. var qrcode = require('qrcode-terminal');
  3. const axios = require('axios');
  4. const Scraper = require('@yimura/scraper').default;
  5. const youtube = new Scraper();
  6.  
  7. function downloadGambar(url){
  8.     return axios.get(url, {responseType: 'arraybuffer'})
  9.         .then(response => Buffer.from(response.data, "binary").toString("base64"))
  10. }
  11.  
  12. const client = new Client();
  13.  
  14.  
  15. client.on('authenticated', (session) => {
  16.     console.log('AUTHENTICATED', session);
  17. });
  18.  
  19.  
  20. client.on('qr', (qr) => {
  21.         // Generate and scan this code with your phone
  22.         console.log('QR RECEIVED', qr);
  23.  
  24.     qrcode.generate(qr, {small: true}, function (qrcode) {
  25.             console.log(qrcode)
  26.     });
  27.  
  28. });
  29.  
  30. client.on('ready', () => {
  31.     console.log('Client is ready!');
  32. });
  33.  
  34. const prefix = "!";
  35.  
  36. client.on('message', async msg => {
  37.         if (msg.body == '!ping') {
  38.             msg.reply('pong');
  39.         }
  40.  
  41.     if (msg.body == "selamat pagi") {
  42.         client.sendMessage(msg.from, 'selamat pagi juga');
  43.     }
  44.  
  45.     if (msg.body[0] == prefix){
  46.         var [cmd, ...args] = msg.body.slice(1).split(" ");
  47.         args = args.join(" ");
  48.  
  49.         if (cmd == "say"){
  50.             client.sendMessage(msg.from, args);
  51.         }
  52.  
  53.         if (cmd == "gambar"){
  54.             const media = MessageMedia.fromFilePath('./anime/anime-cry.gif');
  55.             client.sendMessage(msg.from, media);
  56.         }
  57.         if (cmd == "kanata"){
  58.             //https://i.waifu.pics/sS-rREz.jpg
  59.             const gambar = await downloadGambar("https://i.waifu.pics/sS-rREz.jpg");
  60.             const media = new MessageMedia('image/jpg', gambar);
  61.             client.sendMessage(msg.from, media);
  62.         }
  63.  
  64.         if (cmd == "yt" || cmd == "youtube"){
  65.             youtube.search(args).then(results => {
  66.                     //console.log(results.videos[0]);
  67.                 client.sendMessage(msg.from,  results.videos[0].description + " " + results.videos[0].link );
  68.             });
  69.         }
  70.  
  71.     }
  72.    
  73.  
  74. });
  75.  
  76. client.initialize();
Add Comment
Please, Sign In to add comment