Automate Sending Scheduled Whatsapp Message in Python

Automate Sending Scheduled Whatsapp Message in Python

Task Automation is an easy task on python. Probably the most common task we do every day is sending messages on WhatsApp. in this tutorial we want to Automate Sending whatsapp Message in Python. At first we need pywhatkit library. The pywhatkit library allows you to send individual Whatsapp messages, send messages to groups, and even send images – all from Python!. To install the last version of pywhatkit, open up a terminal and run the following command.

pip install pywhatkit

The sendwhatmsg_instantly() function will send a Whatsapp message after running the code, hence the “instantly” in the name. Two parameters are required:

  • phone_no – A phone number to which you want to send the message. Don’t forget to include the country code.
  • message – The actual message you want to send.

Let’s see it in action:

import pywhatkit

# syntax: phone number with country code, message
pywhatkit.sendwhatmsg_instantly(
    phone_no='<phone_number_with_country_code>', 
    message="Howdy! This message is Send Automatically by Python. CodeTipsAcademy.com!",
)

after running code, you’ll land on a new chat screen with your message populated in the input area. For some reason, the message isn’t sent automatically, and you have to manually click on the send button. So, this is not good and we will fix it in future.

Send Schedule Whatsapp Messages

if you want to send Scheduled Whatsapp Message you should use sendwhatmsg() function. sendwhatmsg() has following input parameters.

  • phone_no – A phone number to which you want to send the message. Don’t forget to include the country code.
  • message – The actual message you want to send.
  • time_hour – Integer, represents the hour (24h format) in which you want to send the message.
  • time_min – Integer, represents the minute in which you want to send the message.

See an example of using sendwhatmsg() function. in code below we send a whatsapp message at 19:30.

import pywhatkit

# syntax: phone number with country code, message, hour and minutes
pywhatkit.sendwhatmsg('<phone_number_with_country_code>', 
"Howdy! This message is Send Automatically by Python. CodeTipsAcademy.com!",
 19, 30)

As before, the message doesn’t get sent automatically, and you have to manually click on the Send button. this issue will be solved in future of this article, keep reading.

Send Whatsapp Image

To send Images and GIF’s to WhatsApp users sendwhats_image function must be used. For Linux based distributions you need copyq installed on your system. Windows users can send Images (all formats) and GIF’s. For Linux based distributions, only JPEG and PNG are supported. For MacOS users, only JPEG is supported currently.

import pywhatkit

# syntax: phone number with country code, Image, caption
pywhatkit.sendwhats_image('<phone_number_with_country_code>', 
"C:\\Image.png", "Here is the image", 10, True, 5)

As before, the message doesn’t get sent automatically, and you have to manually click on the Send button. this issue will be solved in next section, keep reading.

Solve Issue of needing to press send button

We need 2 additional Python libraries to automatically trigger the Send button. These are pyautogui and pynput. so, cammand below download these packages.

pip install pyautogui pynput

The send_whatsapp_message() function does the following:

  1. Opens Whatsapp Web and populates the input field with the specified message.
  2. Sleeps for 20 seconds to ensure everything has loaded properly.
  3. Clicks on the screen to ensure the correct window/tab is selected.
  4. Presses and releases the Enter key on the keyboard to send the message.

If any of the steps fail, the exception is printed to the console.

Here’s the entire code snippet for the function with a usage example:

import time 
import pywhatkit
import pyautogui
from pynput.keyboard import Key, Controller

keyboard = Controller()

def send_whatsapp_message(msg: str):
    try:
        pywhatkit.sendwhatmsg_instantly(
            phone_no='<phone_number_with_country_code>', 
            message=msg,
            tab_close=True
        )
        time.sleep(20)
        pyautogui.click()
        time.sleep(2)
        keyboard.press(Key.enter)
        keyboard.release(Key.enter)
        print("Message sent!")
    except Exception as e:
        print(str(e))


if __name__ == "__main__":
    send_whatsapp_message(msg="Test message from a Python script!")