Skip to content

Commit

Permalink
Continued with the post request
Browse files Browse the repository at this point in the history
  • Loading branch information
JAYBORICHA07 committed Nov 27, 2023
1 parent aab59f3 commit acc45c5
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 45 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type messageDataType = {
phoneNumber : string
message? : string | null
media_blob? : string | null
caption? : string | null
timestamp : string
}
14 changes: 14 additions & 0 deletions src/utils/sendDataSAbackend.ts
Original file line number Diff line number Diff line change
@@ -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 : ''
}
}
77 changes: 36 additions & 41 deletions src/wa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number>(); // Map to store the number of retries for each session
const sessions = new Map<string, any>(); // 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 () => {
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit acc45c5

Please sign in to comment.