Comparing version
@@ -9,3 +9,3 @@ { | ||
"description": "wbm is an API to send bulk message.", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"main": "src/index.js", | ||
@@ -12,0 +12,0 @@ "devDependencies": {}, |
@@ -23,4 +23,4 @@ | ||
wbm.start().then(async () => { | ||
const phones = ['5535988841854']; | ||
const message = "good morning"; | ||
const phones = ['5535988841854', '35988841854', '5535988841854']; | ||
const message = 'Good Morning.'; | ||
await wbm.send(phones, message); | ||
@@ -36,8 +36,32 @@ }) | ||
wbm.start().then(async () => { | ||
const contacts = [{ phone: '5535988841854', name: 'Bruno' }]; | ||
const contacts = [{ phone: '5535988841854', name: 'Bruno', age: 21 }]; | ||
const message = 'Hi {{name}}, your age is {{age}}'; | ||
// it will send 'Hi Bruno, your age is 21' | ||
await wbm.sendCustom(contacts, message); | ||
}); | ||
``` | ||
### Send custom messages using YOUR OWN RULE | ||
```javascript | ||
const wbm = require('wbm'); | ||
wbm.start().then(async () => { | ||
const contacts = [ | ||
{ phone: '5535988841854', name: 'Bruno', group: 'friend' }, | ||
{ phone: '5535988841854', name: 'Will', group: 'customer' } | ||
]; | ||
for (contact of contacts) { | ||
let message = 'good morning ' + contact.name; | ||
let message = 'hi'; | ||
if(contact.group === 'customer') { | ||
message = 'Good morning ' + contact.name; | ||
} | ||
else if(contact.group === 'friend') { | ||
message = 'Hey ' + contact.name + '. Wassup?'; | ||
} | ||
await wbm.sendTo(contact.phone, message); | ||
} | ||
await wbm.end() | ||
}) | ||
``` | ||
@@ -59,2 +83,16 @@ | ||
### sendCustom(contacts, message) | ||
Send custom message to every phone number. | ||
##### contacts | ||
Array of contact objects created by the user(with dynamic properties)<br /> | ||
like [{phone: '5535988841854', name: 'Will', group: 'partner', age: 22', any: 'anything', ...}, ...].<br /> | ||
Type: `array` | ||
##### message | ||
Message prototype to send to every phone number, text with curly braces like {{text}}<br /> | ||
will be replaced by the contact property with same text name.<br /> | ||
Type: `string` | ||
### sendTo(phone, message) | ||
@@ -61,0 +99,0 @@ |
const puppeteer = require("puppeteer"); | ||
const qrcode = require("qrcode-terminal"); | ||
const ora = require('ora'); | ||
const ora = require("ora"); | ||
@@ -10,2 +10,5 @@ let browser = null; | ||
/** | ||
* Initialize browser, page and setup page desktop mode | ||
*/ | ||
async function start() { | ||
@@ -27,6 +30,9 @@ browser = await puppeteer.launch({ | ||
/** | ||
* Access whatsapp web page, get QR Code data and generate it on terminal | ||
*/ | ||
async function generateQRCode() { | ||
spinner.start('generating QRCode\n'); | ||
spinner.start("generating QRCode\n"); | ||
await page.goto("https://web.whatsapp.com"); | ||
await page.waitForSelector('div[data-ref]'); | ||
await page.waitForSelector("div[data-ref]"); | ||
const qrcodeData = await page.evaluate(() => { | ||
@@ -37,4 +43,4 @@ let qrcodeDiv = document.querySelector("div[data-ref]"); | ||
qrcode.generate(qrcodeData, { small: true }); | ||
spinner.info('QRCode generated! Scan it using Whatsapp App.'); | ||
await page.waitForSelector('div[data-ref]', { hidden: true }); | ||
spinner.info("QRCode generated! Scan it using Whatsapp App."); | ||
await page.waitForSelector("div[data-ref]", { hidden: true }); | ||
} | ||
@@ -48,9 +54,9 @@ | ||
async function sendTo(phone, message) { | ||
spinner.start('Sending Message\n'); | ||
spinner.start("Sending Message\n"); | ||
await page.goto(`https://web.whatsapp.com/send?phone=${phone}&text=${message}`); | ||
await page.waitForSelector('div#startup'); | ||
await page.waitForSelector('div#startup', { hidden: true }); | ||
await page.waitForSelector("div#startup"); | ||
await page.waitForSelector("div#startup", { hidden: true }); | ||
try { | ||
await page.waitForSelector('div[data-tab="1"]', { timeout: 3000 }); | ||
await page.keyboard.press('Enter'); | ||
await page.keyboard.press("Enter"); | ||
await page.waitFor(1000); | ||
@@ -74,12 +80,36 @@ spinner.succeed(`${phone} Sent`); | ||
} | ||
await browser.close(); | ||
showResult(); | ||
await end(); | ||
} | ||
/** | ||
* @param {array} contacts Array of contacts | ||
* @param {string} message Custom message to send to every phone number | ||
* Send custom message to every phone number | ||
*/ | ||
async function sendCustom(contacts, messagePrototype) { | ||
for (let contact of contacts) { | ||
await sendTo(contact.phone, generateCustomMessage(contact, messagePrototype)); | ||
} | ||
await end(); | ||
} | ||
/** | ||
* @param {object} contact contact with several properties defined by the user | ||
* @param {string} messagePrototype Custom message to send to every phone number | ||
* @returns {string} message | ||
* Replace all text between {{}} to respective contact property | ||
*/ | ||
function generateCustomMessage(contact, messagePrototype) { | ||
let message = messagePrototype; | ||
for (let property in contact) { | ||
message = message.replace(new RegExp(`{{${property}}}`, "g"), contact[property]); | ||
} | ||
return message; | ||
} | ||
/** | ||
* Close browser and show results(number of messages sent and failed) | ||
*/ | ||
async function end() { | ||
await browser.close(); | ||
showResult(); | ||
} | ||
function showResult() { | ||
spinner.info(`Result: ${counter.success} sent, ${counter.fails} failed`); | ||
@@ -92,3 +122,4 @@ } | ||
sendTo, | ||
sendCustom, | ||
end | ||
} |
21
test.js
@@ -9,9 +9,24 @@ const wbm = require('./src/index'); | ||
// wbm.start().then(async () => { | ||
// const contacts = [{ phone: '5535988841854', name: "Bruno", age: 21 }]; | ||
// const message = 'hello {{name}} your age is {{age}}{{age}}'; | ||
// await wbm.sendCustom(contacts, message); | ||
// }); | ||
wbm.start().then(async () => { | ||
const contacts = [{ phone: '5535988841854', name: 'Bruno' }, { phone: '35988841854', name: 'Bruno' }, { phone: '5535988841854', name: 'Bruno' }]; | ||
const contacts = [ | ||
{ phone: '5535988841854', name: 'Bruno', group: 'friend' }, | ||
{ phone: '5535988841854', name: 'Will', group: 'customer' } | ||
]; | ||
for (contact of contacts) { | ||
let message = `hi ${contact.name}`; | ||
let message = 'hi'; | ||
if (contact.group === 'customer') { | ||
message = 'Good morning ' + contact.name; | ||
} | ||
else if (contact.group === 'friend') { | ||
message = 'Hey ' + contact.name + '. Wassup?'; | ||
} | ||
await wbm.sendTo(contact.phone, message); | ||
} | ||
await wbm.end(); | ||
await wbm.end() | ||
}) |
60502
4.67%135
48.35%111
52.05%