Skip to content

Commit

Permalink
feat: New features and documentation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Nov 28, 2024
1 parent 87b553e commit 59c9131
Show file tree
Hide file tree
Showing 75 changed files with 1,167 additions and 405 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ token
*.tgz
dist.zip
api-docs
files
test/dowload/*
102 changes: 93 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,15 @@ const fs = require('fs');

// Was connected to whatsapp chat
if (conn.connect) {
client = conn.client;
client = conn.client; // class client from hydra-bot
const getMe = await client.getHost();
const hostNumber = getMe.id._serialized; // number host
console.log('Host Number: ', hostNumber);

// send a text message
await client
.sendMessage({
to: '[email protected]', // you can pass the contact number or group number
to: hostNumber, // you can pass the contact number or group number
body: "hi i'm hydra bot", // message text
options: {
type: 'sendText', // shipping type
Expand All @@ -127,11 +131,11 @@ const fs = require('fs');
// return receive new messages
ev.on('newMessage', async (newMsg) => {
// when is received
if (!newMsg.result.isSentByMe) {
if (!newMsg.result.fromMe) {
// message received!
console.log('NewMessageReceived: ', newMsg.result);
// dowload files
if (newMsg.result.isMedia === true || newMsg.result.isMMS === true) {
if (newMsg.result.isMedia) {
const buffer = await client.decryptFile(newMsg.result);
// At this point you can do whatever you want with the buffer
// Most likely you want to write it into a file
Expand All @@ -146,7 +150,7 @@ const fs = require('fs');
}
}
// when is it sent
if (!!newMsg.result.isSentByMe) {
if (!!newMsg.result.fromMe) {
// Message sent
console.log('NewMessageSent: ', newMsg.result);
}
Expand Down Expand Up @@ -176,20 +180,22 @@ const mime = require('mime-types');
let client;
// start bot service
const ev = await hydraBot.initServer();

// return connection information
ev.on('connection', async (conn) => {
// Was connected to whatsapp chat
if (conn.connect) {
client = conn.client;
}
});

ev.on('newMessage', async (newMsg) => {
// when is received
if (!newMsg.result.isSentByMe) {
if (!newMsg.result.fromMe) {
// message received!
console.log('NewMessageReceived: ', newMsg.result);
// dowload files
if (newMsg.result.isMedia === true || newMsg.result.isMMS === true) {
if (newMsg.result.isMedia) {
const buffer = await client.decryptFile(newMsg.result);
// At this point you can do whatever you want with the buffer
// Most likely you want to write it into a file
Expand Down Expand Up @@ -530,6 +536,9 @@ await client.getHost();
```javascript
// returns a list of contacts
const contacts = await client.getAllContacts();

// return whatsapp version
const version = await client.getWAVersion();
```

## Group Management
Expand Down Expand Up @@ -581,6 +590,81 @@ await client
});
```

## Events

List of events triggered in the project

```javascript

// Event triggered when there's a change in the WhatsApp interface
// The change information can include elements like screen changes or navigation.
ev.on("interfaceChange", (change: any) => {
// Processes the interface change, like navigation between screens
console.log("Interface change detected:", change);
});

// Event triggered when a QR code is generated, typically used for authentication
// The QR code is sent to the client as a string or object containing data for login.
ev.on("qrcode", (qrcode) => {
// Displays the generated QR code for WhatsApp Web authentication
console.log("QR Code for authentication:", qrcode);
});

// Event triggered when there is a connection change, such as connection loss or establishment
// Connection data may include network status or connection errors.
ev.on('connection', async (conn) => {
// Displays information about the connection status
console.log("Connection status:", conn);
if (conn.connect) {
// Was connected to whatsapp chat
console.error("Has connected");
}
});

// Event triggered when a new message is received
// The message may include data such as sender, content, timestamp, etc.
ev.on("newMessage", (newMsg) => {
// Displays the data of the new received message
console.log("New message received:", newMsg);
});

// Event triggered when a message is edited
// The edited message may include the previous content and the new content.
ev.on("newEditMessage", async (editMessage) => {
// Processes the edited message by checking changes in content
console.log("Message edited:", editMessage);
});

// Event triggered when a message is deleted
// The deleted message may include the message ID and other related details.
ev.on("newDeleteMessage", async (deleteMessage) => {
// Processes the deletion of the message and notifies the user
console.log("Message deleted:", deleteMessage);
});

// Event triggered when there is a new intro reaction (emoji) to a message
// This can be used to analyze which reactions were added to new messages.
ev.on("onIntroReactionMessage", async (introReaction) => {
// Processes the intro reaction (emoji) to the new message
console.log("New intro reaction received:", introReaction);
});

// Event triggered when an emoji reaction is added to an existing message
// The reaction may include details like the emoji, sender, and associated message.
ev.on("onReactionMessage", async (reaction) => {
// Processes the reaction added to an existing message
console.log("Reaction added to message:", reaction);
});

// Event triggered to return the status of each message (e.g., read, delivered, etc.)
// This can include data such as delivery and read status, allowing message state tracking.
ev.on("newOnAck", async (event) => {
// Processes the acknowledgment status of the message
console.log("Message ack status:", event);
});

```

### Debugging

Building the hydra-bot is very simple
Expand All @@ -595,10 +679,10 @@ To build the entire project just run

## Test

run a test inside the project
Run a test inside the project

```bash
> npm start
> npm run build:dev
```

## Maintainers
Expand Down
52 changes: 36 additions & 16 deletions docs/getting-started/admin.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Example Postman
Use Postman to test routes!
Use **Postman** to test the routes!
[Postman example](https://github.com/jonalan7/Hydra-bot/blob/master/Postman/postman_collection.json)
[Download Postman](https://www.postman.com/downloads/)

### Commands for administration via terminal

To start the administration interface use:
To start the administration interface, run the following command in the terminal:

```bash
> yarn admin
Expand All @@ -21,30 +21,32 @@ Install yarn Ubuntu:
```

#### what is an administrator for?
Administrators will be able to manage API users via Webhook.
Administrators are responsible for managing API users via Webhook. They can execute commands that allow creating, deleting, and managing user data.

List of commands in the terminal:
## Available Commands in the Terminal for Administration:

Here is the list of commands an administrator can use in the terminal to manage users:

| Command | Description |
|---------------|--------------------------------------------------|
| `/create` | Create user |
| `/delete` | Delete user |
| `/selectid` | Show user by id |
| `/selectname` | Select user by name |
| `/create` | Create a new user |
| `/delete` | Delete an existing user |
| `/selectid` | Show a user by ID |
| `/selectname` | Show a user by name |
| `/getall` | List all users |
| `/deactivate` | Disable user |
| `/activate` | Activate User |
| `/changename` | Change username |
| `/password` | Change user password |
| `/changename` | Change a user's name |
| `/password` | Change a user's password |
| `/cls` | Clear screen/terminal |
| `/help` | List all commands for administration in terminal |
| `/exit` | Exit manager |
| `/help` | List all available commands for administration |
| `/exit` | Exit the administration manager |


### Administration via Webhook
Routes for handling and querying users.
List of commands using `REST API`
All user wheels have a pattern of `Headers`, to be able to access them, to create a administrador:
These routes allow you to manage and query users using a REST API. An administrator can access these routes by authenticating with a specific header that includes the admin credentials.

To authenticate requests via Webhook, you need to include the following headers in the request:

```json
{
Expand All @@ -54,7 +56,7 @@ All user wheels have a pattern of `Headers`, to be able to access them, to creat
}
```

### List of routes for user management:
### List of Routes for User Management:

With an [administrator](#commands-for-administration-via-terminal) (there is a default administrator, the username and password as admin), he can access via Web Service with the following routes:

Expand All @@ -68,3 +70,21 @@ With an [administrator](#commands-for-administration-via-terminal) (there is a d
| 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"}` |

### How to Use the Routes:

- **POST `/create_user`**: This route creates a new user. Send a JSON body containing the name and password of the user you want to create.

- **DELETE `/delete_user/USER_ID`**: This route deletes a specific user by their USER_ID. No body is required in the request.

- **GET `/get_user_by_id/USER_ID`**: To retrieve a user by their ID, send a GET request. No body is required.

- **GET `/get_all_users`**: This route lists all users. No body is required in the request.

- **PUT `/deactivate_user`**: To deactivate a user, send a JSON body containing the USER_ID of the user you want to disable.

- **PUT `/activate_user`**: This route activates a disabled user. Send the USER_ID in the request body.

- **PUT `/change_name`**: To change a user's name, send the USER_ID and the new name in the request body.

- **PUT `/change_password`**: This route changes a user's password. Send the USER_ID and the new password in the request body.
4 changes: 2 additions & 2 deletions docs/getting-started/downloading_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const mime = require('mime-types');
});
ev.on('newMessage', async (newMsg) => {
// when is received
if (!newMsg.result.isSentByMe) {
if (!newMsg.result.fromMe) {
// message received!
console.log('NewMessageReceived: ', newMsg.result);
// dowload files
if (newMsg.result.isMedia === true || newMsg.result.isMMS === true) {
if (newMsg.result.isMedia) {
const buffer = await client.decryptFile(newMsg.result);
// At this point you can do whatever you want with the buffer
// Most likely you want to write it into a file
Expand Down
108 changes: 108 additions & 0 deletions docs/getting-started/events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Group Functions

##### Here, `to` can be `<phone Number>@c.us`, `<phone Number>-<groupId>@g.us`, or `<phone Number><groupId>@g.us`.

To use these functions, it is necessary to initialize the bot.
[**Click here to learn more.**](../Getting%20Started/start_bot.html)

## Summary
- [interfaceChange](#interfaceChange)
- [qrcode](#qrcode)
- [connection](#connection)
- [newEditMessage](#newEditMessage)
- [newDeleteMessage](#newDeleteMessage)
- [onIntroReactionMessage](#onIntroReactionMessage)
- [onReactionMessage](#onReactionMessage)
- [newOnAck](#newOnAck)

### interfaceChange
Event triggered when there's a change in the WhatsApp interface

```javascript
// The change information can include elements like screen changes or navigation.
ev.on("interfaceChange", (change: any) => {
// Processes the interface change, like navigation between screens
console.log("Interface change detected:", change);
});
```

### qrcode
Event triggered when a QR code is generated, typically used for authentication

```javascript
// The QR code is sent to the client as a string or object containing data for login.
ev.on("qrcode", (qrcode) => {
// Displays the generated QR code for WhatsApp Web authentication
console.log("QR Code for authentication:", qrcode);
});
```

### connection
Event triggered when there is a connection change, such as connection loss or establishment

```javascript
// Connection data may include network status or connection errors.
ev.on('connection', async (conn) => {
// Displays information about the connection status
console.log("Connection status:", conn);
if (conn.connect) {
// Was connected to whatsapp chat
console.error("Has connected");
}
});
```

### newEditMessage
Event triggered when a message is edited

```javascript
// The edited message may include the previous content and the new content.
ev.on("newEditMessage", async (editMessage) => {
// Processes the edited message by checking changes in content
console.log("Message edited:", editMessage);
});
```

### newDeleteMessage
Event triggered when a message is deleted

```javascript
// The deleted message may include the message ID and other related details.
ev.on("newDeleteMessage", async (deleteMessage) => {
// Processes the deletion of the message and notifies the user
console.log("Message deleted:", deleteMessage);
});
```

### onIntroReactionMessage
Event triggered when there is a new intro reaction (emoji) to a message

```javascript
// This can be used to analyze which reactions were added to new messages.
ev.on("onIntroReactionMessage", async (introReaction) => {
// Processes the intro reaction (emoji) to the new message
console.log("New intro reaction received:", introReaction);
});
```

### onReactionMessage
Event triggered when an emoji reaction is added to an existing message

```javascript
// The reaction may include details like the emoji, sender, and associated message.
ev.on("onReactionMessage", async (reaction) => {
// Processes the reaction added to an existing message
console.log("Reaction added to message:", reaction);
});
```

### newOnAck
Event triggered to return the status of each message (e.g., read, delivered, etc.)

```javascript
// This can include data such as delivery and read status, allowing message state tracking.
ev.on("newOnAck", async (event) => {
// Processes the acknowledgment status of the message
console.log("Message ack status:", event);
});
```
Loading

0 comments on commit 59c9131

Please sign in to comment.