Wa-automate-nodejs: Feature: Expose a basic express middleware from Client

Created on 22 May 2020  Â·  7Comments  Â·  Source: open-wa/wa-automate-nodejs

Implementing this library with express shouldn't be so complicated and verbose. A simple middleware should be able to manage the vast majority of functionality from the client. This feature needs to be implemented with minimal increased library footprint.

PRIORITY enhancement

Most helpful comment

How to use the middleware:

import { create } from '@open-wa/wa-automate';
const express = require('express')
const app = express()
app.use(express.json())
const PORT = 8082;

function start(client){
  app.use(client.middleware);
  app.listen(PORT, function () {
    console.log(`\n• Listening on port ${PORT}!`);
  });
  ...
}


create({
  sessionId:'session1'
}).then(start)

All requests need to be POST requests. You use the API the same way you would with client. The method can be the path or the method param in the post body. The arguments for the method should be properly ordered in the args array in the JSON post body.

Example:

  await client.sendText('[email protected]','test')
  //returns "[email protected]_3EB0645E623D91006252"

as a request with a path:

const axios = require('axios').default;
axios.post('localhost:8082/sendText', {
    args: [
       "[email protected]",    
       "test"    
        ]
  })

or as a request without a path:

const axios = require('axios').default;
axios.post('localhost:8082', {
    method:'sendText',
    args: [
       "[email protected]",    
       "test"   
        ]
})

All 7 comments

@github-actions run


âš¡ Release! âš¡

(async () => {
function exec(cmd) {
  console.log(execSync(cmd).toString());
}

// Config
const gitUserEmail = "github-actions[bot]@users.noreply.github.com";
const gitUserName = "github-actions[bot]";

exec(`echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc`);
exec(`git config --global user.email "${gitUserEmail}"`);
exec(`git config --global user.name "${gitUserName}"`);
exec(`npm i`);
exec(`npm run release-ci`);

//comment on the issue
var result = execSync(`npx auto-changelog -o ./tempchangelog.txt --commit-limit false --template ./compact-keepachangelog.hbs --stdout`).toString();

    await postComment(result);

//create changelog image
exec(`npm run release-image`);
exec(`git commit -a -m 'updated release-image'`);
exec(`git push --force`);
  })();

Changelog

🚀 Release 1.9.37 (2020-05-22)

  • Release 1.9.37 f6e11c8
  • added docs for middleware #466 e61c8c2
  • feature: client exposes basic express middleware #466 880091e
  • added express middleware example demo #466 63cc3bc

How to use the middleware:

import { create } from '@open-wa/wa-automate';
const express = require('express')
const app = express()
app.use(express.json())
const PORT = 8082;

function start(client){
  app.use(client.middleware);
  app.listen(PORT, function () {
    console.log(`\n• Listening on port ${PORT}!`);
  });
  ...
}


create({
  sessionId:'session1'
}).then(start)

All requests need to be POST requests. You use the API the same way you would with client. The method can be the path or the method param in the post body. The arguments for the method should be properly ordered in the args array in the JSON post body.

Example:

  await client.sendText('[email protected]','test')
  //returns "[email protected]_3EB0645E623D91006252"

as a request with a path:

const axios = require('axios').default;
axios.post('localhost:8082/sendText', {
    args: [
       "[email protected]",    
       "test"    
        ]
  })

or as a request without a path:

const axios = require('axios').default;
axios.post('localhost:8082', {
    method:'sendText',
    args: [
       "[email protected]",    
       "test"   
        ]
})

Thanks for your incredible work and superb documentation. I got this to work very easily with your help.
I will continue to explore it.

Thanks

Hello,
I cant seem to send an image. I am wsing the latest version of the library and was able to get most things to work except sending of files.
it usually returns b64Data.split is not a function among other errors. My simple axios request is as follows

const file = 'iVBORw...cx';
const axios = require('axios').default;
axios.post('http://localhost:8082/sendFile', {
    args: [
        "[email protected]",
        [file],
        'ing.png',
        'ntesho!'
    ]
}).then((res) => console.log(res.data))
    .catch(err => console.log(err))

Thanks once again for your incredible work.

afaik you need to send the base64 data directly. Here is how i send it (not with the REST api, mind you)

client.sendImage(
          to,
          `data:${mime.lookup(FILE)};base64,${fs.readFileSync(FILE).toString('base64')}`,
           FILE.substring(FILE.lastIndexOf('/') + 1).trim(),
          caption
        );

Here i am using the libs mime and fs in node.
mime to lookup the mimetype and then fs to open and read the FILE.

Note that FILE is a filepath

Oh. Thanks. I will try that in a minute

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jazZcarabazZ picture jazZcarabazZ  Â·  5Comments

DeLuca92 picture DeLuca92  Â·  5Comments

arisros picture arisros  Â·  4Comments

mrbarnk picture mrbarnk  Â·  5Comments

sandro-salles picture sandro-salles  Â·  4Comments