Let's WhatsApp using Bash
PC: Forbes

Let's WhatsApp using Bash

AuthorAuthorIntroduction

In today's fast-paced world, automation plays a crucial role in streamlining various tasks. As a tech enthusiast, I recently explored a fascinating project that allows me to send WhatsApp messages using a simple Bash script. In this article, I'll walk you through the steps I took to achieve this automation and how you can do it too.

Sending WhatsApp messages using Bash can be achieved by interacting with the WhatsApp Web service programmatically.

Prerequisites

  • Make sure you have curl installed on your system. Most Linux distributions come with curl pre-installed, but you can install it if needed.
  • You need a way to obtain the WhatsApp Web session token. You can do this manually by logging in to WhatsApp Web using your phone and inspecting the network requests to find the session token. Alternatively, you can use third-party libraries or tools to automate this process, like "yowsup" for Python.


Step 1: Obtaining the WhatsApp Web Session Token

To interact with WhatsApp Web programmatically, we need a session token. This token can be obtained by logging in to WhatsApp Web using your phone and inspecting the network requests. This process allows you to authenticate and establish a connection with WhatsApp servers.


Step 2: Create a Bash script

Once we have the session token, we can create a Bash script that will interact with WhatsApp Web. The script uses the 'curl' command-line tool to make HTTP requests to the WhatsApp servers. We can define a function to send WhatsApp messages, taking the recipient's phone number and the message as input.

Now, create a Bash script (e.g., send_whatsapp.sh) with the following content:


#!/bin/bash


# Replace this with your WhatsApp Web session token
SESSION_TOKEN="YOUR_SESSION_TOKEN"


# Function to send a WhatsApp message
send_whatsapp_message() {
? ? local phone_number="$1"
? ? local message="$2"
? ? local url="https://web.whatsapp.com/send"


? ? # Compose the message data
? ? local data="phone=${phone_number}&text=$(urlencode "${message}")"


? ? # Send the HTTP POST request
? ? curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -b "wa-session=${SESSION_TOKEN}" -d "${data}" "${url}"
}


# Function to URL encode the message
urlencode() {
? ? echo -n "${1}" | xxd -plain | tr -d '\n' | sed 's/\(..\)/%\1/g'
}


# Usage example:
phone_number="RECIPIENT_PHONE_NUMBER_WITH_COUNTRY_CODE"
message="Hello, this is a test message sent using Bash!"
send_whatsapp_message "${phone_number}" "${message}"        

Replace YOUR_SESSION_TOKEN with the WhatsApp Web session token you obtained in Step 2. Also, ensure that the recipient's phone number is provided with the country code (e.g., +1 for the United States).

Make the script executable

Run the following command in your terminal to make the script executable:

$ chmod +x send_whatsapp.sh        


Step 3: Sending WhatsApp Messages

With the Bash script in place, sending WhatsApp messages becomes a breeze. By executing the script with the appropriate parameters, we can send messages to our contacts directly from the terminal. This automation can be particularly useful for sending reminders, notifications, or even running personal chatbots.

To send a WhatsApp message, run the script with the recipient's phone number and the message you want to send:

$ ./send_whatsapp.sh RECIPIENT_PHONE_NUMBER_WITH_COUNTRY_CODE "Hello, this is a test message sent using Bash!"        


Keep In Mind

While automating WhatsApp messages can be incredibly convenient, it's essential to use this capability responsibly. Always respect the privacy and preferences of your contacts, and avoid sending spam or unsolicited messages. Additionally, keep in mind that automating WhatsApp interactions may be subject to WhatsApp's terms of service, so use it with caution.

Conclusion

Automating WhatsApp messages using Bash has been a rewarding project that showcases the power of simple scripting in everyday tasks. By following the steps outlined in this article, you too can harness the potential of automation to enhance your messaging experience.

Remember, automation is a tool, and it is up to us to use it ethically and responsibly. I hope this article has inspired you to explore the possibilities of automation further. Happy scripting and happy messaging!


Check out my other write-ups by visiting to my profile

Thanks

- SK -

Ricardo Wolosker

Programador de sistemas

8 个月

not work for me

回复
José Monteiro

Diretor de Meio Ambiente na Quimifactor

9 个月

Well, I will test this method. Dont know if whatsapp's API will accept the data. In order to get the SID (the session token), I will use Wireshark. But you also can grab it by installing ADB (Android Debug Bridge), connecting the phone via USB, putting it under usb debug mode and typing the command "adb shell dumpsys window | grep isdn". The number after isdn is the session token

回复
Scripnix Infotech

Co-Founder & CEO at SCRIPNIX | ??♀? Digital Marketing Expert | ?? For collabs DM | ?? [email protected]

10 个月

Can you please elaborate more on how to obtain session token?

回复

How can I get WA Session Token?

回复

Step 1 seems to be a bit vague... any chance you could elaborate?

回复

要查看或添加评论,请登录

SAKSHAM TRIVEDI的更多文章

  • DNF5... It's time to rethink the package management.

    DNF5... It's time to rethink the package management.

    Hello Folks, It's been a while since I have written any article. Well here I am now.

  • File BackuP & Sync...

    File BackuP & Sync...

    Introduction In today's fast-paced world, seamless file sharing and synchronization are essential for efficient…

  • Simplifying SSH Connection: A Guide To Build Your Own Proxy Network

    Simplifying SSH Connection: A Guide To Build Your Own Proxy Network

    Establishing a secure SSH connection between Machine A and Machine F can be challenging, especially when intermediate…

  • Unleashing the Power of Linux: A Browser-Based Linux Experience

    Unleashing the Power of Linux: A Browser-Based Linux Experience

    To access Fedora Linux's graphical system in a browser using Apache Guacamole, you will need to set up Guacamole on a…

  • Gmail from Old Dog Terminal

    Gmail from Old Dog Terminal

    Mutt is used to send and receive email from your terminal, even if you're using hosted service such as Gmail. A…

  • Ping... a message arrrived

    Ping... a message arrrived

    In this short tutorial, Here I am going to demonstrate chatting using ping command. For this first of all you need a…

    1 条评论
  • Containerized SSH Server

    Containerized SSH Server

    In this article I am going to demonstrate how to containerize SSH server so without any further explanation let's…

  • Mutiple Linux shells on a same system

    Mutiple Linux shells on a same system

    Recently I came across the question is there any possibility that multiple shells can be installed on the same system…

  • Jupyter Lab as a container

    Jupyter Lab as a container

    This article is focused on containerizing Jupyter Notebook inside the docker container with a fully-fledged develop…

  • Launching graphical application inside the docker container

    Launching graphical application inside the docker container

    Hello guys, In this article I am going to discuss how to launch the graphical application inside the docker. So, for…

社区洞察

其他会员也浏览了