Skip to content

Commit

Permalink
Merge pull request ookamiiixd#2 from ookamiiixd/es6
Browse files Browse the repository at this point in the history
Es6
  • Loading branch information
ookamiiixd authored Jan 2, 2022
2 parents 0b37b56 + 2b7dfc4 commit fc0848f
Show file tree
Hide file tree
Showing 21 changed files with 1,902 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
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
22 changes: 22 additions & 0 deletions .eslintrc.json
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"]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
package-lock.json
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 120,
"tabWidth": 4,
"semi": false,
"singleQuote": true
}
32 changes: 32 additions & 0 deletions README.md
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**.
18 changes: 18 additions & 0 deletions app.js
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
37 changes: 37 additions & 0 deletions controllers/chatController.js
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 }
24 changes: 24 additions & 0 deletions controllers/getMessages.js
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
37 changes: 37 additions & 0 deletions controllers/groupController.js
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 }
28 changes: 28 additions & 0 deletions controllers/sessionController.js
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 }
14 changes: 14 additions & 0 deletions middleware/requestValidator.js
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
15 changes: 15 additions & 0 deletions middleware/sessionValidator.js
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
41 changes: 41 additions & 0 deletions package.json
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"
}
}
Loading

0 comments on commit fc0848f

Please sign in to comment.