From acc45c56e5f1713cfb880f027aba370e2e51afac Mon Sep 17 00:00:00 2001 From: JAYBORICHA Date: Mon, 27 Nov 2023 13:53:28 +0530 Subject: [PATCH] Continued with the post request --- package.json | 1 + src/prisma/schema.prisma | 6 +++ src/types.ts | 7 ++++ src/utils/sendDataSAbackend.ts | 14 +++++++ src/wa.ts | 77 ++++++++++++++++------------------ yarn.lock | 8 ++-- 6 files changed, 68 insertions(+), 45 deletions(-) create mode 100644 src/types.ts create mode 100644 src/utils/sendDataSAbackend.ts diff --git a/package.json b/package.json index 14d8303..25d246c 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "dependencies": { "@prisma/client": "^5.2.0", "@whiskeysockets/baileys": "^6.4.1", + "axios": "^1.6.2", "baileys-bottle": "^2.1.2", "express": "^4.18.2", "express-validator": "^7.0.1", diff --git a/src/prisma/schema.prisma b/src/prisma/schema.prisma index 4eb13ed..e8035ba 100644 --- a/src/prisma/schema.prisma +++ b/src/prisma/schema.prisma @@ -20,3 +20,9 @@ model Session { @@unique([sessionId, id], map: "unique_id_per_session_id") @@index([sessionId]) } + +model Proxy { + pkId Int @id @default(autoincrement()) + productName String @db.VarChar(128) + api_endpoint String @db.VarChar(128) +} \ No newline at end of file diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..e451502 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,7 @@ +export type messageDataType = { + phoneNumber : string + message? : string | null + media_blob? : string | null + caption? : string | null + timestamp : string + } \ No newline at end of file diff --git a/src/utils/sendDataSAbackend.ts b/src/utils/sendDataSAbackend.ts new file mode 100644 index 0000000..e22113e --- /dev/null +++ b/src/utils/sendDataSAbackend.ts @@ -0,0 +1,14 @@ +import axios from 'axios' +import { messageDataType } from '../types' + + +export const sendDataSAbackend = async (messageData : messageDataType) => { + const apiUrl = `` + + const body = { + method : 'post', + url : apiUrl, + data : messageData, + headers : '' + } +} \ No newline at end of file diff --git a/src/wa.ts b/src/wa.ts index 9ac1cfd..9b68795 100644 --- a/src/wa.ts +++ b/src/wa.ts @@ -5,20 +5,14 @@ import { logger, prisma } from "./shared"; import { useSession } from "./store/session"; import * as qrcode from 'qrcode'; import { writeFile } from 'fs/promises' +import { sendDataSAbackend } from "./utils/sendDataSAbackend"; +import { messageDataType } from "./types"; const retries = new Map(); // Map to store the number of retries for each session const sessions = new Map(); // Map to store the socket for each session const RECONNECT_INTERVAL = Number(process.env.RECONNECT_INTERVAL || 0); const MAX_RECONNECT_RETRIES = Number(process.env.MAX_RECONNECT_RETRIES || 5); -type messageData = { - phoneNumber : string - message? : string | null - media_blob? : string | null - caption? : string | null - timestamp : string -} - export const SESSION_CONFIG_ID = 'session-config' export const init = async () => { @@ -202,42 +196,43 @@ export async function createSession(options:createSessionOptions) { const upsert = events['messages.upsert'] if(upsert.type === 'notify') { for(const msg of upsert.messages) { - // @ts-ignore - const messageType = Object.keys(msg.message)[0] - console.log(messageType); - if(messageType == 'imageMessage' || messageType == 'conversation'){ - let messageData : messageData = { - phoneNumber : '', - message : '', - media_blob : '', - caption : '', - timestamp : '' - } - if(messageType == 'imageMessage'){ - const buffer = await downloadMediaMessage( - msg, - 'buffer', - { }, - { - logger, - reuploadRequest : sock.updateMediaMessage - } - ) - messageData.phoneNumber = msg.key.remoteJid!.slice(2,12); - // @ts-ignore - messageData.media_blob = Buffer.from(buffer, 'binary').toString('base64'); - messageData.caption = msg?.message?.imageMessage?.caption; - messageData.timestamp = (msg.messageTimestamp)?.toString()!; - console.log(messageData); - // @ts-ignore - // await writeFile('./demo.jpeg', Buffer.from(buffer, 'binary').toString('base64')).then(()=>{console.log("image-generated")}); - }else if(messageType == 'conversation' || messageType == 'extendedTextMessage'){ - if(!msg.key.fromMe){ + if(!msg.key.fromMe){ + // @ts-ignore + const messageType = Object.keys(msg.message)[0] + console.log(messageType); + if(messageType == 'imageMessage' || messageType == 'conversation'){ + let messageData : messageDataType = { + phoneNumber : '', + message : '', + media_blob : '', + caption : '', + timestamp : '' + } + if(messageType == 'imageMessage'){ + const buffer = await downloadMediaMessage( + msg, + 'buffer', + { }, + { + logger, + reuploadRequest : sock.updateMediaMessage + } + ) + messageData.phoneNumber = msg.key.remoteJid!.slice(2,12); + // @ts-ignore + messageData.media_blob = Buffer.from(buffer, 'binary').toString('base64'); + messageData.caption = msg?.message?.imageMessage?.caption; + messageData.timestamp = (msg.messageTimestamp)?.toString()!; + sendDataSAbackend(messageData); + // @ts-ignore + // await writeFile('./demo.jpeg', Buffer.from(buffer, 'binary').toString('base64')).then(()=>{console.log("image-generated")}); + } + else if(messageType == 'conversation' || messageType == 'extendedTextMessage'){ messageData.phoneNumber = msg.key.remoteJid!.slice(2,12); messageData.message = msg.message?.conversation; messageData.timestamp = (msg.messageTimestamp)?.toString()!; - console.log(messageData); - } + sendDataSAbackend(messageData); + } } } } diff --git a/yarn.lock b/yarn.lock index d9c6f56..1b2c232 100644 --- a/yarn.lock +++ b/yarn.lock @@ -539,10 +539,10 @@ axios@^0.24.0: dependencies: follow-redirects "^1.14.4" -axios@^1.3.3: - version "1.5.0" - resolved "https://registry.npmjs.org/axios/-/axios-1.5.0.tgz" - integrity sha512-D4DdjDo5CY50Qms0qGQTTw6Q44jl7zRwY7bthds06pUGfChBCTcQs+N743eFWGEd6pRTMd6A+I87aWyFV5wiZQ== +axios@^1.3.3, axios@^1.6.2: + version "1.6.2" + resolved "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz" + integrity sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A== dependencies: follow-redirects "^1.15.0" form-data "^4.0.0"