Skip to content

Commit

Permalink
Allow to send message group by group name & some fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ngekoding committed Jan 21, 2021
1 parent 9bea507 commit 9b46baa
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
34 changes: 30 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,24 @@ app.post('/send-media', async (req, res) => {
});
});

const findGroupByName = async function(name) {
const group = await client.getChats().then(chats => {
return chats.find(chat =>
chat.isGroup && chat.name.toLowerCase() == name.toLowerCase()
);
});
return group;
}

// Send message to group
// -- Send message !groups to get all groups (id & name)
// -- So you can use that group id to send a message
// You can use chatID or group name, yea!
app.post('/send-group-message', [
body('id').notEmpty(),
body('id').custom((value, { req }) => {
if (!value && !req.body.name) {
throw new Error('Invalid value, you can use `id` or `name`');
}
return true;
}),
body('message').notEmpty(),
], async (req, res) => {
const errors = validationResult(req).formatWith(({
Expand All @@ -224,9 +237,22 @@ app.post('/send-group-message', [
});
}

const chatId = req.body.id;
let chatId = req.body.id;
const groupName = req.body.name;
const message = req.body.message;

// Find the group by name
if (!chatId) {
const group = await findGroupByName(groupName);
if (!group) {
return res.status(422).json({
status: false,
message: 'No group found with name: ' + groupName
});
}
chatId = group.id._serialized;
}

client.sendMessage(chatId, message).then(response => {
res.status(200).json({
status: true,
Expand Down
4 changes: 2 additions & 2 deletions index-multiple-device.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ <h3>Logs:</h3>
</div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js" integrity="sha512-/WwtKR6NnHomLo0O4w9QKc1INTPEJs7ko6u2aBTA1paPldhPl8LtXsi7a35iEZ69+9P5dcgVNESG8hrP4Y2t3w==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
<script>
$(document).ready(function() {
var socket = io();
Expand Down
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ <h3>Logs:</h3>
<ul class="logs"></ul>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js" integrity="sha512-/WwtKR6NnHomLo0O4w9QKc1INTPEJs7ko6u2aBTA1paPldhPl8LtXsi7a35iEZ69+9P5dcgVNESG8hrP4Y2t3w==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
var socket = io();
Expand Down
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"ignore": ["whatsapp-session-*.json", "whatsapp-sessions.json"]
"ignore": ["whatsapp-session*.json", "whatsapp-sessions.json"]
}
13 changes: 8 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ Watch the tutorials:
- Scan the QR Code
- Enjoy!

### Notes
### Send message to group

As mentioned in the video above, you have to install `nodemon` to run the start script. You can install nodemon globally with `npm i -g nodemon` command.
You can send the message to any group by using `chatID` or group `name`, chatID will used if you specify the `id` field in the form, so if you want to send by `name`, only use name.

### Send message to group
**Paramaters:**

- `id` (optinal if name given): the chat ID
- `name` (optional): group name
- `message`: the message

I was added an example to send a message to groups, but before that we must know the group ID (chat ID). Don't worry, I also add the functionality to help you to get that groups ID.
Here the endpoint: `/send-group-message`

Here the way to get the groups info (including ID & name):

- Send a message to the API number `!groups`
- The API will replying with the groups info
- Use the ID to send a message
- Here the endpoint: `/send-group-message`

0 comments on commit 9b46baa

Please sign in to comment.