yayan 1 year ago
parent
commit
8e64f95571
2 changed files with 10 additions and 8 deletions
  1. 9 7
      main.js
  2. 1 1
      whatsapp-client.js

+ 9 - 7
main.js

@@ -67,9 +67,9 @@ fs.readdir('.wwebjs_auth', (err, files) => {
 const app = express();
 app.use(express.json({ limit: '50mb' }));
 app.get("/api/qr/:be", (req, res) => {
+    console.log(`params:${req.params.be}`);
+    const be = req.params.be;
     try {
-        console.log(`params:${req.params.be}`);
-        const be = req.params.be;
         const client = map.get(be);
         if (client != null) {
             res.json({ status: '00', be: be });
@@ -84,12 +84,13 @@ app.get("/api/qr/:be", (req, res) => {
         createWa(be, res);
     } catch (error) {
         console.log(`error:${error}`);
+        res.json({ status: '99', be: be, description: 'General error' });
     }
 });
 app.post("/api/send/:be", async (req, res) => {
+    console.log(`params:${req.params.be}, body:${JSON.stringify(req.body)}`);
+    const be = req.params.be;
     try {
-        console.log(`params:${req.params.be}, body:${JSON.stringify(req.body)}`);
-        const be = req.params.be;
         const client = map.get(be);
         if (client == null) {
             res.json({ status: '01', be: be, description: 'Unauthorize access' });
@@ -103,18 +104,19 @@ app.post("/api/send/:be", async (req, res) => {
             const data = req.body.message.data;
             const caption = req.body.message.caption;
             let messageMedia = new MessageMedia(media, data, fileName);
-            to.forEach( it => {
+            to.forEach(it => {
                 client.sendMessage(`${it}@c.us`, messageMedia, { caption: caption });
             });
             res.json({ status: '00', be: be });
             return;
         }
-        to.forEach( it => {
-            client.sendMessage(`${it}@c.us`, messageMedia, { caption: caption });
+        to.forEach(it => {
+            client.sendMessage(`${it}@c.us`, message);
         });
         res.json({ status: '00', be: be });
     } catch (error) {
         console.log(`error:${error}`);
+        res.json({ status: '99', be: be, description: 'General error' });
     }
 });
 const port = process.env.PORT || 9009;

+ 1 - 1
whatsapp-client.js

@@ -22,7 +22,7 @@ class WhasAppClient {
 
         let that = this.client;
         this.client.on('message', async (msg) => {
-            console.log(`be:${this.clientId}:message:${JSON.stringify(msg)}`);
+            // console.log(`be:${this.clientId}:message:${JSON.stringify(msg)}`);
             if (msg.body.startsWith('!bot ')) {
                 let message = msg.body.slice(5, msg.body.length);
                 let chat = await msg.getChat();