Skip to content

Commit

Permalink
Merge pull request #1 from KMJ-007/self-msg
Browse files Browse the repository at this point in the history
  • Loading branch information
KMJ-007 authored Oct 31, 2023
2 parents bfe101c + f24caf4 commit f45099b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions src/controller/messageController.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { Request, Response } from "express";
import { Request, Response, application } from "express";
import { getSession, jidExist } from "../wa";
import { logger } from "../shared";
import { proto } from "@whiskeysockets/baileys";
import { delay as delayMS } from "@whiskeysockets/baileys";

const messageController = {
send: async (req:Request, res:Response) => {
send: async (req:Request, res:Response, ) => {
try {
const { jid, type = 'number', message, options } = req.body;
const newMsg = { text : `Following msg was sent to the ${jid.slice(2,12)} \n ${message.text}` }
// @ts-ignore
const session = getSession(req.query.sessionId);
// console.log(session)
const exists = await jidExist(session, jid, type);
if (!exists) return res.status(400).json({ error: 'JID does not exists' });
console.log('sending message')
console.log('sending message')
// console.log();
const result = await session.sendMessage(jid, message, options);

res.status(200).json(result);
const msgSelf = await session.sendMessage(session.user.id, newMsg, options)
res.status(200).json({result, msgSelf});

} catch (error) {
const message = 'An error occured during message send';
Expand All @@ -28,6 +29,7 @@ const messageController = {
let sessionId:any = req.query.sessionId;
const session = getSession(sessionId)!;
const results: { index: number; result: proto.WebMessageInfo | undefined }[] = [];
const selfMsgResults : { index: number; selfMsgresult: proto.WebMessageInfo | undefined }[] = [];
const errors: { index: number; error: string }[] = [];
for (const [
index,
Expand All @@ -41,8 +43,11 @@ const messageController = {
}

if (index > 0) await delayMS(delay);
const newMsg = { text : ` \n Following msg was sent to the ${jid.slice(2,12)} \n \n ${message.text}` }
const result = await session.sendMessage(jid, message, options);
const selfMsgresult = await session.sendMessage(session.user.id, newMsg, options);
results.push({ index, result });
selfMsgResults.push({ index, selfMsgresult });
} catch (e) {
const message = 'An error occured during message send';
logger.error(e, message);
Expand All @@ -52,8 +57,8 @@ const messageController = {

res
.status(req.body.length !== 0 && errors.length === req.body.length ? 500 : 200)
.json({ results, errors });
}
.json({ results, selfMsgResults, errors });
},
}

export default messageController;
2 changes: 1 addition & 1 deletion src/wa.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import makeWASocket, { AnyMessageContent, AuthenticationCreds, ConnectionState, DisconnectReason, SocketConfig, delay, fetchLatestBaileysVersion, initAuthCreds, makeCacheableSignalKeyStore, useMultiFileAuthState } from "@whiskeysockets/baileys";
import makeWASocket, { ConnectionState, DisconnectReason, SocketConfig, fetchLatestBaileysVersion, makeCacheableSignalKeyStore } from "@whiskeysockets/baileys";
import { Boom } from '@hapi/boom';
import { Response } from "express";
import { logger, prisma } from "./shared";
Expand Down

0 comments on commit f45099b

Please sign in to comment.