diff --git a/baileys-api.json b/baileys-api.json new file mode 100644 index 00000000..7186faac --- /dev/null +++ b/baileys-api.json @@ -0,0 +1,453 @@ +{ + "info": { + "_postman_id": "4e47ac1f-23ee-4d64-8bc8-ea84762913f2", + "name": "New Collection", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "Session", + "item": [ + { + "name": "Create New Session", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"session\": \"instance-one\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/sessions", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "sessions" + ], + "query": [ + { + "key": "session", + "value": "tiagoandre", + "disabled": true + } + ] + } + }, + "response": [] + }, + { + "name": "QRCode Session", + "protocolProfileBehavior": { + "disableBodyPruning": true + }, + "request": { + "method": "GET", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"session\": \"instance-one\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/sessions/qrcode", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "sessions", + "qrcode" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "Groups", + "item": [ + { + "name": "Create New Group", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{ \n \"sender\": \"instance-one\",\n \"name\" : \"group-test\",\n \"members\" : \"5511900000000\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/groups/group-create", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "groups", + "group-create" + ] + } + }, + "response": [] + }, + { + "name": "Update Description", + "request": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "{ \n \"sender\": \"instance-one\",\n \"groupId\" : \"551190000000-1635813336@g.us\",\n \"description\" : \"Nova Descrição do Grupo\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/groups/group-update-description", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "groups", + "group-update-description" + ] + } + }, + "response": [] + }, + { + "name": "Update Name", + "request": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "{ \n \"sender\": \"instance-one\",\n \"groupId\" : \"551190000000-1635813336@g.us\",\n \"name\" : \"Test 001\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/groups/group-update-name", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "groups", + "group-update-name" + ] + } + }, + "response": [] + }, + { + "name": "Add Members", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{ \n \"sender\": \"instance-one\",\n \"groupId\" : \"551190000000-1635813336@g.us\",\n \"members\" : \"55119000000\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/groups/group-add-members", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "groups", + "group-add-members" + ] + } + }, + "response": [] + }, + { + "name": "Add Admin", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{ \n \"sender\": \"instance-one\",\n \"groupId\" : \"551190000000-1635813336@g.us\",\n \"members\" : \"55119000000\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/groups/group-make-admin", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "groups", + "group-make-admin" + ] + } + }, + "response": [] + }, + { + "name": "Remove Admin", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{ \n \"sender\": \"instance-one\",\n \"groupId\" : \"551190000000-1635813336@g.us\",\n \"members\" : \"551190000000\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/groups/group-remove-admin", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "groups", + "group-remove-admin" + ] + } + }, + "response": [] + }, + { + "name": "All Groups", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{host}}:{{port}}/groups/get?session=instance-one", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "groups", + "get" + ], + "query": [ + { + "key": "session", + "value": "instance-one" + } + ] + } + }, + "response": [] + }, + { + "name": "Medadata", + "protocolProfileBehavior": { + "disableBodyPruning": true + }, + "request": { + "method": "GET", + "header": [], + "body": { + "mode": "raw", + "raw": "{ \n \"sender\": \"instance-one\",\n \"groupId\" : \"551190000000-1635813336@g.us\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/groups/group-meta-data", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "groups", + "group-meta-data" + ] + } + }, + "response": [] + }, + { + "name": "Invite Code", + "protocolProfileBehavior": { + "disableBodyPruning": true + }, + "request": { + "method": "GET", + "header": [], + "body": { + "mode": "raw", + "raw": "{ \n \"sender\": \"instance-one\",\n \"groupId\" : \"551190000000-1635813336@g.us\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/groups/group-invite-code", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "groups", + "group-invite-code" + ] + } + }, + "response": [] + }, + { + "name": "Leave", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{ \n \"sender\": \"instance-one\",\n \"groupId\" : \"551190000000-1635813336@g.us\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/groups/group-leave", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "groups", + "group-leave" + ] + } + }, + "response": [] + }, + { + "name": "Setting Message", + "request": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "{ \n \"sender\": \"instance-one\",\n \"groupId\" : \"551190000000-1635813336@g.us\",\n \"value\" : true\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/groups/group-setting-message", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "groups", + "group-setting-message" + ] + } + }, + "response": [] + }, + { + "name": "Setting Change", + "request": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "{ \n \"sender\": \"instance-one\",\n \"groupId\" : \"551190000000-1635813336@g.us\",\n \"value\" : true\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{host}}:{{port}}/groups/group-setting-change", + "host": [ + "{{host}}" + ], + "port": "{{port}}", + "path": [ + "groups", + "group-setting-change" + ] + } + }, + "response": [] + } + ] + } + ], + "event": [ + { + "listen": "prerequest", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + }, + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + } + ], + "variable": [ + { + "key": "host", + "value": "localhost" + }, + { + "key": "port", + "value": "8000" + } + ] +} \ No newline at end of file diff --git a/src/controllers/groupsController.js b/src/controllers/groupsController.js index 302fc12e..9feeed71 100644 --- a/src/controllers/groupsController.js +++ b/src/controllers/groupsController.js @@ -1,7 +1,7 @@ const {response} = require('./../response'), {validationResult} = require('express-validator'), whatsapp = require('./../whatsapp'), - {MessageType} = require('@adiwajshing/baileys') + {MessageType, GroupSettingChange} = require('@adiwajshing/baileys') const getChats = (req, res) => { const errors = validationResult(req) @@ -35,20 +35,11 @@ const groupCreate = (req, res) => { const session = whatsapp.getSession(req.body.sender) const name = req.body.name - const members = whatsapp.formatNumberGroup(req.body.members) + const members = whatsapp.formatNumberGroup(req.body.members) if (!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) - async function create() { - try { - let group = await session.groupCreate(name, members) - return group - } catch (error) { - return error - } - } - - create() + session.groupCreate(name, members) .then((success) => response(res, 200, { success: true, message: 'Group created successfully.', @@ -57,7 +48,7 @@ const groupCreate = (req, res) => { .catch(err => response(res, 500, {success: false, message: res})) } -const groupAdd = (req, res) => { +const groupAddMember = (req, res) => { const errors = validationResult(req) if (!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'}) @@ -68,17 +59,7 @@ const groupAdd = (req, res) => { if (!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) - async function groupAdd() { - try { - let group = await session.groupAdd(groupId, members) - console.log(group) - return group - } catch (error) { - return error - } - } - - groupAdd() + session.groupAdd(groupId, members) .then((success) => response(res, 200, { success: true, message: 'New members successfully added.', @@ -87,9 +68,199 @@ const groupAdd = (req, res) => { } +const groupMakeAdmin = (req, res) => { + + const errors = validationResult(req) + if (!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'}) + + const session = whatsapp.getSession(req.body.sender) + const groupId = req.body.groupId + const members = whatsapp.formatNumberGroup(req.body.members) + + if (!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) + + session.groupMakeAdmin(groupId, members) + .then((success) => response(res, 200, { + success: true, + message: 'Admin successfully added.', + })) + .catch(err => response(res, 500, {success: false, message: res})) + +} + +const groupRemoveAdmin = (req, res) => { + + const errors = validationResult(req) + if (!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'}) + + const session = whatsapp.getSession(req.body.sender) + const groupId = req.body.groupId + const members = whatsapp.formatNumberGroup(req.body.members) + + if (!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) + + session.groupDemoteAdmin(groupId, members) + .then((success) => response(res, 200, { + success: true, + message: 'Admin remove successfully.', + })) + .catch(err => response(res, 500, {success: false, message: res})) + +} + +const groupUpdateName = (req, res) => { + + const errors = validationResult(req) + if (!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'}) + + const session = whatsapp.getSession(req.body.sender) + const name = req.body.name + const groupId = req.body.groupId + + if (!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) + + session.groupUpdateSubject(groupId, name) + .then((success) => response(res, 200, { + success: true, + message: 'Update name group successfully.', + })) + .catch(err => response(res, 500, {success: false, message: res})) + +} + +const groupUpdateDescription = (req, res) => { + + const errors = validationResult(req) + if (!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'}) + + const session = whatsapp.getSession(req.body.sender) + const description = req.body.description + const groupId = req.body.groupId + + if (!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) + + session.groupUpdateDescription(groupId, description) + .then((success) => response(res, 200, { + success: true, + message: 'Update description group successfully.', + })) + .catch(err => response(res, 500, {success: false, message: res})) + +} + +const groupSettingMessage = (req, res) => { + + const errors = validationResult(req) + if (!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'}) + + const session = whatsapp.getSession(req.body.sender) + const value = req.body.value + const groupId = req.body.groupId + + if (!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) + + session.groupSettingChange(groupId, GroupSettingChange.messageSend, value) + .then((success) => response(res, 200, { + success: true, + message: 'Update setting message successfully.', + })) + .catch(err => response(res, 500, {success: false, message: res})) + +} + +const groupSettingChange = (req, res) => { + + const errors = validationResult(req) + if (!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'}) + + const session = whatsapp.getSession(req.body.sender) + const value = req.body.value + const groupId = req.body.groupId + + if (!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) + + session.groupSettingChange(groupId, GroupSettingChange.settingsChange, value) + .then((success) => response(res, 200, { + success: true, + message: 'Update setting successfully.', + })) + .catch(err => response(res, 500, {success: false, message: res})) + +} + +const groupLeave = (req, res) => { + + const errors = validationResult(req) + if (!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'}) + + const session = whatsapp.getSession(req.body.sender) + const groupId = req.body.groupId + + if (!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) + + session.groupLeave(groupId) + .then((success) => response(res, 200, { + success: true, + message: 'Leave a group successfully.', + })) + .catch(err => response(res, 500, {success: false, message: res})) + +} + +const groupInviteCode = (req, res) => { + + const errors = validationResult(req) + if (!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'}) + + const session = whatsapp.getSession(req.body.sender) + const groupId = req.body.groupId + + if (!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) + + session.groupInviteCode(groupId) + .then((success) => response(res, 200, { + success: true, + message: 'Invite Code Successfully.', + code: success, + link: "https://chat.whatsapp.com/"+success + })) + .catch(err => response(res, 500, {success: false, message: res})) + +} + +const groupMetadata = (req, res) => { + + const errors = validationResult(req) + if (!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'}) + + const session = whatsapp.getSession(req.body.sender) + const groupId = req.body.groupId + + if (!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) + + session.groupMetadata(groupId) + .then((success) => response(res, 200, { + success: true, + message: 'Metadata successfully.', + group: success, + })) + .catch(err => response(res, 500, {success: false, message: res})) + +} + module.exports = { getChats: getChats, sendMessage: sendMessage, groupCreate: groupCreate, - groupAdd: groupAdd + groupAddMember: groupAddMember, + groupMakeAdmin: groupMakeAdmin, + groupRemoveAdmin: groupRemoveAdmin, + groupUpdateName: groupUpdateName, + groupUpdateDescription: groupUpdateDescription, + groupSettingMessage: groupSettingMessage, + groupSettingChange: groupSettingChange, + groupLeave: groupLeave, + groupInviteCode: groupInviteCode, + groupMetadata: groupMetadata + } \ No newline at end of file diff --git a/src/routes/groupsRouter.js b/src/routes/groupsRouter.js index 7a6dbe2a..c37120c1 100644 --- a/src/routes/groupsRouter.js +++ b/src/routes/groupsRouter.js @@ -25,11 +25,66 @@ router.post( ) router.post( - '/group-add', + '/group-add-members', body('sender').notEmpty(), body('groupId').notEmpty(), body('members').notEmpty(), - controller.groupAdd + controller.groupAddMember +) + +router.post( + '/group-make-admin', + body('sender').notEmpty(), + body('groupId').notEmpty(), + body('members').notEmpty(), + controller.groupMakeAdmin +) + +router.post( + '/group-remove-admin', + body('sender').notEmpty(), + body('groupId').notEmpty(), + body('members').notEmpty(), + controller.groupRemoveAdmin +) + +router.patch('/group-update-name', + body('sender').notEmpty(), + body('groupId').notEmpty(), + body('name').notEmpty(), + controller.groupUpdateName +) + +router.patch('/group-setting-message', + body('sender').notEmpty(), + body('groupId').notEmpty(), + body('value').notEmpty(), + controller.groupSettingMessage +) + +router.patch('/group-setting-change', + body('sender').notEmpty(), + body('groupId').notEmpty(), + body('value').notEmpty(), + controller.groupSettingChange +) + +router.post('/group-leave', + body('sender').notEmpty(), + body('groupId').notEmpty(), + controller.groupLeave +) + +router.get('/group-invite-code', + body('sender').notEmpty(), + body('groupId').notEmpty(), + controller.groupInviteCode +) + +router.get('/group-meta-data', + body('sender').notEmpty(), + body('groupId').notEmpty(), + controller.groupMetadata ) module.exports = router \ No newline at end of file diff --git a/src/whatsapp.js b/src/whatsapp.js index 81986dde..55130921 100644 --- a/src/whatsapp.js +++ b/src/whatsapp.js @@ -23,6 +23,7 @@ const createExistedSession = async session => { let wa = new WAConnection() wa.browserDescription = ['Windows', 'Chrome', '10'] + wa.version = [2,2134,10] wa.loadAuthInfo(path.join(__dirname, 'sessions', `${session}.json`)) wa.on('open', () => { @@ -46,7 +47,7 @@ const createSession = async (session) => { console.log("Creating session: " + session) wa.browserDescription = ['Windows', 'Chrome', '10'] - wa.version = [2,2142,12] + wa.version = [2,2134,10] let timeout = setTimeout(() => { if(fs.existsSync(path.join(__dirname, 'data', `session_qrcode.json`))) fs.unlinkSync(path.join(__dirname, 'data', `session_qrcode.json`))