const { Client, LocalAuth, RemoteAuth } = require('whatsapp-web.js'); var qrcode = require('qrcode-terminal'); const express = require('express') const bodyParser = require('body-parser'); const app = express() const port = 3000 app.use(bodyParser.json()) // for parsing application/json app.use(bodyParser.urlencoded({ extended: true })) const client = new Client({ authStrategy: new LocalAuth() }); client.on('qr', (qr) => { // Generate and scan this code with your phone qrcode.generate(qr, {small: true}); console.log('qr code ready'); }); client.on('ready', () => { console.log('Client is ready!'); }); client.on('message', (msg) => { const message = msg._data; console.log('recived message from ' + message.from + 'content: ' + message.body); const chat_id = '62882007050601' + '@c.us'; client.sendMessage(chat_id, 'client recived message'); }); client.on('change_state', (state) => { console.log('status client is' + state); }); client.initialize(); app.post('/send', function (req, res) { headers = JSON.stringify(req.headers); auth = headers.authorization body = req.body chat_id = body.phone + '@c.us'; client.sendMessage(chat_id, body.message); res.send('Hello World'); }) app.get('/', function (req, res) { res.send('Hello World') }) app.listen(port, () => { console.info(`Server started on port ${port}...`) });
Write, Run & Share NodeJS code online using OneCompiler's NodeJS online compiler for free. It's one of the robust, feature-rich online compilers for NodeJS language,running on the latest LTS version NodeJS 16.14.2. Getting started with the OneCompiler's NodeJS editor is easy and fast. The editor shows sample boilerplate code when you choose language as NodeJS and start coding. You can provide the dependencies in package.json
.
Node.js is a free and open-source server environment. Node.js is very popular in recent times and a large number of companies like Microsoft, Paypal, Uber, Yahoo, General Electric and many others are using Node.js.
Google chrome's javascript engine V8
and is pretty fast.Asynchronous
, event-driven
and works on single-thread model
thus eliminating the dis-advantages of multi-thread model.Express is one of the most popular web application framework in the NodeJS echosystem.
let moment = require('moment');
console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));
const _ = require("lodash");
let colors = ['blue', 'green', 'yellow', 'red'];
let firstElement = _.first(colors);
let lastElement = _.last(colors);
console.log(`First element: ${firstElement}`);
console.log(`Last element: ${lastElement}`);
Following are the libraries supported by OneCompiler's NodeJS compiler.