innovatorswhatsappclass
Advanced tools
Comparing version
77
index.js
@@ -15,4 +15,5 @@ const { | ||
const path = require('path') | ||
const mime = require('mime-types') | ||
const mime = require("mime"); | ||
class Group { | ||
@@ -31,6 +32,7 @@ constructor(client, groupData) { | ||
class WhatsAppClient extends EventEmitter { | ||
constructor() { | ||
constructor(config = {}) { | ||
super() | ||
this.sock = null | ||
this.isConnected = false | ||
this.sessionName = config.sessionName || 'auth_info_baileys' | ||
this.store = makeInMemoryStore({ logger: P().child({ level: 'silent', stream: 'store' }) }) | ||
@@ -44,3 +46,3 @@ } | ||
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys') | ||
const { state, saveCreds } = await useMultiFileAuthState(this.sessionName) | ||
@@ -61,3 +63,3 @@ const logger = P({ level: 'silent' }) | ||
setInterval(() => { | ||
this.store.writeToFile('./auth_info_baileys/baileys_store.json') | ||
this.store.writeToFile(`./${this.sessionName}/baileys_store.json`) | ||
}, 10_000) | ||
@@ -81,2 +83,5 @@ | ||
this.connect() | ||
} else if (lastDisconnect?.error?.output?.statusCode === DisconnectReason.loggedOut) { | ||
// Handle logout by reinitializing | ||
await this.reinitialize(); | ||
} | ||
@@ -284,12 +289,22 @@ } else if(connection === 'open') { | ||
async isNumberOnWhatsApp(number) { | ||
async isNumberOnWhatsApp(number) { | ||
try { | ||
const profilePictureUrl = await this.sock.profilePictureUrl(number); | ||
return profilePictureUrl; | ||
const results = await this.sock.onWhatsApp(number); | ||
return results.length > 0; | ||
} catch (error) { | ||
console.error("Error getting profile picture:"); | ||
return; | ||
console.error("Error checking number on WhatsApp:", error); | ||
throw error; | ||
} | ||
} | ||
async getProfilePicture(jid) { | ||
try { | ||
const profilePictureUrl = await this.sock.profilePictureUrl(jid); | ||
return profilePictureUrl; | ||
} catch (error) { | ||
console.error("Error getting profile picture:"); | ||
return; | ||
} | ||
} | ||
async getContacts() { | ||
@@ -320,4 +335,46 @@ try { | ||
} | ||
async reinitialize() { | ||
try { | ||
// Clear the authentication state | ||
await fs.promises.rm(this.sessionName, { recursive: true, force: true }); | ||
// Reset connection status | ||
this.isConnected = false; | ||
this.sock = null; | ||
// Attempt to reconnect | ||
await this.connect(); | ||
this.emit('reinitialized'); | ||
} catch (error) { | ||
console.error('Error during reinitialization:', error); | ||
this.emit('error', error); | ||
} | ||
} | ||
async logout() { | ||
try { | ||
// Close the socket connection if it exists | ||
if (this.sock) { | ||
await this.sock.logout() | ||
await this.sock.end() | ||
this.sock = null | ||
} | ||
// Clear authentication data | ||
if (fs.existsSync(this.sessionName)) { | ||
fs.rmSync(this.sessionName, { recursive: true, force: true }) | ||
} | ||
this.isConnected = false | ||
this.emit('logout', 'Logged out successfully') | ||
return true | ||
} catch (error) { | ||
console.error('Logout error:', error) | ||
throw new Error('Failed to logout: ' + error.message) | ||
} | ||
} | ||
} | ||
module.exports = { WhatsAppClient, Group } |
{ | ||
"name": "innovatorswhatsappclass", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "", | ||
@@ -17,4 +17,5 @@ "main": "index.js", | ||
"pino": "^9.6.0", | ||
"qrcode-terminal": "^0.12.0" | ||
"qrcode-terminal": "^0.12.0", | ||
"mime": "^3.0.0" | ||
} | ||
} |
24866
8.06%512
10.34%6
20%+ Added
+ Added