Status repository
Our online channels
Welcome to Hydra Bot
This project was developed to help the community that uses whatsapp as a way to implement an API quickly and effectively, for companies and much more! Thank you for being part of this family.
You can use this project in two ways, the first is using Web Services using a set of well-defined operations, the POST, GET, PUT and DELETE methods, the second option is to use the bot in raw form, without using a Web Services.
Supporters
To maintain quality, we are receiving support! We thank you in advance for the opportunity to develop and maintain this project!
WhatSapp Group
Do you have any doubt? Need help? Join our whatsapp group and ask your questions with other people!
Installation
Use the stable version:
> npm i --save hydra-bot
or for Nightly releases:
> npm i --save https://github.com/jonalan7/Hydra-bot/releases/download/nightly/hydra-nightly.tgz
Terminal Admin:
> yarn admin
Install yarn Ubuntu:
> curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
> echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
> sudo apt update && sudo apt install yarn
> yarn
Getting started Web Service
The service will be started on localhost on port 8080
const hydraBot = require('hydra-bot');
(async () => {
const WS = await hydraBot.initWs();
})();
Getting started bot (the bot in raw form, without using a Web Services).
If you want to work in free mode, using only the bot, dry the necessary information!
const hydraBot = require('hydra-bot');
const mime = require('mime-types');
const fs = require('fs');
(async () => {
let client;
const ev = await hydraBot.initServer();
ev.on('interfaceChange', (change) => {
console.log('interfaceChange: ', change);
});
ev.on('qrcode', (qrcode) => {
console.log('qrcode: ', qrcode);
});
ev.on('connection', async (conn) => {
if (conn.statusFind === 'browser') {
console.log('info Browser: ', conn.text);
}
if (conn.connect) {
client = conn.client;
await client
.sendMessage({
to: '0000000000@c.us',
body: "hi i'm hydra bot",
options: {
type: 'sendText',
},
})
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
}
});
ev.on('newMessage', async (newMsg) => {
if (!newMsg.result.isSentByMe) {
console.log('NewMessageReceived: ', newMsg.result);
if (newMsg.result.isMedia === true || newMsg.result.isMMS === true) {
const buffer = await client.decryptFile(newMsg.result);
const fileName = `some-file-name.${mime.extension(
newMsg.result.mimetype
)}`;
fs.writeFile(fileName, buffer, (err) => {
if (err) {
console.log(err);
}
});
}
}
if (!!newMsg.result.isSentByMe) {
console.log('NewMessageSent: ', newMsg.result);
}
});
ev.on('newOnAck', async (event) => {
console.log('id Message: ', event.result.id._serialized);
console.log('Status Message: ', event.result.ack);
console.log('From Message: ', event.result.from);
console.log('To Message: ', event.result.to);
});
})();
Downloading Files
Puppeteer takes care of the file downloading. The decryption is being done as
fast as possible (outruns native methods). Supports big files!
const hydraBot = require('hydra-bot');
const fs = require('fs');
const mime = require('mime-types');
(async () => {
let client;
const ev = await hydraBot.initServer();
ev.on('connection', async (conn) => {
if (conn.connect) {
client = conn.client;
}
});
ev.on('newMessage', async (newMsg) => {
if (!newMsg.result.isSentByMe) {
console.log('NewMessageReceived: ', newMsg.result);
if (newMsg.result.isMedia === true || newMsg.result.isMMS === true) {
const buffer = await client.decryptFile(newMsg.result);
const fileName = `some-file-name.${mime.extension(
newMsg.result.mimetype
)}`;
fs.writeFile(fileName, buffer, (err) => {
if (err) {
console.log(err);
}
});
}
}
});
})();
Optional create parameters (the bot in raw form, without using a Web Services)
const hydraBot = require('hydra-bot');
hydraBot.initServer({
session: 'session',
pathNameToken: 'token',
printQRInTerminal: true,
updatesLog: true,
timeAutoClose: 60000,
mkdirFolderToken: '',
puppeteerOptions: {
headless: true,
args: [],
executablePath: 'useChrome',
},
});
Optional create parameters Web Services
const hydraBot = require('hydra-bot');
hydraBot.initWs({
port: '8080',
url: '',
authentication: true,
pathNameToken: 'token',
printQRInTerminal: true,
updatesLog: true,
timeAutoClose: 60000,
mkdirFolderToken: '',
puppeteerOptions: {
headless: true,
args: [],
executablePath: 'useChrome',
},
});
Commands for administration via terminal
To start the administration interface use:
> yarn admin
List of commands in the terminal:
Command | Description |
---|
/create | Create user |
/delete | Delete user |
/selectid | Show user by id |
/selectname | Select user by name |
/getall | List all users |
/deactivate | Disable user |
/activate | Activate User |
/changename | Change username |
/password | Change user password |
/cls | Clear screen/terminal |
/help | List all commands for administration in terminal |
/exit | Exit manager |
Routes for handling and querying users.
List of commands using REST API
{
"Content-Type": "application/json",
"admin": "admin",
"admin_pass": "admin"
}
List of routes for user management:
Type | Route to browser | Description | Body |
---|
POST | /create_user | Create user | {"name":"USE","password":"USER PASSWORD"} |
DEL | /delete_user/ID_USE | Delete user | EMPTY |
GET | /get_user_by_id/ID_USE | Show user by ID | EMPTY |
GET | /get_all_users | List all users | EMPTY |
PUT | /deactivate_user | Disable user | {"id":"USER ID"} |
PUT | /activate_user | Activate User | {"id":"USER ID"} |
PUT | /change_name | Change username | {"id":"USER ID","name":"NEW USERNAME"} |
PUT | /change_password | Change user password | {"id":"USER ID","password":"NEW SECURE PASSWORD"} |
Web Service Routes (more features still under development)
Note: Parameters can be changed during development!
List of routes for managing whatsapp:
The headers must be parameterized as :
{
"Content-Type": "application/json",
"user": "user",
"user_pass": "user"
}
Using Webhook
if you want to receive a callback on a specific url, pass the url parameter in the connect route.
Methods POST
Type | Route to browser | Description | Body |
---|
POST | /connect | Start connection with Whatsapp | { "url": "http://localhost:8080/webhooktest" } |
POST | /sendtext | Send a text to a number | { "to": "contact number", "body": "message"} |
POST | /sendFile | Send file to a number | { "to": "contact number", "file_path": "https://docs.marklogic.com/guide/node-dev.pdf", "file_name": "node.js" } |
POST | /sendAudio | Send audio | { "to": "contact number", "url_mp3": "https://cdn.freesound.org/previews/620/620094_4935038-lq.mp3", "file_name": "node.js" } |
POST | /sendImage | Send image message | { "to": "contact number", "url_img": "https://i.pinimg.com/564x/a9/b1/18/a9b118761788b1ab260aae2835c468cd.jpg" } |
POST | /disconnect | Disconnecting from the server | EMPTY |
POST | /check_user | Check if the entered user exists | EMPTY |
Methods GET
Type | Route to browser | Description | Body |
---|
GET | /get_all_contacts | Retrieve contacts | EMPTY |
GET | /check_connect | check if the client is connected | EMPTY |
GET | /last_qrcode | Check if the QR-Code is active | EMPTY |
GET | /screenshot | Get screenshot | EMPTY |
Basic send options functions (more features still under development)
You must be logged in to use these functions!
Here, to
can be <phone Number>@c.us
or <phone Number>-<groupId>@g.us
or <phone Number><groupId>@g.us
you can send messages only using one function!
await client
.sendMessage({
to: '0000000000@c.us',
body: 'A message sent by hydra-bot',
options: {
type: 'sendText',
},
})
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.sendMessage({
to: '0000000000@c.us',
body: './file.pdf',
options: {
type: 'sendFile',
filename: 'filename',
},
})
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.sendMessage({
to: '0000000000@c.us',
body: './file.mp3',
options: {
type: 'sendAudio',
},
})
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.sendMessage({
to: '0000000000@c.us',
body: base64MP3,
options: {
type: 'sendAudioBase64',
},
})
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.sendMessage({
to: '0000000000@c.us',
body: './file.jpg',
options: {
type: 'sendImage',
caption: 'image text',
},
})
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.sendMessage({
to: '0000000000@c.us',
body: base64IMG,
options: {
type: 'sendImageFromBase64',
caption: 'image text',
},
})
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
Basic send functions
await client
.sendText('0000000000@c.us', 'A message sent by hydra-bot')
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.sendFile('0000000000@c.us', './file.pdf', { filename: 'filename' })
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.sendAudio('0000000000@c.us', './file.mp3')
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.sendAudioBase64('0000000000@c.us', base64MP3)
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.sendImage('0000000000@c.us', './file.jpg', { caption: 'image text' })
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.sendImageFromBase64('0000000000@c.us', base64IMG, { caption: 'image text' })
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
Profile Functions
await client.getHost();
Retrieving Data
const contacts = await client.getAllContacts();
Group Management
Group number example <phone Number>-<groupId>@g.us
or <phone Number><groupId>@g.us
await client
.createGroup('Group name', ['111111111111@c.us', '222222222222@c.us'])
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.addParticipant('00000000-000000@g.us', [
'111111111111@c.us',
'222222222222@c.us',
])
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.setGroupDescription('00000000-000000@g.us', 'group description')
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
await client
.setGroupImage('00000000000000@g.us', './file.jpg')
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
Debugging
Building the hydra-bot is very simple
Development
To build the entire project just run
> npm run build
Test
run a test inside the project
> npm start
Maintainers
Maintainers are needed, I cannot keep with all the updates by myself. If you are
interested please open a Pull Request.
Contributing
Pull requests are welcome. For major changes, please open an issue first to
discuss what you would like to change.