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
3fb430b
commit 03eff28
Showing
12 changed files
with
300 additions
and
10 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,20 @@ | ||
import type { RequestHandler } from 'express'; | ||
import { logger, prisma } from '../shared'; | ||
|
||
export const list: RequestHandler = async (req, res) => { | ||
try { | ||
const { sessionId } = req.params; | ||
const { cursor = undefined, limit = 25 } = req.query; | ||
const chats = await prisma.chat.findMany({ | ||
cursor: cursor ? { sessionId_id: { id: cursor as string, sessionId } } : undefined, | ||
take: Number(limit), | ||
skip: cursor ? 1 : 0, | ||
}); | ||
|
||
res.status(200).json({ data: chats, cursor: chats.length ? chats[chats.length - 1].id : null }); | ||
} catch (e) { | ||
const message = 'An error occured during chat list'; | ||
logger.error(e, message); | ||
res.status(500).json({ error: message }); | ||
} | ||
}; |
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 @@ | ||
import type { RequestHandler } from 'express'; | ||
import { logger, prisma } from '../shared'; | ||
|
||
export const list: RequestHandler = async (req, res) => { | ||
try { | ||
const { sessionId } = req.params; | ||
const { cursor = undefined, limit = 25 } = req.query; | ||
const groups = await prisma.groupMetadata.findMany({ | ||
cursor: cursor ? { sessionId_id: { id: cursor as string, sessionId } } : undefined, | ||
take: Number(limit), | ||
skip: cursor ? 1 : 0, | ||
}); | ||
|
||
res | ||
.status(200) | ||
.json({ data: groups, cursor: groups.length ? groups[groups.length - 1].id : null }); | ||
} catch (e) { | ||
const message = 'An error occured during group list'; | ||
logger.error(e, message); | ||
res.status(500).json({ error: message }); | ||
} | ||
}; |
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,98 @@ | ||
import type { RequestHandler } from 'express'; | ||
import { delay as delayMs } from '@adiwajshing/baileys'; | ||
import { logger, prisma } from '../shared'; | ||
import { getSession, jidExists } from '../wa'; | ||
|
||
export const send: RequestHandler = async (req, res) => { | ||
try { | ||
const { sessionId } = req.params; | ||
const { jid, type = 'number', message, options } = req.body; | ||
const session = getSession(sessionId)!; | ||
|
||
const exists = await jidExists(session, jid, type); | ||
if (!exists) return res.status(400).json({ error: 'JID does not exist' }); | ||
|
||
const result = await session.sendMessage(jid, message, options); | ||
res.status(200).json(result); | ||
} catch (e) { | ||
const message = 'An error occured during message send'; | ||
logger.error(e, message); | ||
res.status(500).json({ error: message }); | ||
} | ||
}; | ||
|
||
export const sendBulk: RequestHandler = async (req, res) => { | ||
const { sessionId } = req.params; | ||
const session = getSession(sessionId)!; | ||
const results: { index: number; result: any }[] = []; | ||
const errors: { index: number; error: string }[] = []; | ||
|
||
for (const [ | ||
index, | ||
{ jid, type = 'number', delay = 2500, message, options }, | ||
] of req.body.entries()) { | ||
try { | ||
const exists = await jidExists(session, jid, type); | ||
if (!exists) { | ||
errors.push({ index, error: 'JID does not exist' }); | ||
continue; | ||
} | ||
|
||
if (index > 0) await delayMs(delay); | ||
const result = await session.sendMessage(jid, message, options); | ||
results.push({ index, result }); | ||
} catch (e) { | ||
const message = 'An error occured during message send'; | ||
logger.error(e, message); | ||
errors.push({ index, error: message }); | ||
} | ||
} | ||
|
||
res | ||
.status(req.body.length !== 0 && errors.length === req.body.length ? 500 : 200) | ||
.json({ results, errors }); | ||
}; | ||
|
||
export const list: RequestHandler = async (req, res) => { | ||
try { | ||
const { sessionId } = req.params; | ||
const { cursor = undefined, limit = 25 } = req.query; | ||
const messages = await prisma.message.findMany({ | ||
cursor: cursor ? { pkId: Number(cursor) } : undefined, | ||
take: Number(limit), | ||
skip: cursor ? 1 : 0, | ||
where: { sessionId }, | ||
}); | ||
|
||
res | ||
.status(200) | ||
.json({ data: messages, cursor: messages.length ? messages[messages.length - 1].id : null }); | ||
} catch (e) { | ||
const message = 'An error occured during message list'; | ||
logger.error(e, message); | ||
res.status(500).json({ error: message }); | ||
} | ||
}; | ||
|
||
export const find: RequestHandler = async (req, res) => { | ||
try { | ||
const { sessionId, jid } = req.params; | ||
const { cursor = undefined, limit = 25 } = req.query; | ||
const messages = await prisma.message.findMany({ | ||
cursor: cursor | ||
? { sessionId_remoteJid_id: { id: cursor as string, remoteJid: jid, sessionId } } | ||
: undefined, | ||
take: Number(limit), | ||
skip: cursor ? 1 : 0, | ||
orderBy: { messageTimestamp: 'desc' }, | ||
}); | ||
|
||
res | ||
.status(200) | ||
.json({ data: messages, cursor: messages.length ? messages[messages.length - 1].id : null }); | ||
} catch (e) { | ||
const message = 'An error occured during message find'; | ||
logger.error(e, message); | ||
res.status(500).json({ error: message }); | ||
} | ||
}; |
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 @@ | ||
import type { RequestHandler } from 'express'; | ||
import { logger } from '../shared'; | ||
import { getSession, jidExists } from '../wa'; | ||
|
||
export const checkJid: RequestHandler = async (req, res) => { | ||
try { | ||
const { sessionId, jid } = req.params; | ||
const { type = 'number' } = req.query; | ||
const session = getSession(sessionId)!; | ||
const exists = await jidExists(session, jid, type as any); | ||
res.status(200).json({ exists }); | ||
} catch (e) { | ||
const message = 'An error occured during jid check'; | ||
logger.error(e, message); | ||
res.status(500).json({ error: message }); | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Router } from 'express'; | ||
import { list } from '../controllers/chat'; | ||
import { find } from '../controllers/message'; | ||
import sessionValidator from '../middlewares/session-validator'; | ||
import requestValidator from '../middlewares/request-validator'; | ||
import { query } from 'express-validator'; | ||
|
||
const router = Router(); | ||
router.get( | ||
'/', | ||
sessionValidator, | ||
query('cursor').isString(), | ||
query('limit').isNumeric(), | ||
requestValidator, | ||
list | ||
); | ||
router.get( | ||
'/:jid', | ||
sessionValidator, | ||
query('cursor').isString(), | ||
query('limit').isNumeric(), | ||
requestValidator, | ||
find | ||
); | ||
|
||
export default 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,26 @@ | ||
import { Router } from 'express'; | ||
import { list } from '../controllers/group'; | ||
import { find } from '../controllers/message'; | ||
import sessionValidator from '../middlewares/session-validator'; | ||
import requestValidator from '../middlewares/request-validator'; | ||
import { query } from 'express-validator'; | ||
|
||
const router = Router(); | ||
router.get( | ||
'/', | ||
sessionValidator, | ||
query('cursor').isString(), | ||
query('limit').isNumeric(), | ||
requestValidator, | ||
list | ||
); | ||
router.get( | ||
'/:jid', | ||
sessionValidator, | ||
query('cursor').isString(), | ||
query('limit').isNumeric(), | ||
requestValidator, | ||
find | ||
); | ||
|
||
export default 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 |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import { Router } from 'express'; | ||
import sessionRoutes from './sessions'; | ||
import chatRoutes from './chats'; | ||
import groupRoutes from './groups'; | ||
import messageRoutes from './messages'; | ||
import miscRoutes from './misc'; | ||
|
||
const router = Router(); | ||
router.use('/sessions', sessionRoutes); | ||
router.all('*', (req, res) => res.status(404).json({ error: 'URL not found' })); | ||
router.use('/:sessionId/chats', chatRoutes); | ||
router.use('/:sessionId/groups', groupRoutes); | ||
router.use('/:sessionId/messages', messageRoutes); | ||
router.use('/:sessionId/misc', miscRoutes); | ||
|
||
export default 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,34 @@ | ||
import { Router } from 'express'; | ||
import { body, query } from 'express-validator'; | ||
import * as controller from '../controllers/message'; | ||
import requestValidator from '../middlewares/request-validator'; | ||
import sessionValidator from '../middlewares/session-validator'; | ||
|
||
const router = Router(); | ||
router.get( | ||
'/', | ||
sessionValidator, | ||
query('cursor').isString(), | ||
query('limit').isNumeric(), | ||
requestValidator, | ||
controller.list | ||
); | ||
router.post( | ||
'/send', | ||
sessionValidator, | ||
body('jid').isString().notEmpty(), | ||
body('type').isString().isIn(['group', 'number']), | ||
body('message').isObject().notEmpty(), | ||
body('options').isObject(), | ||
requestValidator, | ||
controller.send | ||
); | ||
router.post( | ||
'/send-bulk', | ||
sessionValidator, | ||
body().isArray().notEmpty(), | ||
requestValidator, | ||
controller.sendBulk | ||
); | ||
|
||
export default 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,16 @@ | ||
import { Router } from 'express'; | ||
import * as controller from '../controllers/misc'; | ||
import sessionValidator from '../middlewares/session-validator'; | ||
import requestValidator from '../middlewares/request-validator'; | ||
import { query } from 'express-validator'; | ||
|
||
const router = Router(); | ||
router.get( | ||
'/check-jid/:jid', | ||
sessionValidator, | ||
query('type').isString().isIn(['group', 'number']), | ||
requestValidator, | ||
controller.checkJid | ||
); | ||
|
||
export default 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
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