Skip to content

Commit

Permalink
created a better way to send custom messages using sendCustom()
Browse files Browse the repository at this point in the history
  • Loading branch information
Briuor committed Feb 23, 2020
1 parent f55ec18 commit d35fe4a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 23 deletions.
46 changes: 42 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
const wbm = require('wbm');

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);
})

Expand All @@ -32,12 +32,36 @@ wbm.start().then(async () => {
const wbm = require('wbm');

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()
})

```

## API
Expand All @@ -54,6 +78,20 @@ Type: `array`
Message to send to every phone number.<br />
Type: `string`

### 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)

Send message to a phone number.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"name": "wbm",
"description": "wbm is an API to send bulk message.",
"version": "1.0.1",
"version": "1.0.3",
"main": "src/index.js",
"devDependencies": {},
"repository": {
Expand Down
61 changes: 46 additions & 15 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const puppeteer = require("puppeteer");
const qrcode = require("qrcode-terminal");
const ora = require('ora');
const ora = require("ora");

let browser = null;
let page = null;
let spinner = ora();
let counter = { fails: 0, success: 0 }

/**
* Initialize browser, page and setup page desktop mode
*/
async function start() {
browser = await puppeteer.launch({
headless: true,
Expand All @@ -23,17 +26,20 @@ async function start() {
await generateQRCode();
}

/**
* 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(() => {
let qrcodeDiv = document.querySelector("div[data-ref]");
return qrcodeDiv.getAttribute("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 });
}

/**
Expand All @@ -42,13 +48,13 @@ async function generateQRCode() {
* Send message to a phone number
*/
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);
spinner.succeed(`${phone} Sent`);
counter.success++;
Expand All @@ -67,22 +73,47 @@ async function send(phones, message) {
for (let phone of phones) {
await sendTo(phone, message);
}
await browser.close();
showResult();
await end();
}

async function end() {
await browser.close();
showResult();
/**
* @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();
}

function showResult() {
/**
* @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();
spinner.info(`Result: ${counter.success} sent, ${counter.fails} failed`);
}

module.exports = {
start,
send,
sendTo,
sendCustom,
end
}
21 changes: 18 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@ const wbm = require('./src/index');
// await wbm.send(phones, message);
// })

// 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()
})

0 comments on commit d35fe4a

Please sign in to comment.