-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from Zain-ul-din/baileys
Baileys
- Loading branch information
Showing
21 changed files
with
1,092 additions
and
1,575 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
npm-debug.log | ||
Dockerfile | ||
.dockerignore | ||
auth_info_baileys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
|
||
# local session | ||
/session | ||
/auth_info_baileys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
{ | ||
"semi": true, | ||
"tabWidth": 4, | ||
"tabWidth": 2, | ||
"printWidth": 100, | ||
"singleQuote": true, | ||
"trailingComma": "none" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
FROM node:20 | ||
|
||
# Set the working directory | ||
WORKDIR /app | ||
|
||
# Copy package.json and package-lock.json (if available) | ||
COPY package*.json ./ | ||
COPY yarn*.lock ./ | ||
|
||
# Install dependencies | ||
RUN npx yarn | ||
|
||
# Copy the rest of the application code | ||
COPY . . | ||
|
||
# Expose the port the app runs on | ||
EXPOSE 3000 | ||
|
||
# Define the command to run the application | ||
CMD ["npx", "yarn", "dev"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Code for learning @whiskeysockets/baileys library | ||
*/ | ||
|
||
import makeWASocket, { DisconnectReason, useMultiFileAuthState } from '@whiskeysockets/baileys'; | ||
import { Boom } from '@hapi/boom'; | ||
import * as fs from 'fs'; | ||
|
||
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys'); | ||
|
||
async function connectToWhatsApp() { | ||
const sock = makeWASocket({ | ||
// can provide additional config here | ||
printQRInTerminal: true, | ||
auth: state | ||
}); | ||
|
||
sock.ev.on('connection.update', (update) => { | ||
const { connection, lastDisconnect } = update; | ||
try { | ||
if (connection === 'close' && lastDisconnect) { | ||
const shouldReconnect = | ||
(lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut; | ||
|
||
console.log( | ||
'connection closed due to ', | ||
lastDisconnect.error, | ||
', reconnecting ', | ||
shouldReconnect | ||
); | ||
// reconnect if not logged out | ||
if (shouldReconnect) { | ||
connectToWhatsApp(); | ||
} else { | ||
// clear credentials | ||
if (lastDisconnect.error) | ||
fs.rmSync('./auth_info_baileys', { force: true, recursive: true }); | ||
} | ||
} else if (connection === 'open') { | ||
console.log('opened connection'); | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}); | ||
|
||
sock.ev.on('messages.upsert', async (m) => { | ||
console.log(JSON.stringify(m, undefined, 2)); | ||
|
||
console.log('replying to', m.messages[0].key.remoteJid); | ||
// don't reply to self | ||
if (m.messages[0].key.fromMe) { | ||
console.log('returning this message is from me'); | ||
return; | ||
} | ||
await sock.sendMessage(m.messages[0].key.remoteJid!, { text: 'Hello there!' }); | ||
}); | ||
|
||
sock.ev.on('creds.update', saveCreds); | ||
} | ||
|
||
// run in main file | ||
connectToWhatsApp(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { WhatsAppClient } from './lib/WhatsAppClient'; | ||
import welcomeUser from './services/welcomeUser'; | ||
// import { WhatsAppClient } from './lib/WhatsAppClient'; | ||
// import welcomeUser from './services/welcomeUser'; | ||
|
||
console.log('🤖 starting client...'); | ||
const whatsappClient = new WhatsAppClient(); | ||
whatsappClient.initializeClient(); | ||
// console.log('🤖 starting client...'); | ||
// const whatsappClient = new WhatsAppClient(); | ||
// whatsappClient.initializeClient(); | ||
|
||
whatsappClient.messageEvent.on('self', welcomeUser); | ||
// whatsappClient.messageEvent.on('self', welcomeUser); | ||
|
||
import { connectToWhatsApp } from './lib/baileys'; | ||
connectToWhatsApp(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { downloadMediaMessage } from '@whiskeysockets/baileys'; | ||
import { ChatGptModel } from '../../../models/ChatGptModel'; | ||
import { GeminiModel } from '../../../models/GeminiModel'; | ||
import { GeminiVisionModel } from '../../../models/GeminiVisionModel'; | ||
import { AiModels } from '../../../types/AiModels'; | ||
import { Util } from '../../../util/Util'; | ||
import MessageHandlerParams from '../types'; | ||
|
||
const gptModel = new ChatGptModel(); | ||
const geminiModel = new GeminiModel(); | ||
const geminiVisionModel = new GeminiVisionModel(); | ||
|
||
// handles message | ||
export async function handleMessage({ client, msg, metadata }: MessageHandlerParams) { | ||
const modelToUse = Util.getModelByPrefix(metadata.text) as AiModels; | ||
|
||
if (!modelToUse) return; | ||
|
||
const prompt = metadata.text.split(' ').slice(1).join(' '); | ||
|
||
if (modelToUse === 'ChatGPT' && metadata.msgType === 'text') { | ||
await gptModel.sendMessage({ sender: metadata.sender, prompt }, async (res, err) => { | ||
client.sendMessage(metadata.remoteJid, { text: err || res }, { quoted: msg }); | ||
}); | ||
return; | ||
} | ||
|
||
if (modelToUse === 'Gemini' && metadata.msgType === 'text') { | ||
await geminiModel.sendMessage({ sender: metadata.sender, prompt }, async (res, err) => { | ||
client.sendMessage(metadata.remoteJid, { text: err || res }, { quoted: msg }); | ||
}); | ||
return; | ||
} | ||
|
||
// Generative Models | ||
if ( | ||
modelToUse === 'GeminiVision' && | ||
metadata.msgType === 'image' && | ||
metadata.imgMetaData.caption | ||
) { | ||
const { mimeType, url } = metadata.imgMetaData; | ||
if (mimeType === 'image/jpeg') { | ||
const buffer = await downloadMediaMessage(msg, 'buffer', {}); | ||
await geminiVisionModel.sendMessage( | ||
{ sender: metadata.sender, prompt: { prompt, buffer, mimeType } }, | ||
async (res, err) => { | ||
client.sendMessage(metadata.remoteJid, { text: err || res }, { quoted: msg }); | ||
} | ||
); | ||
return; | ||
} | ||
} | ||
|
||
client.sendMessage(metadata.remoteJid, { text: `Hi It's WAAI BOT here 🤖` }, { quoted: msg }); | ||
} | ||
|
||
// handles message from self | ||
export async function handlerMessageFromMe({ metadata, client, msg, type }: MessageHandlerParams) { | ||
if (metadata.fromMe && metadata.isQuoted) return; | ||
if (metadata.isQuoted && Util.getModelByPrefix(metadata.text)) return; | ||
await handleMessage({ metadata, client, msg, type }); | ||
} |
Oops, something went wrong.