No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Automate WhatsApp with Python pywhatkit

by Khaleel O.
January 17, 2022
in Python
Reading Time: 4 mins read
A A
Python pywhatkit
Python pywhatkit

Hi! Let’s automate WhatsApp with the pywhatkit library. We will be using Python 3.8.10. Let’s go! 🔥⚡

The pywhatkit library is a brilliant little tool that allows us to automate a number of WhatsApp functions such as:

  • Sending messages
  • Sending images/files
  • Sending files/messages to a group
  • Play a Youtube Video

The first step is to get WhatsApp activated and working on your PC or Workstation where your Python script will be executed. You will need a WhatsApp account for this, obviously, and you will need to visit the WhatsApp Web website HERE. Follow the instructions on that page to get WhatsApp working on your computer. We suggest you use the Chrome browser for this example. Get Chrome HERE.

Once you get WhatsApp up and working on your computer, you need to install a few Python Libraries with the following commands:

pip install Flask
pip install pywhatkit

Run those two commands in the order presented: Flask first, then pywhatkit second. Read the output and ensure that both are installed successfully. When that is done, you can write the following code:

import pywhatkit as py

py.sendwhatmsg("+1*******************", "Code Everyday!", 16, 15)

py.sendwhats_image("+1*******************", "HappyBirthday.png")

py.playonyt("Python")

Let’s explain what is happening here:

  1. First we include the pywhatkit library.
  2. The method py.sendwhatmsg() accepts 6 parameters but here we are only giving 4:
    • First parameter, phone_num is required and is the phone number that the message will be sent to. The recipient must also have WhatsApp. Be sure to include the full number with the country code in the format: “+1xxxxxxxxxxx”
    • Second parameter, message is required and is the text of the message to be sent.
    • Third, time_hour which is the hour of the day to schedule the message for. This MUST be in 24HR clock format.
    • Fourth, time_min which is the minute of the day to schedule the message for.
    • The phone number used in this example is a dummy value. Make sure you change the code to include a real and valid WhatsApp recipient phone number BEFORE you run the code.
  3. The method py.sendwhats_image() will send an image to the phone_num specified in the first parameter. The second parameter is where you specify the name and path of the image. In this case, our image was in the same path as the Python script.
  4. Finally, the method py.playonyt() will take the topic supplied as the parameter and play the first video in search results for that keyword.

When the code executes you will see the following output:

#output
#In 37 Seconds WhatsApp will open and after 15 Seconds Message will be Delivered!

After the time shown in that message elapses, three things will happen:

  • If you used Chrome and are signed into WhatApp Web, the first statement will execute and a new tab will automatically open at the exact time specified in the py.sendwhatmsg() method. The message will appear in the send text box and after a brief pause it will be sent to the WhatsApp recipient specified.
  • After a little while, the second statement will execute and yet another tab will open automatically and the image specified will be sent to the specified WhatsApp recipient.
  • Finally, when the third statement executes yet another tab will open and it will automatically play the first Youtube video returned in the search results for the topic specified.
Python pywhatkit
Message Sent Automatically!

Great right? This is a simple script so a few things may go wrong such as the message failing to send, or WhatsApp Web freezing. If any of this happens, make sure that you specify the correct phone number to send the message to in the code and make sure it includes the country code. Also make sure the time you set for the message to be sent isn’t too far in the future.

Second, ensure that WhatApp Web is up and working when the script runs. I have found that the code works better when the app is open and working on your phone. Ensure that your internet connection is working too. If all else fails, reinstall the packages.

We hope this helped! Automation is the FUTURE and it saves time 😊. Find out more about pywhatkit HERE. Good luck and be good! 👌👌👌

Tags: automationwhatsapp
Previous Post

Python RSA Encrypt Decrypt Example

Next Post

Python RSA Generate Certificate

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy