{"id":255,"date":"2023-11-28T21:11:26","date_gmt":"2023-11-28T21:11:26","guid":{"rendered":"https:\/\/arizu.id\/blog\/?p=255"},"modified":"2023-11-28T21:11:26","modified_gmt":"2023-11-28T21:11:26","slug":"whatsapp-bot-message-nodejs","status":"publish","type":"post","link":"https:\/\/arizu.id\/blog\/whatsapp-bot-message-nodejs\/","title":{"rendered":"Creating a WhatsApp Bot Message Using Node.js","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p>Hey there, tech enthusiasts! Ever wondered about simplifying communication through WhatsApp? Well, buckle up because we&#8217;re diving into the world of bots and Node.js to create a WhatsApp send message bot!<\/p>\n<p>WhatsApp has revolutionized how we connect, and with the power of Node.js, we can take it a step further. This guide will walk you through the process of setting up a WhatsApp bot using Node.js.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/arizu.id\/blog\/whatsapp-bot-message-nodejs\/#What_is_WhatsApp\" title=\"What is WhatsApp?\">What is WhatsApp?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/arizu.id\/blog\/whatsapp-bot-message-nodejs\/#What_is_Nodejs\" title=\"What is Node.js\">What is Node.js<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/arizu.id\/blog\/whatsapp-bot-message-nodejs\/#Advantages_of_Using_Nodejs\" title=\"Advantages of Using Node.js\">Advantages of Using Node.js<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/arizu.id\/blog\/whatsapp-bot-message-nodejs\/#How_to_install_nodejs_on_windows\" title=\"How to install node.js on windows\">How to install node.js on windows<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/arizu.id\/blog\/whatsapp-bot-message-nodejs\/#How_to_install_nodejs_on_linux\" title=\"How to install node.js on linux\">How to install node.js on linux<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/arizu.id\/blog\/whatsapp-bot-message-nodejs\/#What_is_WhatsApp_bot_message\" title=\"What is WhatsApp bot message?\">What is WhatsApp bot message?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/arizu.id\/blog\/whatsapp-bot-message-nodejs\/#Are_WhatsApp_bot_messages_Safe_and_Legal\" title=\"Are WhatsApp bot messages Safe and Legal?\">Are WhatsApp bot messages Safe and Legal?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/arizu.id\/blog\/whatsapp-bot-message-nodejs\/#Creating_a_WhatsApp_Send_Message_Bot_Using_Nodejs\" title=\"Creating a WhatsApp Send Message Bot Using Node.js\">Creating a WhatsApp Send Message Bot Using Node.js<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/arizu.id\/blog\/whatsapp-bot-message-nodejs\/#Step_1_Installation\" title=\"Step 1: Installation\">Step 1: Installation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/arizu.id\/blog\/whatsapp-bot-message-nodejs\/#Step_2_Nodejs_Script\" title=\"Step 2: Node.js Script\">Step 2: Node.js Script<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/arizu.id\/blog\/whatsapp-bot-message-nodejs\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_is_WhatsApp\"><\/span>What is WhatsApp?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>WhatsApp is a widely-used messaging platform that allows users to send text and voice messages, make voice and video calls, share media like photos and videos, and engage in group chats. It was founded in 2009 and acquired by Facebook in 2014. Operating as an app for smartphones, WhatsApp initially gained popularity for its ease of use, cost-effectiveness (it uses internet data rather than SMS), and its end-to-end encryption for secure messaging.<\/p>\n<figure id=\"attachment_258\" aria-describedby=\"caption-attachment-258\" style=\"width: 428px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-258\" src=\"https:\/\/arizu.id\/blog\/wp-content\/uploads\/2023\/11\/Cara-Aktifkan-Fitur-Chat-Lock-WhatsApp-3660880654.webp\" alt=\"Creating a WhatsApp Bot Message Using Node.js\" width=\"428\" height=\"285\" title=\"\"><figcaption id=\"caption-attachment-258\" class=\"wp-caption-text\">images sources : ayo bogor<\/figcaption><\/figure>\n<p>The platform has evolved to include features like status updates, which allow users to share temporary photos and videos with their contacts, and WhatsApp Business, designed for small businesses to interact with customers through the app. With its user-friendly interface and widespread adoption globally, WhatsApp has become a go-to platform for personal and professional communication.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_is_Nodejs\"><\/span>What is Node.js<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Node.js is an open-source, server-side platform built on Chrome&#8217;s V8 JavaScript engine. It allows developers to run JavaScript code outside of a web browser, enabling the execution of JavaScript on the server side. Initially released in 2009 by Ryan Dahl, Node.js has gained widespread popularity due to its efficiency in building scalable and high-performance applications.<\/p>\n<figure id=\"attachment_257\" aria-describedby=\"caption-attachment-257\" style=\"width: 430px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-257\" src=\"https:\/\/arizu.id\/blog\/wp-content\/uploads\/2023\/11\/1200px-Node.js_logo.svg_.png\" alt=\"Creating a WhatsApp Bot Message Using Node.js\" width=\"430\" height=\"263\" title=\"\" srcset=\"https:\/\/arizu.id\/blog\/wp-content\/uploads\/2023\/11\/1200px-Node.js_logo.svg_.png 1200w, https:\/\/arizu.id\/blog\/wp-content\/uploads\/2023\/11\/1200px-Node.js_logo.svg_-768x470.png 768w\" sizes=\"auto, (max-width: 430px) 100vw, 430px\" \/><figcaption id=\"caption-attachment-257\" class=\"wp-caption-text\">images source : wikipedia<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Advantages_of_Using_Nodejs\"><\/span>Advantages of Using Node.js<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Node.js offers several advantages that make it a popular choice for developers and businesses:<\/p>\n<ol>\n<li><strong>Highly Scalable:<\/strong> Its non-blocking, event-driven architecture allows Node.js to handle a large number of concurrent connections efficiently. This makes it ideal for building applications that require scalability and real-time capabilities, such as chat applications or streaming services.<\/li>\n<li><strong>Fast and Efficient:<\/strong> Node.js is built on Chrome&#8217;s V8 JavaScript engine, known for its speed and performance. Its event-driven, non-blocking I\/O model enhances the speed of data-intensive applications by optimizing throughput and minimizing delays.<\/li>\n<li><strong>Unified Language:<\/strong> Using JavaScript for both client-side and server-side development allows for a more streamlined and efficient development process. Developers can share code and resources between the front end and back end, reducing the learning curve and enhancing productivity.<\/li>\n<li><strong>Rich Ecosystem (npm):<\/strong> Node Package Manager (npm) provides access to a vast repository of open-source libraries and modules. This extensive ecosystem simplifies development by offering a wide range of pre-built modules that developers can leverage to expedite the development process.<\/li>\n<li><strong>Community Support:<\/strong> Node.js has a robust and active community of developers contributing to its growth. This community-driven ecosystem results in continuous improvements, updates, and the sharing of best practices, providing valuable support to developers.<\/li>\n<li><strong>Microservices Architecture:<\/strong> Node.js is well-suited for building microservices-based architectures due to its lightweight nature and scalability. It allows developers to create small, independent services that can be developed, deployed, and scaled individually.<\/li>\n<li><strong>Real-Time Applications:<\/strong> Node.js excels in building real-time applications like chat applications, online gaming platforms, collaboration tools, and streaming services. Its event-driven architecture enables handling multiple concurrent connections efficiently, making real-time updates seamless.<\/li>\n<li><strong>Cross-Platform Compatibility:<\/strong> Node.js applications can be developed and run across different platforms, including Windows, macOS, and Linux, providing flexibility and ease of deployment.<\/li>\n<\/ol>\n<p>These advantages make Node.js a compelling choice for building modern, high-performance applications, especially those requiring scalability, real-time capabilities, and a responsive user experience.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_install_nodejs_on_windows\"><\/span>How to install node.js on windows<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Node.js is a powerful tool for server-side JavaScript programming, and installing it on Windows is a straightforward process. Follow these steps to get Node.js up and running on your Windows machine:<\/p>\n<p><strong>Step 1: Download Node.js<\/strong><\/p>\n<ol>\n<li><strong>Visit the Node.js website:<\/strong> Go to <a href=\"https:\/\/nodejs.org\/\" target=\"_new\" rel=\"noopener\">nodejs.org<\/a> using your web browser.<\/li>\n<li><strong>Download the installer:<\/strong> On the website, click on the &#8220;Downloads&#8221; button and select the Windows installer package (usually an .msi file). Choose the version you prefer (LTS or Current), considering your project&#8217;s requirements.<\/li>\n<\/ol>\n<p><strong>Step 2: Run the Installer<\/strong><\/p>\n<ol>\n<li><strong>Open the downloaded file:<\/strong> Once the installer is downloaded, locate the file (typically in your Downloads folder) and double-click to run it.<\/li>\n<li><strong>Start the installation:<\/strong> Follow the installation wizard instructions. Click &#8220;Next&#8221; or &#8220;Continue&#8221; as prompted.<\/li>\n<\/ol>\n<p><strong>Step 3: Complete the Installation<\/strong><\/p>\n<ol>\n<li><strong>Choose installation location:<\/strong> You&#8217;ll be asked to select the destination folder for Node.js. Usually, the default location is recommended unless you have specific preferences.<\/li>\n<li><strong>Finalize installation:<\/strong> Proceed with the installation process and wait for it to finish. Node.js will be installed along with npm (Node Package Manager).<\/li>\n<\/ol>\n<p><strong>Step 4: Verify the Installation<\/strong><\/p>\n<ol>\n<li><strong>Open Command Prompt:<\/strong> Type &#8220;cmd&#8221; in the Windows search bar and open Command Prompt.<\/li>\n<li><strong>Check Node.js version:<\/strong> In the Command Prompt, type <code>node -v<\/code> and press Enter. This command will display the installed Node.js version.<\/li>\n<li><strong>Verify npm installation:<\/strong> Additionally, type <code>npm -v<\/code> and press Enter. This command will show the installed npm version.<\/li>\n<\/ol>\n<p>If you see version numbers displayed for both Node.js and npm, congratulations! Node.js has been successfully installed on your Windows system.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_install_nodejs_on_linux\"><\/span>How to install node.js on linux<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><strong>Step 1: Update Package Manager<\/strong><\/p>\n<p>Before installing Node.js, it&#8217;s good practice to update your system&#8217;s package manager to ensure you have the latest information about available packages. Open the terminal and run the following commands based on your Linux distribution:<\/p>\n<p>For Debian\/Ubuntu:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo apt update\r\nsudo apt upgrade\r\n<\/pre>\n<p>For Fedora:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo dnf update\r\n<\/pre>\n<p><strong>Step 2: Install Node.js Using Package Manager<\/strong><\/p>\n<p>Most Linux distributions have Node.js available in their default repositories. Use the package manager to install Node.js:<\/p>\n<p>For Debian\/Ubuntu:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo apt install nodejs\r\n<\/pre>\n<p>For Fedora:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo dnf install nodejs\r\n<\/pre>\n<p>This command will install both Node.js and npm (Node Package Manager).<\/p>\n<p><strong>Step 3: Verify the Installation<\/strong><\/p>\n<p>Once the installation is complete, verify that Node.js and npm have been installed successfully. Open the terminal and type the following commands:<\/p>\n<p>Check Node.js version:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">node -v\r\n<\/pre>\n<p>Verify npm installation:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">npm -v\r\n<\/pre>\n<p>If you see version numbers displayed for both Node.js and npm, the installation was successful.<\/p>\n<p><strong>Step 4: Additional Notes (Optional)<\/strong><\/p>\n<p>If your Linux distribution&#8217;s default repository provides an outdated version of Node.js, consider using NodeSource or another Node.js version manager (like nvm) to install the latest version. These tools allow you to manage multiple Node.js versions on your system.<\/p>\n<div class=\"w-full text-token-text-primary\" data-testid=\"conversation-turn-8\">\n<div class=\"px-4 py-2 justify-center text-base md:gap-6 md:py-6 m-auto\">\n<div class=\"flex flex-1 text-base mx-auto gap-3 md:px-5 lg:px-1 xl:px-5 md:max-w-3xl lg:max-w-[40rem] xl:max-w-[48rem] } group\">\n<div class=\"relative flex w-full flex-col lg:w-[calc(100%-115px)]\">\n<div class=\"flex-col gap-1 md:gap-3\">\n<div class=\"flex flex-grow flex-col max-w-full\">\n<div class=\"min-h-[20px] text-message flex flex-col items-start gap-3 whitespace-pre-wrap break-words [.text-message+&amp;]:mt-5 overflow-x-auto\" data-message-author-role=\"user\" data-message-id=\"aaa288dc-59b3-465a-a0e5-0012cc4ec4ee\">\n<h2><span class=\"ez-toc-section\" id=\"What_is_WhatsApp_bot_message\"><\/span>What is WhatsApp bot message?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<\/div>\n<\/div>\n<div class=\"mt-1 flex justify-start gap-3 empty:hidden\">\n<div class=\"text-gray-400 flex self-end lg:self-center justify-center lg:justify-start mt-0 gap-1 visible\">\n<div class=\"flex items-center gap-1.5 text-xs\">\n<p>A WhatsApp bot message refers to an automated message sent by a bot on the WhatsApp platform. These bots are programmed to interact with users, respond to inquiries, perform tasks, or provide information based on predefined instructions or AI algorithms.<\/p>\n<p>WhatsApp bots can be designed to serve various purposes, such as:<\/p>\n<ol>\n<li><strong>Customer Service:<\/strong> Bots can handle customer queries, provide support, and offer information about products or services.<\/li>\n<li><strong>Automated Responses:<\/strong> They can send automated replies to frequently asked questions or specific keywords.<\/li>\n<li><strong>Notifications and Alerts:<\/strong> Bots can send notifications, reminders, or alerts to users based on certain triggers or events.<\/li>\n<li><strong>Transactional Purposes:<\/strong> They facilitate transactions, bookings, or reservations directly through the WhatsApp platform.<\/li>\n<li><strong>Content Distribution:<\/strong> Bots can deliver personalized content, such as news updates, weather forecasts, or educational materials.<\/li>\n<\/ol>\n<p>WhatsApp bot messages can be initiated by users interacting with the bot or can be triggered by specific events or queries. They aim to streamline communication, automate repetitive tasks, and enhance user experiences on the platform.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Are_WhatsApp_bot_messages_Safe_and_Legal\"><\/span>Are WhatsApp bot messages Safe and Legal?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The safety and legality of WhatsApp bot messages largely depend on how they are implemented and used. Here are some considerations:<\/p>\n<ol>\n<li><strong>User Privacy and Data Protection:<\/strong> It&#8217;s crucial for WhatsApp bot developers to adhere to privacy regulations and protect user data. Any collection, storage, or usage of personal information must comply with applicable laws like GDPR (General Data Protection Regulation) or other regional data protection laws.<\/li>\n<li><strong>Consent and Opt-In:<\/strong> Users should have the option to opt in or opt out of receiving messages from a WhatsApp bot. Collecting user consent before sending messages or storing their data is essential for compliance and user trust.<\/li>\n<li><strong>Message Content and Compliance:<\/strong> Messages sent by WhatsApp bots should comply with WhatsApp&#8217;s policies and guidelines. They should not contain spam, malicious content, or violate any laws or regulations related to content distribution, marketing, or communication.<\/li>\n<li><strong>Legal Compliance:<\/strong> Businesses and developers using WhatsApp bots for commercial purposes should ensure compliance with applicable laws and regulations governing communication, marketing, data privacy, and consumer rights.<\/li>\n<li><strong>Respect for Users:<\/strong> WhatsApp bots should respect users&#8217; preferences, refrain from excessive messaging, and provide easy opt-out options. They should also clearly identify themselves as bots to avoid confusion.<\/li>\n<\/ol>\n<p>WhatsApp has its own policies and terms of service that dictate how bots and automated messages should be used on their platform. Violating these policies can result in penalties, restrictions, or account suspension.<\/p>\n<p>As with any technology, the responsibility lies with developers and businesses to implement WhatsApp bots in a manner that prioritizes user privacy, complies with laws and regulations, and respects ethical considerations.<\/p>\n<figure id=\"attachment_259\" aria-describedby=\"caption-attachment-259\" style=\"width: 561px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-259\" src=\"https:\/\/arizu.id\/blog\/wp-content\/uploads\/2023\/11\/Qr_code_mkrfyt.webp\" alt=\"Creating a WhatsApp Bot Message Using Node.js\" width=\"561\" height=\"336\" title=\"\" srcset=\"https:\/\/arizu.id\/blog\/wp-content\/uploads\/2023\/11\/Qr_code_mkrfyt.webp 800w, https:\/\/arizu.id\/blog\/wp-content\/uploads\/2023\/11\/Qr_code_mkrfyt-768x460.webp 768w\" sizes=\"auto, (max-width: 561px) 100vw, 561px\" \/><figcaption id=\"caption-attachment-259\" class=\"wp-caption-text\">images sources : jagad yudha awali<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Creating_a_WhatsApp_Send_Message_Bot_Using_Nodejs\"><\/span>Creating a WhatsApp Send Message Bot Using Node.js<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Step_1_Installation\"><\/span>Step 1: Installation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Make sure you have Node.js installed on your computer. Then create a folder for your project and open a terminal or command prompt inside the folder.<\/p>\n<p>Then run the following command to install the required modules:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">npm init -y\r\nnpm install express whatsapp-web.js qrcode-terminal\r\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Step_2_Nodejs_Script\"><\/span><strong>Step 2: Node.js Script<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Below is the Node.js script that enables you to send messages via WhatsApp and provides HTTP access for sending messages:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const express = require('express');\r\nconst { Client } = require('whatsapp-web.js');\r\nconst qrcode = require('qrcode-terminal');\r\nconst fs = require('fs');\r\n\r\nconst SESSION_FILE_PATH = '.\/session.txt';\r\n\r\nlet sessionCfg;\r\nif (fs.existsSync(SESSION_FILE_PATH)) {\r\n    sessionCfg = fs.readFileSync(SESSION_FILE_PATH, 'utf-8');\r\n}\r\n\r\nconst client = new Client({ puppeteer: { headless: true }, session: JSON.parse(sessionCfg) });\r\n\r\nclient.on('qr', qr =&gt; {\r\n    qrcode.generate(qr, { small: true });\r\n});\r\n\r\nclient.on('authenticated', session =&gt; {\r\n    console.log('Authenticated');\r\n    fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), (err) =&gt; {\r\n        if (err) console.error(err);\r\n    });\r\n});\r\n\r\nclient.on('ready', () =&gt; {\r\n    console.log('Client is ready!');\r\n\r\n    const app = express();\r\n    const PORT = 3000;\r\n\r\n    app.get('\/send-message', (req, res) =&gt; {\r\n        const targetNumber = req.query.number;\r\n        const message = req.query.message;\r\n\r\n        if (!targetNumber || !message) {\r\n            return res.status(400).send('Number and message parameters are required');\r\n        }\r\n\r\n        client.sendMessage(`${targetNumber}@c.us`, message).then(() =&gt; {\r\n            console.log('Message sent!');\r\n            res.status(200).send('Message sent!');\r\n        }).catch(err =&gt; {\r\n            console.error('Error:', err);\r\n            res.status(500).send('Error sending message');\r\n        });\r\n    });\r\n\r\n    app.listen(PORT, () =&gt; {\r\n        console.log(`Server running on port ${PORT}`);\r\n    });\r\n});\r\n\r\nclient.initialize();\r\n<\/pre>\n<p><strong>Usage:<\/strong><\/p>\n<ol>\n<li>Save this script in a file with the <code>.js<\/code> extension, for example, <code>whatsapp-bot.js<\/code>.<\/li>\n<li>Run the script with the command <code>node whatsapp-bot.js<\/code>.<\/li>\n<li>After running it, a QR code will appear in the terminal. Use your phone to scan the QR code through your WhatsApp application to link the bot to your WhatsApp account.<\/li>\n<li>Once successfully connected, access <code>http:\/\/localhost:3000\/send-message?number=recipient_number&amp;message=Your_message<\/code> in a browser or through a GET request, adjusting the recipient number and the message you want to send.<\/li>\n<\/ol>\n<p>Remember to use this WhatsApp bot responsibly and in accordance with WhatsApp&#8217;s terms of service and applicable laws in your region.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Congratulations! You&#8217;ve created a simple Node.js script that sends a WhatsApp message using Twilio&#8217;s API. You can expand upon this by integrating more functionalities, handling user input, or deploying the bot to a server for continuous operation.<\/p>\n<p>Experiment with the Twilio API to explore more capabilities and create personalized WhatsApp bots tailored to your needs.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>Hey there, tech enthusiasts! Ever wondered about simplifying communication through WhatsApp? Well, buckle up because&#8230;<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":256,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[204,203],"newstopic":[205],"class_list":["post-255","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorial","tag-node-js","tag-whatsapp-bot","newstopic-bot"],"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/arizu.id\/blog\/wp-json\/wp\/v2\/posts\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arizu.id\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arizu.id\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arizu.id\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arizu.id\/blog\/wp-json\/wp\/v2\/comments?post=255"}],"version-history":[{"count":1,"href":"https:\/\/arizu.id\/blog\/wp-json\/wp\/v2\/posts\/255\/revisions"}],"predecessor-version":[{"id":260,"href":"https:\/\/arizu.id\/blog\/wp-json\/wp\/v2\/posts\/255\/revisions\/260"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/arizu.id\/blog\/wp-json\/wp\/v2\/media\/256"}],"wp:attachment":[{"href":"https:\/\/arizu.id\/blog\/wp-json\/wp\/v2\/media?parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arizu.id\/blog\/wp-json\/wp\/v2\/categories?post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arizu.id\/blog\/wp-json\/wp\/v2\/tags?post=255"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/arizu.id\/blog\/wp-json\/wp\/v2\/newstopic?post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}