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 -

There's a bit of trouble with this article. Step 1 seems to want to describe how to get the Web Session token, except that it doesn't. The text below the bash script then incorrectly says that the token comes from Step 2 (it "tries" to come from Step 1 but it doesn't). So the question remains, how do I get the Web Session token?

Like
Reply

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

Like
Reply

How can I get WA Session Token?

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore content categories