mirror of
https://github.com/cheveguerra/whatsapp-api-tutorial.git
synced 2026-04-17 19:36:59 +00:00
Allow to send message group by group name & some fix
This commit is contained in:
34
app.js
34
app.js
@@ -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(({
|
||||
@@ -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,
|
||||
|
||||
@@ -41,8 +41,8 @@
|
||||
</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();
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
<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();
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"ignore": ["whatsapp-session-*.json", "whatsapp-sessions.json"]
|
||||
"ignore": ["whatsapp-session*.json", "whatsapp-sessions.json"]
|
||||
}
|
||||
15
readme.md
15
readme.md
@@ -22,17 +22,20 @@ Watch the tutorials:
|
||||
- Scan the QR Code
|
||||
- Enjoy!
|
||||
|
||||
### Notes
|
||||
|
||||
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.
|
||||
|
||||
### Send message to group
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
**Paramaters:**
|
||||
|
||||
- `id` (optinal if name given): the chat ID
|
||||
- `name` (optional): group name
|
||||
- `message`: the message
|
||||
|
||||
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`
|
||||
|
||||
Reference in New Issue
Block a user