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
fc0848f
commit 08aa94a
Showing
18 changed files
with
8,614 additions
and
190 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,4 @@ | ||
HOST=127.0.0.1 | ||
PORT=8000 | ||
MAX_RETRIES=5 | ||
RECONNECT_INTERVAL=5000 |
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,2 +1 @@ | ||
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,2 @@ | ||
package.json | ||
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
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,37 +1,71 @@ | ||
import { MessageType } from '@adiwajshing/baileys' | ||
import { getChatList, formatPhone } from './../whatsapp.js' | ||
import { getSession, 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)) | ||
return response(res, 200, true, '', getChatList(res.locals.sessionId)) | ||
} | ||
|
||
const send = (req, res) => { | ||
const { session } = res.locals | ||
const send = async (req, res) => { | ||
const session = getSession(res.locals.sessionId) | ||
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.') | ||
try { | ||
const [isExists] = await session.onWhatsApp(receiver) | ||
|
||
if (!isExists.exists) { | ||
return response(res, 400, false, 'The receiver number is not exists.') | ||
} | ||
|
||
await session.sendMessage(receiver, { text: message }) | ||
|
||
response(res, 200, true, 'The message has been successfully sent.') | ||
} catch { | ||
response(res, 500, false, 'Failed to send the message.') | ||
} | ||
} | ||
|
||
const sendBulk = async (req, res) => { | ||
const session = getSession(res.locals.sessionId) | ||
const errors = [] | ||
|
||
for (const [key, data] of req.body.entries()) { | ||
if (!data.receiver || !data.message) { | ||
errors.push(key) | ||
|
||
continue | ||
} | ||
|
||
data.receiver = formatPhone(data.receiver) | ||
|
||
try { | ||
const [isExists] = await session.onWhatsApp(data.receiver) | ||
|
||
if (!isExists.exists) { | ||
errors.push(key) | ||
|
||
continue | ||
} | ||
|
||
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.') | ||
}) | ||
await session.sendMessage(data.receiver, { text: data.message }) | ||
} catch { | ||
errors.push(key) | ||
} | ||
} | ||
|
||
if (errors.length === 0) { | ||
return response(res, 200, true, 'All messages has been successfully sent.') | ||
} | ||
|
||
const isAllFailed = errors.length === req.body.length | ||
|
||
response( | ||
res, | ||
isAllFailed ? 500 : 200, | ||
!isAllFailed, | ||
isAllFailed ? 'Failed to send all messages.' : 'Some messages has been successfully sent.', | ||
{ errors } | ||
) | ||
} | ||
|
||
export { getList, send } | ||
export { getList, send, sendBulk } |
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,24 +1,27 @@ | ||
import { getSession } from '../whatsapp.js' | ||
import response from './../response.js' | ||
|
||
const getMessages = (req, res) => { | ||
const getMessages = async (req, res) => { | ||
const session = getSession(res.locals.sessionId) | ||
|
||
/* 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', | ||
const { limit = 25, cursor_id = null } = req.query | ||
|
||
const cursor = {} | ||
|
||
if (cursor_id) { | ||
cursor.before = { id: cursor_id } | ||
} | ||
/* 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.') | ||
}) | ||
try { | ||
const messages = await session.store.loadMessages(jid, limit, 'before' in cursor ? cursor : null) | ||
|
||
response(res, 200, true, '', messages) | ||
} catch { | ||
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 |
---|---|---|
@@ -1,37 +1,28 @@ | ||
import { MessageType } from '@adiwajshing/baileys' | ||
import { getChatList, formatGroup } from './../whatsapp.js' | ||
import { getSession, 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)) | ||
return response(res, 200, true, '', getChatList(res.locals.sessionId, true)) | ||
} | ||
|
||
const send = (req, res) => { | ||
const { session } = res.locals | ||
const send = async (req, res) => { | ||
const session = getSession(res.locals.sessionId) | ||
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.') | ||
} | ||
try { | ||
const groupMeta = await session.groupMetadata(receiver) | ||
|
||
if (!groupMeta.id) { | ||
return response(res, 400, false, 'The group is not exists.') | ||
} | ||
|
||
await session.sendMessage(receiver, { text: message }) | ||
|
||
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.') | ||
}) | ||
response(res, 200, true, 'The message has been successfully sent.') | ||
} catch { | ||
response(res, 500, false, 'Failed to send the message.') | ||
} | ||
} | ||
|
||
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 |
---|---|---|
@@ -1,28 +1,35 @@ | ||
import { isSessionExists, createSession, triggerDeleteSession } from './../whatsapp.js' | ||
import { isSessionExists, createSession, getSession, deleteSession } 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.') | ||
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.') | ||
return response(res, 409, false, 'Session already exists, please use another id.') | ||
} | ||
|
||
createSession(sessionId, res) | ||
} | ||
|
||
const del = (req, res) => { | ||
triggerDeleteSession(req.params.id) | ||
const del = async (req, res) => { | ||
const sessionId = req.params.id | ||
|
||
return response(res, 200, true, 'The session has been successfully deleted.') | ||
try { | ||
await getSession(sessionId).logout() | ||
} catch { | ||
} finally { | ||
deleteSession(sessionId) | ||
} | ||
|
||
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,6 @@ | ||
import { dirname } from 'path' | ||
import { fileURLToPath } from 'url' | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
|
||
export default __dirname |
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
Oops, something went wrong.