This is a WhatsApp Web bot customized. It uses the library whatsapp-web.js and is easy to run.
Clone this repository and do:
npm install
You have to consider that the session file is set in ~/wwbot-session.json
by default, but you can change this via environment variables WWBOT_SESSION
.
After you can start easily with:
npm start
# Or
WWBOT_SESSION=/home/user/dir/to/session.json npm start
Or if you use pm2 then do:
pm2 start ecosystem.config.js
If you use pm2 you can edit the environment variable and set your preferences. Edit the file ecosystem.config.js
:
{
apps: [
{
...
env_production: {
WWBOT_SESSION: '~/wwbot-session.json',
},
},
],
};
In src/handlers
you can add news modules to extend the behavior of the bot. For this, you must define a class that extends from BaseHandler
(in src/handler.js
) and override the method onMessage(message)
where message
is a Message object and export it. Example:
const { BaseHandler } = require('../handler');
class Handler extends BaseHandler {
constructor() {
super('name-my-handler');
this.command = '!echo';
}
onMessage(message) {
if (message.body.startsWith(this.command)) {
const text = message.body.slice(this.command.length, message.body.length);
message.reply(text);
}
}
}
module.exports = Handler;
When the client is running a web server start listening on port defined by PORT
environment variable or 3000.
sendMessage:
param name | type | detail |
---|---|---|
chatId | string | The chat group. This value can be gotten in Message.from . |
text | string | The text to send. |
Example:
curl "http://localhost:3000/sendMessage?chatId=chatid-here&text=ctm"