forked from ookamiiixd/baileys-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b40a653
commit 62a5119
Showing
9 changed files
with
190 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,31 @@ | ||
const express = require('express'), | ||
app = express(), | ||
bodyParser = require('body-parser'), | ||
const app = require('express')(), | ||
http = require('http').Server(app), | ||
io = require('socket.io')(http), | ||
bodyParser = require('body-parser'), | ||
{ response } = require('./response'), | ||
whatsapp = require('./whatsapp'), | ||
routes = require('./routes') | ||
|
||
whatsapp.init() | ||
{ listen } = require('./socket'), | ||
chats = require('./routes/chatsRouter'), | ||
groups = require('./routes/groupsRouter') | ||
|
||
app.use(bodyParser.urlencoded({ extended: true })) | ||
app.use(bodyParser.json()) | ||
|
||
app.use('/', routes) | ||
|
||
io.on('connection', socket => { | ||
socket.emit('init', whatsapp.getActiveSessions()) | ||
app.use('/chats', chats) | ||
app.use('/groups', groups) | ||
|
||
socket.on('add', session => { | ||
if(whatsapp.checkSession(session)) return socket.emit('message', 'Session already exists.') | ||
app.get('/', (req, res) => { | ||
res.sendFile('./index.html', {root: __dirname}) | ||
}) | ||
|
||
whatsapp.createSession(socket, session) | ||
.catch(err => { | ||
console.log('unexpected error: ' + err) | ||
socket.emit('message', 'An error occured during creating session.') | ||
}) | ||
}) | ||
app.all('*', (req, res) => { | ||
response(res, 404, {success: false, message: 'The requested url cannot be found.'}) | ||
}) | ||
|
||
http.listen(8000, () => console.log('listening on http://localhost:8000/')) | ||
whatsapp.init() | ||
|
||
http.listen(8000, () => { | ||
listen(io, whatsapp) | ||
|
||
console.log('Server listening on http://localhost:8000/') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const { response } = require('./../response'), | ||
{ validationResult } = require('express-validator'), | ||
whatsapp = require('./../whatsapp'), | ||
{ MessageType } = require('@adiwajshing/baileys') | ||
|
||
const getChats = (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.query.session) | ||
if(!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) | ||
|
||
response(res, 200, {success: true, data: whatsapp.getChats(session, 'single')}) | ||
} | ||
|
||
const sendMessage = (req, res) => { | ||
const errors = validationResult(req) | ||
if(!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'}) | ||
|
||
const receiver = whatsapp.formatPhone(req.body.receiver) | ||
const message = req.body.message | ||
const session = whatsapp.getSession(req.body.sender) | ||
|
||
if(!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) | ||
|
||
session.isOnWhatsApp(receiver) | ||
.then(exists => { | ||
if(!exists) return response(res, 404, {success: false, message: 'The receiver number cannot be found.'}) | ||
|
||
session.sendMessage(receiver, message, MessageType.text) | ||
.then(() => response(res, 200, {success: true, message: 'The message has been sent successfully.'})) | ||
.catch(err => response(res, 500, {sucess: false, message: 'An error occured during sending the message.'})) | ||
}) | ||
} | ||
|
||
module.exports = { | ||
getChats: getChats, | ||
sendMessage: sendMessage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const { response } = require('./../response'), | ||
{ validationResult } = require('express-validator'), | ||
whatsapp = require('./../whatsapp'), | ||
{ MessageType } = require('@adiwajshing/baileys') | ||
|
||
const getChats = (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.query.session) | ||
if(!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) | ||
|
||
response(res, 200, {success: true, data: whatsapp.getChats(session, 'group')}) | ||
} | ||
|
||
const sendMessage = (req, res) => { | ||
const errors = validationResult(req) | ||
if(!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'}) | ||
|
||
const receiver = whatsapp.formatGroup(req.body.receiver) | ||
const message = req.body.message | ||
const session = whatsapp.getSession(req.body.sender) | ||
|
||
if(!session) return response(res, 404, {success: false, message: 'The requested session cannot be found.'}) | ||
|
||
session.sendMessage(receiver, message, MessageType.text) | ||
.then(() => response(res, 200, {success: true, message: 'The message has been sent successfully.'})) | ||
.catch(err => response(res, 500, {sucess: false, message: 'An error occured during sending the message.'})) | ||
} | ||
|
||
module.exports = { | ||
getChats: getChats, | ||
sendMessage: sendMessage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const response = (res, code, data) => { | ||
res.status(code) | ||
res.json(data) | ||
res.end() | ||
} | ||
|
||
exports.response = response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const router = require('express').Router(), | ||
{ query, body } = require('express-validator'), | ||
controller = require('./../controllers/chatsController') | ||
|
||
router.get( | ||
'/get', | ||
query('session').notEmpty(), | ||
controller.getChats | ||
) | ||
|
||
router.post( | ||
'/send', | ||
body('sender').notEmpty(), | ||
body('receiver').notEmpty(), | ||
body('message').notEmpty(), | ||
controller.sendMessage | ||
) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const router = require('express').Router(), | ||
{ query, body } = require('express-validator'), | ||
controller = require('./../controllers/groupsController') | ||
|
||
router.get( | ||
'/get', | ||
query('session').notEmpty(), | ||
controller.getChats | ||
) | ||
|
||
router.post( | ||
'/send', | ||
body('sender').notEmpty(), | ||
body('receiver').notEmpty(), | ||
body('message').notEmpty(), | ||
controller.sendMessage | ||
) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const listen = (io, whatsapp) => { | ||
io.on('connection', socket => { | ||
socket.emit('init', whatsapp.getActiveSessions()) | ||
|
||
socket.on('add', session => { | ||
if(whatsapp.checkSession(session)) return socket.emit('message', 'Session already exists.') | ||
|
||
whatsapp.createSession(socket, session) | ||
.catch(err => { | ||
console.log('unexpected error: ' + err) | ||
socket.emit('message', 'An error occured during creating session.') | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
exports.listen = listen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters