├── example.env
├── package.json
├── README.md
├── index.js
└── .gitignore
/example.env:
--------------------------------------------------------------------------------
1 | COUNTRY_CODE="521"
2 | NUMBER="XXXXXXXXXX"
3 | MSG="Hello world!"
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "whatsapp-bot",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1",
8 | "start": "node index"
9 | },
10 | "author": "oscar-aguilar",
11 | "license": "ISC",
12 | "dependencies": {
13 | "dotenv": "^8.2.0",
14 | "qrcode-terminal": "^0.12.0",
15 | "whatsapp-web.js": "^1.12.5"
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
Basic WhatsApp Bot 👋
2 |
3 | > Basic WhatsApp bot using NodeJS and whatsapp-web.js
4 |
5 | ## Install
6 |
7 | ```sh
8 | npm install
9 | ```
10 |
11 | ## Usage
12 |
13 | First you have to locate the ```example.env``` file, rename it to ```.env```
14 |
15 | Now replace the variable values with yours
16 |
17 | ```sh
18 | COUNTRY_CODE="521" # Replace it with your country code
19 | NUMBER="XXXXXXXXXX" # Replace it with your target phone number
20 | MSG="Hello, world!" # The message you want to send
21 | ```
22 |
23 | And now you are ready to run the bot
24 |
25 | ```sh
26 | npm start
27 | ```
28 |
29 | ## Author
30 |
31 | 👤 **Oscar Aguilar**
32 |
33 | - Website: oscar-aguilar.site
34 | - Twitter: [@oscar_aguilaar](https://twitter.com/oscar_aguilaar)
35 | - Github: [@oscar-aguilaar](https://github.com/oscar-aguilaar)
36 |
37 | ## Show your support
38 |
39 | Give a ⭐️ if this project helped you!
40 |
41 | This was a fun made project, if you want to change something, PR's are welcome
42 |
43 | ---
44 |
45 | _This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_
46 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | require('dotenv').config()
2 | const fs = require("fs");
3 | const qrcode = require("qrcode-terminal");
4 | const { Client } = require("whatsapp-web.js");
5 |
6 | // Path where the session data will be stored
7 | const SESSION_FILE_PATH = "./session.json";
8 | // Environment variables
9 | const country_code = process.env.COUNTRY_CODE;
10 | const number = process.env.NUMBER;
11 | const msg = process.env.MSG;
12 |
13 | // Load the session data if it has been previously saved
14 | let sessionData;
15 | if (fs.existsSync(SESSION_FILE_PATH)) {
16 | sessionData = require(SESSION_FILE_PATH);
17 | }
18 |
19 | const client = new Client({
20 | session: sessionData,
21 | });
22 |
23 | client.initialize();
24 |
25 | client.on("qr", (qr) => {
26 | qrcode.generate(qr, { small: true });
27 | });
28 |
29 | // Save session values to the file upon successful auth
30 | client.on("authenticated", (session) => {
31 | sessionData = session;
32 | fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function (err) {
33 | if (err) {
34 | console.error(err);
35 | }
36 | });
37 | });
38 |
39 | client.on("auth_failure", msg => {
40 | // Fired if session restore was unsuccessfull
41 | console.error('AUTHENTICATION FAILURE', msg);
42 | })
43 |
44 |
45 | client.on("ready", () => {
46 | console.log("Client is ready!");
47 |
48 | setTimeout(() => {
49 | let chatId = `${country_code}${number}@c.us`;
50 | client.sendMessage(chatId, msg).then((response) => {
51 | if (response.id.fromMe) {
52 | console.log("It works!");
53 | }
54 | })
55 | }, 5000);
56 | });
57 |
58 | client.on("message", message => {
59 | if (message.body === "Hello") {
60 | client.sendMessage(message.from, 'World!');
61 | }
62 | });
63 |
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | # Created by https://www.toptal.com/developers/gitignore/api/node,visualstudiocode
3 | # Edit at https://www.toptal.com/developers/gitignore?templates=node,visualstudiocode
4 |
5 | ### Node ###
6 | # Logs
7 | logs
8 | *.log
9 | npm-debug.log*
10 | yarn-debug.log*
11 | yarn-error.log*
12 | lerna-debug.log*
13 |
14 | # Diagnostic reports (https://nodejs.org/api/report.html)
15 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
16 |
17 | # Runtime data
18 | pids
19 | *.pid
20 | *.seed
21 | *.pid.lock
22 |
23 | # Directory for instrumented libs generated by jscoverage/JSCover
24 | lib-cov
25 |
26 | # Coverage directory used by tools like istanbul
27 | coverage
28 | *.lcov
29 |
30 | # nyc test coverage
31 | .nyc_output
32 |
33 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
34 | .grunt
35 |
36 | # Bower dependency directory (https://bower.io/)
37 | bower_components
38 |
39 | # node-waf configuration
40 | .lock-wscript
41 |
42 | # Compiled binary addons (https://nodejs.org/api/addons.html)
43 | build/Release
44 |
45 | # Dependency directories
46 | node_modules/
47 | jspm_packages/
48 |
49 | # TypeScript v1 declaration files
50 | typings/
51 |
52 | # TypeScript cache
53 | *.tsbuildinfo
54 |
55 | # Optional npm cache directory
56 | .npm
57 |
58 | # Optional eslint cache
59 | .eslintcache
60 |
61 | # Optional stylelint cache
62 | .stylelintcache
63 |
64 | # Microbundle cache
65 | .rpt2_cache/
66 | .rts2_cache_cjs/
67 | .rts2_cache_es/
68 | .rts2_cache_umd/
69 |
70 | # Optional REPL history
71 | .node_repl_history
72 |
73 | # Output of 'npm pack'
74 | *.tgz
75 |
76 | # Yarn Integrity file
77 | .yarn-integrity
78 |
79 | # dotenv environment variables file
80 | .env
81 | .env.test
82 | .env*.local
83 |
84 | # parcel-bundler cache (https://parceljs.org/)
85 | .cache
86 | .parcel-cache
87 |
88 | # Next.js build output
89 | .next
90 |
91 | # Nuxt.js build / generate output
92 | .nuxt
93 | dist
94 |
95 | # Storybook build outputs
96 | .out
97 | .storybook-out
98 | storybook-static
99 |
100 | # rollup.js default build output
101 | dist/
102 |
103 | # Gatsby files
104 | .cache/
105 | # Comment in the public line in if your project uses Gatsby and not Next.js
106 | # https://nextjs.org/blog/next-9-1#public-directory-support
107 | # public
108 |
109 | # vuepress build output
110 | .vuepress/dist
111 |
112 | # Serverless directories
113 | .serverless/
114 |
115 | # FuseBox cache
116 | .fusebox/
117 |
118 | # DynamoDB Local files
119 | .dynamodb/
120 |
121 | # TernJS port file
122 | .tern-port
123 |
124 | # Stores VSCode versions used for testing VSCode extensions
125 | .vscode-test
126 |
127 | # Temporary folders
128 | tmp/
129 | temp/
130 |
131 | ### VisualStudioCode ###
132 | .vscode/*
133 | !.vscode/tasks.json
134 | !.vscode/launch.json
135 | *.code-workspace
136 |
137 | ### VisualStudioCode Patch ###
138 | # Ignore all local history of files
139 | .history
140 | .ionide
141 |
142 | # Session for whatsapp-web.js
143 | session.json
144 |
145 | # Environment variables
146 | .env
147 |
148 | # End of https://www.toptal.com/developers/gitignore/api/node,visualstudiocode
149 |
--------------------------------------------------------------------------------