salman0ansari / whatsapp-api-nodejs

RESTful WhatsApp API with Multiple Device Support
GNU General Public License v3.0
1.27k stars 606 forks source link

Restore one instance #728

Closed LuisArteModelo closed 1 year ago

LuisArteModelo commented 1 year ago

How can restore only one instance?

sairmali commented 1 year ago

+1

sairmali commented 1 year ago

any update ?

LuisArteModelo commented 1 year ago

I'm create a function ahead inside a "src\api\class\session.js" file, create a route in "src\api\routes\instance.route.js"

async restoreSessionByKey(key) { let restoredSessions = new Array() try { const db = mongoClient.db('whatsapp-api') const query = {} db.collection(key) .find(query) .toArray(async (err, result) => { if (err) throw err const webhook = !config.webhookEnabled ? undefined : config.webhookEnabled const webhookUrl = !config.webhookUrl ? undefined : config.webhookUrl const instance = new WhatsAppInstance( key, webhook, webhookUrl ) await instance.init() WhatsAppInstances[key] = instance }) restoredSessions.push(key) } catch (e) { logger.error('Error restoring session') logger.error(e) } return restoredSessions }

sairmali commented 1 year ago

it is working from your side ?

LuisArteModelo commented 1 year ago

Yes

sairmali commented 1 year ago
async restoreSessionByKey(key) {

    let restoredSessions = new Array()
    try {
        const db = mongoClient.db('whatsapp-api')
        const query = {}
        db.collection(key).find(query).toArray(async (err, result) => {
            if (err) throw err 
            const webhook = !config.webhookEnabled ? undefined : config.webhookEnabled
            const webhookUrl = !config.webhookUrl ? undefined : config.webhookUrl
            const instance = new WhatsAppInstance(key, webhook, webhookUrl)
            await instance.init()
            WhatsAppInstances[key] = instance
        })
        restoredSessions.push(key)
    }
    catch (e) {
        logger.error('Error restoring session')
        logger.error(e)
    }
    return restoredSessions

}
sairmali commented 1 year ago

Error: Route.get() requires a callback function but got a [object Undefined] at Route. [as get] (\whatsapp-api-nodejs-main\node_modules\express\lib\router\route.js:211:15) at Object. (\whatsapp-api-nodejs-main\src\api\routes\instance.route.js:12:38) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) at Object. (\whatsapp-api-nodejs-main\src\api\routes\index.js:3:24) at Module._compile (node:internal/modules/cjs/loader:1105:14)

sairmali commented 1 year ago

router.route('/restoreSessionByKey').get(controller.restoreSessionByKey)

achdiadsyah commented 1 year ago

may be on typescript version this feature will be done to