Skip to content

Commit 38f1ccc

Browse files
authored
Merge pull request #34 from louiCoder/Anekenonso-patch-2
Add read and write to files functionality
2 parents 59e9166 + 93d9969 commit 38f1ccc

File tree

4 files changed

+46
-26
lines changed

4 files changed

+46
-26
lines changed

App/CronJobs/cronJob.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const cron = require("node-cron");
22
// const express = require("express"); // import is not necessary since we will not run the job in this file.
3+
// const fs = require("fs"); // we shall use this file-system module at a later stage when needed.
34
// const fs = require("fs"); // we shall you this file-system module at a later stage when needed.
45
const sendWhatsappMessage = require('../Whatsapp/whatsapp');
56
const telNumbers = require('../Whatsapp/numbers');
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function* writeToTextFile() {
2+
fs.writeFile('Output.txt', data, (err) => {
3+
4+
// In case of a error throw err.
5+
if (err) throw err;
6+
})
7+
}
8+
9+
function readFromTextFile() {
10+
11+
}
12+
13+
module.exports = { writeToTextFile, readFromTextFile }

App/Whatsapp/whatsapp.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,34 @@ const client = require('twilio')(accountSid, authToken);
55
const whatsappNumbers = require('./numbers')
66

77
// function that handles sending of the whatsapp messages
8-
function sendWhatsappMessage (whatsappNumbers) {
8+
function sendMessage(numbers) {
9+
function sendWhatsappMessage(whatsappNumbers) {
910

10-
// to handle multiple numbers we need to create this loop below
11-
// sending a message to each number.
12-
whatsappNumbers.forEach(num => {
13-
// send message
14-
client.messages
15-
.create({
16-
// body should be the message you are sending. you can insert anything you feel like
17-
body: `Hello, this is an automated whatsapp message from Node webscrapper project. This message proves that the app is working fine`,
18-
// for now we are using the twilio sandbox untill production then it can be changed
19-
from: `whatsapp:${process.env.SANDBOX_WHATSAPP_NUMBER}`,
20-
// THIS NUMBER IS KEPT AS AN ENVIRONMENTAL VARIABLE IN THE nodemon.json file, key is number
21-
to: `whatsapp:${num}`
22-
})
23-
// resolve
24-
.then(message => {
25-
// message was sent successsfully
26-
console.log(message._solution)
27-
})
28-
.catch(error => {
29-
// an error occured and message was not sent
30-
console.log(`An error occured ${error}`)
31-
})
32-
.done();
33-
})
34-
}
11+
// to handle multiple numbers we need to create this loop below
12+
// sending a message to each number.
13+
whatsappNumbers.forEach(num => {
14+
// send message
15+
client.messages
16+
.create({
17+
// body should be the message you are sending. you can insert anything you feel like
18+
body: `Hello, this is an automated whatsapp message from Node webscrapper project. This message proves that the app is working fine`,
19+
// for now we are using the twilio sandbox untill production then it can be changed
20+
from: `whatsapp:${process.env.SANDBOX_WHATSAPP_NUMBER}`,
21+
// THIS NUMBER IS KEPT AS AN ENVIRONMENTAL VARIABLE IN THE nodemon.json file, key is number
22+
to: `whatsapp:${num}`
23+
})
24+
// resolve
25+
.then(message => {
26+
// message was sent successsfully
27+
console.log(message._solution)
28+
})
29+
.catch(error => {
30+
// an error occured and message was not sent
31+
console.log(`An error occured ${error}`)
32+
})
33+
.done();
34+
})
35+
}
3536

3637
// let's export the function here.
3738
module.exports = sendWhatsappMessage;

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ We look forward to using this idea to be able to gather online analytics from di
1010
---
1111
run `npm install` or simply `npm i` to install dependencies for the application.
1212

13+
| Author | Github handle | Twitter handle |
14+
| ------------- |:-------------:|
15+
| Louis Musanje Michael | [louicoder](github.com/louicoder)|
1316
#### CronJobs:
1417
---
1518
We automate a few tasks like sending emails, SMSs and whatsapp messages using this particualr feature. To start using the cron-job function, simply import the function you want to be executed into the croJob.js file and execute within the cronJob function
@@ -20,6 +23,8 @@ const cronJob = () => cron.schedule("*/10 * * * * *", function () {
2023
sendMessage(telNumbers); // uncomment to send automated whtsapp messages here.
2124
});
2225
26+
| Collaborators | Github handle | Twitter handle |
27+
| ------------- |:-------------:|
2328
Example:
2429
========
2530
0 8 * * * every day at 8am

0 commit comments

Comments
 (0)