From 2d2bafc73805813329ff5e27c96fcc5e93d71b27 Mon Sep 17 00:00:00 2001 From: JAYBORICHA Date: Thu, 15 Feb 2024 17:03:51 +0530 Subject: [PATCH] Tried to send msg through child process not working rn --- src/utils/child_process.js | 14 ++++++++++++++ src/utils/sendDataSAbackend.ts | 35 +++++++++++++++++++++++----------- src/wa.ts | 4 +++- 3 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 src/utils/child_process.js diff --git a/src/utils/child_process.js b/src/utils/child_process.js new file mode 100644 index 0000000..7fdcac5 --- /dev/null +++ b/src/utils/child_process.js @@ -0,0 +1,14 @@ +import axios from 'axios'; + +process.stdin.setEncoding('utf8'); + +process.stdin.on('data', async (data) => { + const requestData = JSON.parse(data); + + try { + const response = await axios(requestData); + console.log(response.data); + } catch (error) { + console.error('Error sending data:', error); + } +}); diff --git a/src/utils/sendDataSAbackend.ts b/src/utils/sendDataSAbackend.ts index e22113e..8a0cba7 100644 --- a/src/utils/sendDataSAbackend.ts +++ b/src/utils/sendDataSAbackend.ts @@ -1,14 +1,27 @@ -import axios from 'axios' -import { messageDataType } from '../types' +import { spawn } from 'child_process'; +import axios from 'axios'; +import { messageDataType } from '../types'; - -export const sendDataSAbackend = async (messageData : messageDataType) => { - const apiUrl = `` +export const sendDataSAbackend = async (messageData: messageDataType) => { + const apiUrl = 'YOUR_BACKEND_ENDPOINT_URL'; const body = { - method : 'post', - url : apiUrl, - data : messageData, - headers : '' - } -} \ No newline at end of file + method: 'post', + url: apiUrl, + data: messageData, + headers: { 'Content-Type': 'application/json' } // Assuming JSON data + }; + + const child = spawn('node', ['child_process.js'], { stdio: 'inherit' }); // Spawning child process + + child.on('exit', (code) => { + console.log(`Child process exited with code ${code}`); + }); + + child.on('error', (err) => { + console.error('Failed to start subprocess.', err); + }); + + child?.stdin?.write(JSON.stringify(body)); // Sending data to child process + child?.stdin?.end(); +}; diff --git a/src/wa.ts b/src/wa.ts index 9b68795..dd0bcdb 100644 --- a/src/wa.ts +++ b/src/wa.ts @@ -1,4 +1,4 @@ -import makeWASocket, { ConnectionState, DisconnectReason, SocketConfig, fetchLatestBaileysVersion, makeCacheableSignalKeyStore, downloadMediaMessage } from "@whiskeysockets/baileys"; +import makeWASocket, { ConnectionState, DisconnectReason, SocketConfig, fetchLatestBaileysVersion, makeCacheableSignalKeyStore, downloadMediaMessage, mediaMessageSHA256B64 } from "@whiskeysockets/baileys"; import { Boom } from '@hapi/boom'; import { Response } from "express"; import { logger, prisma } from "./shared"; @@ -224,6 +224,7 @@ export async function createSession(options:createSessionOptions) { messageData.caption = msg?.message?.imageMessage?.caption; messageData.timestamp = (msg.messageTimestamp)?.toString()!; sendDataSAbackend(messageData); + console.log(messageData); // @ts-ignore // await writeFile('./demo.jpeg', Buffer.from(buffer, 'binary').toString('base64')).then(()=>{console.log("image-generated")}); } @@ -231,6 +232,7 @@ export async function createSession(options:createSessionOptions) { messageData.phoneNumber = msg.key.remoteJid!.slice(2,12); messageData.message = msg.message?.conversation; messageData.timestamp = (msg.messageTimestamp)?.toString()!; + console.log(messageData); sendDataSAbackend(messageData); } }