Skip to content

Commit

Permalink
Implemented sending msg to self
Browse files Browse the repository at this point in the history
  • Loading branch information
JAYBORICHA07 committed Oct 29, 2023
1 parent 5cf9e43 commit 50d3344
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 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.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"express-validator": "^7.0.1",
"pm2": "^5.3.0",
"qrcode": "^1.5.3",
"qrcode-terminal": "^0.12.0"
"qrcode-terminal": "^0.12.0",
"typescript": "^5.2.2"
},
"devDependencies": {
"@types/cors": "^2.8.13",
Expand Down
18 changes: 11 additions & 7 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;
// @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(jid);
// console.log(session.user.id);
const result = await session.sendMessage(jid, message, options);

res.status(200).json(result);
const msgSelf = await session.sendMessage(session.user.id, message, 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 @@ -42,7 +44,9 @@ const messageController = {

if (index > 0) await delayMS(delay);
const result = await session.sendMessage(jid, message, options);
const selfMsgresult = await session.sendMessage(session.user.id, message, 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 +56,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;
20 changes: 20 additions & 0 deletions src/prisma/migrations/20231005124705_jay_dev/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Warnings:
- You are about to drop the `Chat` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `Contact` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `GroupMetadata` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `Message` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropTable
DROP TABLE "Chat";

-- DropTable
DROP TABLE "Contact";

-- DropTable
DROP TABLE "GroupMetadata";

-- DropTable
DROP TABLE "Message";
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
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2450,7 +2450,7 @@ [email protected]:
xml2js "^0.4.23"
yargs "^17.3.1"

typescript@*:
typescript@*, typescript@^5.2.2:
version "5.2.2"
resolved "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz"
integrity sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==
Expand Down

0 comments on commit 50d3344

Please sign in to comment.