mirror of
https://github.com/cheveguerra/whatsapp-api-tutorial.git
synced 2026-04-17 19:36:59 +00:00
Add send message to group
This commit is contained in:
75
app.js
75
app.js
@@ -14,7 +14,9 @@ const server = http.createServer(app);
|
||||
const io = socketIO(server);
|
||||
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.urlencoded({
|
||||
extended: true
|
||||
}));
|
||||
app.use(fileUpload({
|
||||
debug: true
|
||||
}));
|
||||
@@ -26,7 +28,9 @@ if (fs.existsSync(SESSION_FILE_PATH)) {
|
||||
}
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile('index.html', { root: __dirname });
|
||||
res.sendFile('index.html', {
|
||||
root: __dirname
|
||||
});
|
||||
});
|
||||
|
||||
const client = new Client({
|
||||
@@ -51,6 +55,21 @@ client.on('message', msg => {
|
||||
msg.reply('pong');
|
||||
} else if (msg.body == 'good morning') {
|
||||
msg.reply('selamat pagi');
|
||||
} else if (msg.body == '!groups') {
|
||||
client.getChats().then(chats => {
|
||||
const groups = chats.filter(chat => chat.isGroup);
|
||||
|
||||
if (groups.length == 0) {
|
||||
msg.reply('You have no group yet.');
|
||||
} else {
|
||||
let replyMsg = '*YOUR GROUPS*\n\n';
|
||||
groups.forEach((group, i) => {
|
||||
replyMsg += `ID: ${group.id._serialized}\nName: ${group.name}\n\n`;
|
||||
});
|
||||
replyMsg += '_You can use the group id to send a message to the group._'
|
||||
msg.reply(replyMsg);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -77,8 +96,8 @@ io.on('connection', function(socket) {
|
||||
socket.emit('authenticated', 'Whatsapp is authenticated!');
|
||||
socket.emit('message', 'Whatsapp is authenticated!');
|
||||
console.log('AUTHENTICATED', session);
|
||||
sessionCfg=session;
|
||||
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function (err) {
|
||||
sessionCfg = session;
|
||||
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function(err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
}
|
||||
@@ -107,7 +126,9 @@ app.post('/send-message', [
|
||||
body('number').notEmpty(),
|
||||
body('message').notEmpty(),
|
||||
], async (req, res) => {
|
||||
const errors = validationResult(req).formatWith(({ msg }) => {
|
||||
const errors = validationResult(req).formatWith(({
|
||||
msg
|
||||
}) => {
|
||||
return msg;
|
||||
});
|
||||
|
||||
@@ -153,14 +174,54 @@ app.post('/send-media', async (req, res) => {
|
||||
// const file = req.files.file;
|
||||
// const media = new MessageMedia(file.mimetype, file.data.toString('base64'), file.name);
|
||||
let mimetype;
|
||||
const attachment = await axios.get(fileUrl, { responseType: 'arraybuffer' }).then(response => {
|
||||
const attachment = await axios.get(fileUrl, {
|
||||
responseType: 'arraybuffer'
|
||||
}).then(response => {
|
||||
mimetype = response.headers['content-type'];
|
||||
return response.data.toString('base64');
|
||||
});
|
||||
|
||||
const media = new MessageMedia(mimetype, attachment, 'Media');
|
||||
|
||||
client.sendMessage(number, media, { caption: caption }).then(response => {
|
||||
client.sendMessage(number, media, {
|
||||
caption: caption
|
||||
}).then(response => {
|
||||
res.status(200).json({
|
||||
status: true,
|
||||
response: response
|
||||
});
|
||||
}).catch(err => {
|
||||
res.status(500).json({
|
||||
status: false,
|
||||
response: err
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Send message to group
|
||||
// -- Send message !groups to get all groups (id & name)
|
||||
// -- So you can use that group id to send a message
|
||||
app.post('/send-group-message', [
|
||||
body('id').notEmpty(),
|
||||
body('message').notEmpty(),
|
||||
], async (req, res) => {
|
||||
const errors = validationResult(req).formatWith(({
|
||||
msg
|
||||
}) => {
|
||||
return msg;
|
||||
});
|
||||
|
||||
if (!errors.isEmpty()) {
|
||||
return res.status(422).json({
|
||||
status: false,
|
||||
message: errors.mapped()
|
||||
});
|
||||
}
|
||||
|
||||
const chatId = req.body.id;
|
||||
const message = req.body.message;
|
||||
|
||||
client.sendMessage(chatId, message).then(response => {
|
||||
res.status(200).json({
|
||||
status: true,
|
||||
response: response
|
||||
|
||||
Reference in New Issue
Block a user