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.
Merge pull request ookamiiixd#2 from ookamiiixd/es6
Es6
- Loading branch information
Showing
21 changed files
with
1,902 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 4 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true |
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,22 @@ | ||
{ | ||
"env": { | ||
"node": true | ||
}, | ||
"extends": ["xo", "prettier"], | ||
"parserOptions": { | ||
"ecmaVersion": 2020, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"prettier/prettier": "error", | ||
"one-var": "off", | ||
"max-params": ["error", 5], | ||
"arrow-body-style": ["error", "always"], | ||
"radix": "off", | ||
"no-unused-expressions": ["error", { "allowTernary": true }], | ||
"curly": "error", | ||
"new-cap": "off", | ||
"no-return-assign": "off" | ||
}, | ||
"plugins": ["prettier"] | ||
} |
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,2 @@ | ||
node_modules/ | ||
package-lock.json |
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,6 @@ | ||
{ | ||
"printWidth": 120, | ||
"tabWidth": 4, | ||
"semi": false, | ||
"singleQuote": true | ||
} |
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,32 @@ | ||
# Baileys API | ||
|
||
An implementation of [@adiwajshing/Baileys](https://github.com/adiwajshing/Baileys) as a simple RESTful API service with multiple device support. | ||
|
||
## Installation | ||
|
||
1. Download or clone this repo. | ||
2. Enter to the project directory. | ||
3. Execute `npm i` to install the dependencies. | ||
|
||
## Usage | ||
|
||
1. You can start the app by executing `npm run start` or `node .`. | ||
2. Now the endpoint should be available according to your environment variable settings. Default is at `http://localhost:8000`. | ||
|
||
## API Docs | ||
|
||
The API documentation is available online at [here](https://documenter.getpostman.com/view/18988925/UVRHiNne). You can also import the **Postman Collection File** `(postman_collection.json)` into your Postman App alternatively. | ||
|
||
The server will respond in JSON format: | ||
|
||
```javascript | ||
{ | ||
success: true|false, // bool | ||
message: "", // string | ||
data: {} // object | ||
} | ||
``` | ||
|
||
## Notice | ||
|
||
This project is intended for learning purpose only, don't use this for spam or any activities that is prohibited by **WhatsApp**. |
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,18 @@ | ||
import express from 'express' | ||
import routes from './routes.js' | ||
import { init } from './whatsapp.js' | ||
|
||
const app = express() | ||
const host = process.env.HOST ?? '127.0.0.1' | ||
const port = process.env.PORT ?? 8000 | ||
|
||
app.use(express.urlencoded({ extended: true })) | ||
app.use(express.json()) | ||
app.use('/', routes) | ||
|
||
app.listen(port, host, () => { | ||
init() | ||
console.log(`Server is listening on http://${host}:${port}`) | ||
}) | ||
|
||
export default app |
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,37 @@ | ||
import { MessageType } from '@adiwajshing/baileys' | ||
import { getChatList, formatPhone } from './../whatsapp.js' | ||
import response from './../response.js' | ||
|
||
const getList = (req, res) => { | ||
const { session } = res.locals | ||
|
||
return response(res, 200, true, '', getChatList(session)) | ||
} | ||
|
||
const send = (req, res) => { | ||
const { session } = res.locals | ||
const receiver = formatPhone(req.body.receiver) | ||
const { message } = req.body | ||
|
||
session | ||
.isOnWhatsApp(receiver) | ||
.then((data) => { | ||
if (!data.exists) { | ||
return response(res, 400, false, 'The receiver number cannot be found.') | ||
} | ||
|
||
session | ||
.sendMessage(receiver, message, MessageType.text) | ||
.then(() => { | ||
return response(res, 200, true, 'The message has been successfully sent.') | ||
}) | ||
.catch(() => { | ||
return response(res, 500, false, 'Failed to send the message.') | ||
}) | ||
}) | ||
.catch(() => { | ||
return response(res, 500, false, 'Cannot validate receiver number.') | ||
}) | ||
} | ||
|
||
export { getList, send } |
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,24 @@ | ||
import response from './../response.js' | ||
|
||
const getMessages = (req, res) => { | ||
/* eslint-disable camelcase */ | ||
const { session } = res.locals | ||
const { jid } = req.params | ||
const { limit = 25, cursor_id = null, cursor_fromMe = null } = req.query | ||
const cursor = { | ||
id: cursor_id, | ||
fromMe: cursor_fromMe === null ? cursor_fromMe : cursor_fromMe === 'true', | ||
} | ||
/* eslint-enable camelcase */ | ||
|
||
session | ||
.loadMessages(jid, limit, cursor) | ||
.then((messages) => { | ||
return response(res, 200, true, '', messages) | ||
}) | ||
.catch(() => { | ||
return response(res, 500, false, 'Failed to load messages.') | ||
}) | ||
} | ||
|
||
export default getMessages |
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,37 @@ | ||
import { MessageType } from '@adiwajshing/baileys' | ||
import { getChatList, formatGroup } from './../whatsapp.js' | ||
import response from './../response.js' | ||
|
||
const getList = (req, res) => { | ||
const { session } = res.locals | ||
|
||
return response(res, 200, true, '', getChatList(session, true)) | ||
} | ||
|
||
const send = (req, res) => { | ||
const { session } = res.locals | ||
const receiver = formatGroup(req.body.receiver) | ||
const { message } = req.body | ||
|
||
session | ||
.fetchGroupMetadataFromWA(receiver) | ||
.then((data) => { | ||
if (!data.id) { | ||
return response(res, 400, false, 'The group cannot be found.') | ||
} | ||
|
||
session | ||
.sendMessage(receiver, message, MessageType.text) | ||
.then(() => { | ||
return response(res, 200, true, 'The message has been successfully sent.') | ||
}) | ||
.catch(() => { | ||
return response(res, 500, false, 'Failed to send the message.') | ||
}) | ||
}) | ||
.catch(() => { | ||
return response(res, 500, false, 'Cannot validate group.') | ||
}) | ||
} | ||
|
||
export { getList, send } |
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,28 @@ | ||
import { isSessionExists, createSession, triggerDeleteSession } from './../whatsapp.js' | ||
import response from './../response.js' | ||
|
||
const find = (req, res) => { | ||
if (isSessionExists(req.params.id)) { | ||
return response(res, 200, true, 'Session found.') | ||
} | ||
|
||
return response(res, 404, false, 'Session not found.') | ||
} | ||
|
||
const add = (req, res) => { | ||
const sessionId = req.body.id | ||
|
||
if (isSessionExists(sessionId)) { | ||
return response(res, 409, false, 'Session already exists, please use other id.') | ||
} | ||
|
||
createSession(sessionId, res) | ||
} | ||
|
||
const del = (req, res) => { | ||
triggerDeleteSession(req.params.id) | ||
|
||
return response(res, 200, true, 'The session has been successfully deleted.') | ||
} | ||
|
||
export { find, add, del } |
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,14 @@ | ||
import { validationResult } from 'express-validator' | ||
import response from './../response.js' | ||
|
||
const validate = (req, res, next) => { | ||
const errors = validationResult(req) | ||
|
||
if (!errors.isEmpty()) { | ||
return response(res, 400, false, 'Please fill out all required input.') | ||
} | ||
|
||
next() | ||
} | ||
|
||
export default validate |
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,15 @@ | ||
import { getSession } from '../whatsapp.js' | ||
import response from './../response.js' | ||
|
||
const validate = (req, res, next) => { | ||
const session = getSession(req.query.id) | ||
|
||
if (!session) { | ||
return response(res, 404, false, 'Session not found.') | ||
} | ||
|
||
res.locals.session = session | ||
next() | ||
} | ||
|
||
export default validate |
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,41 @@ | ||
{ | ||
"name": "baileys-api", | ||
"version": "1.0.0", | ||
"description": "A Simple RESTful WhatsApp API", | ||
"main": "app.js", | ||
"type": "module", | ||
"scripts": { | ||
"start": "node ." | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ookamiiixd/baileys-api.git" | ||
}, | ||
"keywords": [ | ||
"whatsapp", | ||
"api", | ||
"rest" | ||
], | ||
"author": "ookamiiixd <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/ookamiiixd/baileys-api/issues" | ||
}, | ||
"homepage": "https://github.com/ookamiiixd/baileys-api#readme", | ||
"engines": { | ||
"node": ">=14.5.0" | ||
}, | ||
"dependencies": { | ||
"@adiwajshing/baileys": "^3.5.3", | ||
"express": "^4.17.2", | ||
"express-validator": "^6.14.0", | ||
"qrcode": "^1.5.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.5.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-config-xo": "^0.39.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"prettier": "^2.5.1" | ||
} | ||
} |
Oops, something went wrong.