Skip to content

Commit

Permalink
whatsapp Cred Store On Firebase Added
Browse files Browse the repository at this point in the history
  • Loading branch information
keyurboss committed Nov 7, 2023
1 parent af64ba0 commit b90da9f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 6 deletions.
94 changes: 94 additions & 0 deletions apps/server/whatsapp-common/src/firebaseRD.creds.store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-explicit-any */
// import generalConfig from "./assets/general.config.json"
import {
AuthenticationCreds,
AuthenticationState,
BufferJSON,
initAuthCreds,
proto,
} from '@whiskeysockets/baileys';
import { firebaseDb } from './firebase.app';
export const useFireBaseRealTimeDatabaseStoreAuthState = async (
collectionName: string,
serverName: string,
): Promise<{ state: AuthenticationState; saveCreds: () => Promise<void> }> => {
const collection = firebaseDb.ref(collectionName).child(serverName);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const writeData = async (data: any, file: string) => {
data = JSON.stringify(data, BufferJSON.replacer);
console.log('Writing data type ', typeof data);
console.log('Writing data ', data);
collection.child(file).set(data);
return;
};

const readData = async (file: string) => {
try {
console.log('Reading ', file);
const data = await collection.child(file).get();
if (!data.exists()) {
return null;
}
const cc = data.val();
let finalRead = JSON.parse(cc, BufferJSON.reviver);
if (typeof finalRead === 'string') {
finalRead = JSON.parse(finalRead, BufferJSON.reviver);
}
console.log('final REad ', finalRead);
console.log(typeof finalRead);
return finalRead;
} catch (error) {
return null;
}
};

const removeData = async (file: string) => {
try {
await collection.child(file).set(null);
} catch {
//
}
};

const creds: AuthenticationCreds =
(await readData('creds')) || initAuthCreds();

return {
state: {
creds,
keys: {
get: async (type: any, ids: string[]) => {
const data: { [_: string]: any } = {};
await Promise.all(
ids.map(async (id) => {
let value = await readData(`${type}-${id}`);
if (type === 'app-state-sync-key' && value) {
value = proto.Message.AppStateSyncKeyData.fromObject(value);
}

data[id] = value;
}),
);

return data;
},
set: async (data: any) => {
const tasks: Promise<void>[] = [];
for (const category in data) {
for (const id in data[category]) {
const value = data[category][id];
const file = `${category}-${id}`;
tasks.push(value ? writeData(value, file) : removeData(file));
}
}

await Promise.all(tasks);
},
},
},
saveCreds: () => {
return writeData(creds, 'creds');
},
};
};
12 changes: 9 additions & 3 deletions apps/server/whatsapp-common/src/firestore.creds.store.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-explicit-any */
// import generalConfig from "./assets/general.config.json"
import {
Expand All @@ -22,7 +23,7 @@ export const useFireStoreAuthState = async (
data = JSON.stringify(data, BufferJSON.replacer);
collection.doc(serverName).update(
{
[file]: JSON.stringify(data, BufferJSON.replacer),
[file]: data,
},
{
// exists:true
Expand All @@ -40,10 +41,15 @@ export const useFireStoreAuthState = async (
return null;
}
const cc = data.data();
const finalRead = cc?.[file]
let finalRead = cc?.[file]
? JSON.parse(cc?.[file], BufferJSON.reviver)
: null;
console.log('final REad ', finalRead);
if (typeof finalRead === 'string') {
finalRead = JSON.parse(finalRead, BufferJSON.reviver);
}
// debugger
// console.log('final REad ', finalRead);
// console.log(typeof finalRead)
return finalRead;
} catch (error) {
return null;
Expand Down
14 changes: 11 additions & 3 deletions apps/server/whatsapp-common/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import { Boom } from '@hapi/boom';
import makeWASocket, { DisconnectReason } from '@whiskeysockets/baileys';
import _generalConfig from './assets/general.config.json';
import { useFireStoreAuthState } from './firestore.creds.store';
import { useFireBaseRealTimeDatabaseStoreAuthState } from './firebaseRD.creds.store';
import pin from 'pino';

const ServerConfig = {
whatsappLoggedIn: false,
Expand All @@ -13,12 +14,17 @@ const ServerConfig = {
console.log(ServerConfig);

async function connectToWhatsApp() {
const { state, saveCreds } = await useFireStoreAuthState(
const { state, saveCreds } = await useFireBaseRealTimeDatabaseStoreAuthState(
_generalConfig.storeCredCollectionName,
_generalConfig.serverName,
);
// const { state, saveCreds } = await useFireStoreAuthState(
// _generalConfig.storeCredCollectionName,
// _generalConfig.serverName,
// );
const sock = makeWASocket({
auth: state,
logger: pin({ level: 'info' }),
// logger: pin({ level: 'debug' }),
// can provide additional config here
printQRInTerminal: true,
Expand Down Expand Up @@ -46,7 +52,9 @@ async function connectToWhatsApp() {
console.log(connection);
}
});
// sock.ev.on('co')
sock.ev.on('presence.update', (update) => {
console.log(update.id, 'Is Online', update.presences);
});
}
// run in main file
connectToWhatsApp();

0 comments on commit b90da9f

Please sign in to comment.