Sending a Message on WhatsApp Using Python

Sending a Message on WhatsApp Using Python

In today’s fast-paced world, automation is becoming increasingly essential, and when it comes to sending messages on platforms like WhatsApp, automation can save a lot of time and effort. Whether you want to schedule a message, send reminders, or even send bulk messages to multiple contacts, automating WhatsApp messaging with Python is a great way to streamline the process.

In this blog post, we will explore how to send WhatsApp messages programmatically using Python. The easiest way to achieve this is by using a library called pywhatkit, which offers a simple interface to interact with WhatsApp Web.

Let’s dive in!


Prerequisites

Before we jump into the code, you’ll need a few things to get started:

  1. Python: Ensure you have Python installed. You can download it from python.org.
  2. pywhatkit library: This is a Python library that simplifies the task of sending WhatsApp messages through WhatsApp Web. We will install it via pip.
  3. WhatsApp Web Access: You will need to scan the QR code with your WhatsApp mobile app to log into WhatsApp Web when you run the code.


Step 1: Install pywhatkit

First, you need to install the pywhatkit library. Open your terminal or command prompt and run the following command:

pip install pywhatkit        

This will download and install the pywhatkit library to your system.


Step 2: Writing the Code to Send a Message

Once you have installed pywhatkit, you can now write a Python script to send a WhatsApp message.

Here is a complete python code that sends a message via WhatsApp:

A complete code to send a message on Whatsapp using python

Explanation of the Code

  • phone_number : This is the recipient's phone number in international format (including the country code). Use +91 for Indian number
  • message: The content of the message you want to send.
  • hour and minute: These define the time at which the message should be sent. The time is specified in 24-hour format. For example, if you want the message to be sent at 3:30 PM, you would set hour = 15 and minute = 30.
  • kit.sendwhatmsg(): This is the function that sends the message. When called, it opens WhatsApp Web, waits until the specified time, and sends the message automatically.


When we run this code python will automatically open the Whatsapp web at the given time and send the given message to the given phone number

How It Works:

  1. When you run the program, a new browser window will open up, navigating to WhatsApp Web.
  2. If you are not already logged in, you’ll be prompted to scan a QR code using the WhatsApp app on your phone (just like when you use WhatsApp Web manually).
  3. After logging in, program waits for the specified time to send the message.
  4. At the given time, the message will be sent automatically to the specified contact.


Things to Keep in Mind:

  • WhatsApp Web Session: The script relies on WhatsApp Web, so you must be logged into WhatsApp Web in your browser. The first time you run the script, you’ll need to scan the QR code manually. After that, you should stay logged in for future use.
  • Timing Precision: There may be a slight delay (a few seconds) when opening the browser and loading WhatsApp Web. Make sure you set the time carefully.
  • Sending Time: The sendwhatmsg() function doesn't send the message immediately. It waits until the specified hour and minute, so you need to plan accordingly. If you specify a time in the past, it will send the message in the next available time slot.


Advanced Use Cases

While the simple code above will work in most cases, you can use pywhatkit for more advanced use cases as well, such as:

  • Sending images: You can send images via WhatsApp using pywhatkit.sendwhats_image().
  • Sending messages to groups: If you want to send messages to multiple contacts or a group, you can modify the script accordingly.
  • Scheduling messages: Automate your messages by scheduling them at specific times using cron jobs (Linux/macOS) or Task Scheduler (Windows).


Conclusion

Automating WhatsApp messages with Python can be incredibly useful for tasks like reminders, alerts, or even sending bulk messages. The pywhatkit library makes this process simple and efficient, allowing you to send messages at scheduled times with just a few lines of code.

In this blog post, we've learned how to send a WhatsApp message using Python and the pywhatkit library. Now you can automate your WhatsApp messaging with ease, saving you time and effort.


Thank you for reading ... :>

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

Himanshu Singh的更多文章

社区洞察

其他会员也浏览了