Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Maintained by Jeanlucafp
This API is a forked from @salman0ansari and maintained by me.

# Archive Notice 🔒
After three years, I've decided to archive this open-source WhatsApp API project. Your support and contributions have been incredible!

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"husky": "^8.0.2",
"lint-staged": "^13.0.4",
"mocha": "^10.1.0",
"nodemon": "^2.0.20",
"nodemon": "^3.1.4",
"prettier": "^2.8.0",
"supertest": "^6.3.1"
}
Expand Down
47 changes: 42 additions & 5 deletions src/api/class/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const {
DisconnectReason,
PHONENUMBER_MCC
} = require('@whiskeysockets/baileys')
const { unlinkSync } = require('fs')
const { v4: uuidv4 } = require('uuid')
const path = require('path')
const processButton = require('../helper/processbtn')
const generateVC = require('../helper/genVc')
const Chat = require('../models/chat.model')
Expand All @@ -19,6 +17,7 @@ const logger = require('pino')()
const useMongoDBAuthState = require('../helper/mongoAuthState')
const { AuditMessages } = require('./audit')
const libPhonenumber = require('libphonenumber-js')
const TypeBot = require('./typebot')

class WhatsAppInstance {
socketConfig = {
Expand All @@ -41,6 +40,7 @@ class WhatsAppInstance {
messages: [],
qrRetry: 0,
customWebhook: '',
typebot: null
}

axiosInstance = axios.create({
Expand Down Expand Up @@ -75,15 +75,22 @@ class WhatsAppInstance {

async init() {
this.collection = mongoClient.db('whatsapp-api').collection(this.key)
const { state, saveCreds } = await useMongoDBAuthState(this.collection)
this.authState = { state: state, saveCreds: saveCreds }
const { state, saveCreds, writeData } = await useMongoDBAuthState(this.collection)
this.authState = { state: state, saveCreds: saveCreds, writeData }
this.socketConfig.auth = this.authState.state
this.socketConfig.browser = Object.values(config.browser)
this.instance.sock = makeWASocket(this.socketConfig)
this.setHandler()
return this
}

async activeTypeBot(apiHost, typebotname, saveData = true) {
const typebot = new TypeBot(apiHost, typebotname, this)
this.instance.typebot = typebot
if (saveData)
await this.authState.writeData(typebot.getObjectToSave(), 'typebot')
}

setHandler() {
const sock = this.instance.sock
// on credentials update save state
Expand Down Expand Up @@ -306,6 +313,31 @@ class WhatsAppInstance {
)
)
await this.SendWebhook('message', webhookData, this.key)

if (this.instance.typebot) {
// extendedTextMessage quando recebe mensagem do web-whatsapp
if (messageType === 'extendedTextMessage') {
await this.instance.typebot.startTypebot({
message: msg.message.extendedTextMessage.text,
remoteJid: msg.key.remoteJid
})
}

// conversation quando recebe mensagem do celular
if (messageType === 'conversation') {
await this.instance.typebot.startTypebot({
message: msg.message.conversation,
remoteJid: msg.key.remoteJid
})
}

if (messageType === 'templateButtonReplyMessage') {
await this.instance.typebot.startTypebot({
message: TypeBot.giveMeTextButtonAndIGiveUId(msg.message.templateButtonReplyMessage.selectedDisplayText),
remoteJid: msg.key.remoteJid
})
}
}
})
})

Expand Down Expand Up @@ -435,6 +467,10 @@ class WhatsAppInstance {
phone_connected: this.instance?.online,
webhookUrl: this.instance.customWebhook,
user: this.instance?.online ? this.instance.sock?.user : {},
typebot: this.instance?.typebot ? {
apiHost: this.instance.typebot.apiHost,
typebotName: this.instance.typebot.typebotName,
} : {},
}
}

Expand Down Expand Up @@ -477,8 +513,9 @@ class WhatsAppInstance {
async sendUrlMediaFile(to, url, type, mimeType, caption = '') {
await this.verifyId(this.getWhatsAppId(to))

const receiver = this.getWhatsAppId(to)
const data = await this.instance.sock?.sendMessage(
this.getWhatsAppId(to),
receiver,
{
[type]: {
url: url,
Expand Down
12 changes: 11 additions & 1 deletion src/api/class/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@ class Session {
webhook,
webhookUrl
)

const typebotItem = result.find(items => items._id === 'typebot')
if (typebotItem) {
await instance.activeTypeBot(typebotItem.apiHost, typebotItem.typebotName, false)
}

await instance.init()
WhatsAppInstances[key] = instance
})
restoredSessions.push(key)

// TODO:: remover essa gambiarra
if (key !== 'audit_messages') {
restoredSessions.push(key)
}
})
} catch (e) {
logger.error('Error restoring sessions')
Expand Down
Loading