Python Automated WhatsApp Message Sender

Learn how to build an automated WhatsApp sender that will send randomized messages on your behalf during the day.

Image by author

Last week was very busy at work, and I barely managed during the day to arrange a time for lunch and worse than that — to talk to my wife and ask how she is. After a week where in the evenings, I had to explain why I didn’t have a few seconds to send a WhatsApp message, I came up with the idea — to build a Python script that would send a daily message on my behalf. So I set out on a mission to create one and try to make it as general, random, and reliable as possible. The last thing I need is for her to find out that I spent time building such a script instead of just sending a message…

Let’s start

We will start with importing the libraries that we’ll use. First, we will import the main library PyWhatKit, which is one of the most popular libraries for WhatsApp and YouTube automation and allows you to send WhatsApp messages quite easily in one line. In fact, the library is built so that we won’t need almost any of the other libraries, as the methods are supposed to open the WhatsApp web, send the message automatically, and close the tab. But to this date, there are some bugs, so we will need more work to work around those.

One of the bugs (at least that I encountered) is that the automatic sender is not working, so to work around it, we will use PyAutoGUI, which lets your Python scripts control the mouse and keyboard to automate interactions with other applications. In addition, we will use PynPut, and specifically the Key and Controller methods, so we can add control to our keyboard to send the messages.

Eventually, we will need the emoji library to make our messages a bit more diverse, the random library to randomly choose the messages and the time that the messages will be sent, and the time library to give the code some time to do its job.

https://medium.com/media/3d627a5a59fecae02a8bf4e1c8734366/href

In order to send the message, we will write a function as below. In general, we will try to send the message (as I mentioned, I encountered several bugs, so it’s easier to work like this), and if it won’t work, we willexcept the error and just print it, so we will know what went wrong. Our function takes four parameters: a phone number with country code and a message, both as strings, and two integers for the hour (between 0 to 23) and minutes between 0 to 59.

In the try, we first use the pywahtkit.sendwahtmsg method, which, as the name suggests, sends a message. All the function’s parameters are inputs for this one, and in order for this to work properly, the time should be at least 5–6 minutes before the desired time for sending the message. Next, we will wait for 10 seconds with the time.sleep() so our browser will have the time to open the WhatsApp tab, and this may vary in time depending on your internet.

https://medium.com/media/1a40c5c8029d3665eae2e19f5b5648e8/href

After the first time you will use the pywahtkit.sendwahtmsg method, the WhatsApp web will ask you to connect your device to the browser, as shown below. This can also affect the first run of the function, as it will take longer than 10 seconds to open the tab and connect your computer (the method also has a built-in 15 delay, but it might not be enough for this)

Image by author

Now, we need to build a “bank” of messages, which we will randomly choose a message every time with the random.choise method. The example below shows you that along with some simple text messages. In addition, I used the emoji.emojize method to spice up some of the messages or just to send an emoji alone.

https://medium.com/media/5dd7a4c00f40648525908b919f52f150/href

Hold on. We are almost there. To make it more interactive, I added an option to choose how many messages I would like to send every day and between which hours (starting hour and ending hour). Obviously, that depends on your working day length. All of this is being achieved by the input() built-in function, which asks the user to write his answer as input.

https://medium.com/media/40bd19fe73be8f4388e38c0de14876d2/href

Eventually, we will run a while loop that runs as long as it doesn’t send all the messages we asked for. It first randomly chooses an hour, then picks the minutes, and eventually calls the sender function that we previously built.

https://medium.com/media/8adced346d800d5167e4f0edcc1df695/href

And that really is it!

I’ll stop here, but you can find the full code here. And if you have any thoughts or questions, feel free to leave a comment 🙂

Thanks for reading! 👍


Python Automated WhatsApp Message Sender was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Asaf Pras

Learn how to build an automated WhatsApp sender that will send randomized messages on your behalf during the day.

Image by author

Last week was very busy at work, and I barely managed during the day to arrange a time for lunch and worse than that — to talk to my wife and ask how she is. After a week where in the evenings, I had to explain why I didn't have a few seconds to send a WhatsApp message, I came up with the idea — to build a Python script that would send a daily message on my behalf. So I set out on a mission to create one and try to make it as general, random, and reliable as possible. The last thing I need is for her to find out that I spent time building such a script instead of just sending a message…

Let's start

We will start with importing the libraries that we'll use. First, we will import the main library PyWhatKit, which is one of the most popular libraries for WhatsApp and YouTube automation and allows you to send WhatsApp messages quite easily in one line. In fact, the library is built so that we won't need almost any of the other libraries, as the methods are supposed to open the WhatsApp web, send the message automatically, and close the tab. But to this date, there are some bugs, so we will need more work to work around those.

One of the bugs (at least that I encountered) is that the automatic sender is not working, so to work around it, we will use PyAutoGUI, which lets your Python scripts control the mouse and keyboard to automate interactions with other applications. In addition, we will use PynPut, and specifically the Key and Controller methods, so we can add control to our keyboard to send the messages.

Eventually, we will need the emoji library to make our messages a bit more diverse, the random library to randomly choose the messages and the time that the messages will be sent, and the time library to give the code some time to do its job.

In order to send the message, we will write a function as below. In general, we will try to send the message (as I mentioned, I encountered several bugs, so it's easier to work like this), and if it won't work, we willexcept the error and just print it, so we will know what went wrong. Our function takes four parameters: a phone number with country code and a message, both as strings, and two integers for the hour (between 0 to 23) and minutes between 0 to 59.

In the try, we first use the pywahtkit.sendwahtmsg method, which, as the name suggests, sends a message. All the function's parameters are inputs for this one, and in order for this to work properly, the time should be at least 5–6 minutes before the desired time for sending the message. Next, we will wait for 10 seconds with the time.sleep() so our browser will have the time to open the WhatsApp tab, and this may vary in time depending on your internet.

After the first time you will use the pywahtkit.sendwahtmsg method, the WhatsApp web will ask you to connect your device to the browser, as shown below. This can also affect the first run of the function, as it will take longer than 10 seconds to open the tab and connect your computer (the method also has a built-in 15 delay, but it might not be enough for this)

Image by author

Now, we need to build a "bank" of messages, which we will randomly choose a message every time with the random.choise method. The example below shows you that along with some simple text messages. In addition, I used the emoji.emojize method to spice up some of the messages or just to send an emoji alone.

Hold on. We are almost there. To make it more interactive, I added an option to choose how many messages I would like to send every day and between which hours (starting hour and ending hour). Obviously, that depends on your working day length. All of this is being achieved by the input() built-in function, which asks the user to write his answer as input.

Eventually, we will run a while loop that runs as long as it doesn't send all the messages we asked for. It first randomly chooses an hour, then picks the minutes, and eventually calls the sender function that we previously built.

And that really is it!

I’ll stop here, but you can find the full code here. And if you have any thoughts or questions, feel free to leave a comment 🙂

Thanks for reading! 👍


Python Automated WhatsApp Message Sender was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Asaf Pras


Print Share Comment Cite Upload Translate Updates
APA

Asaf Pras | Sciencx (2022-12-02T17:50:43+00:00) Python Automated WhatsApp Message Sender. Retrieved from https://www.scien.cx/2022/12/02/python-automated-whatsapp-message-sender/

MLA
" » Python Automated WhatsApp Message Sender." Asaf Pras | Sciencx - Friday December 2, 2022, https://www.scien.cx/2022/12/02/python-automated-whatsapp-message-sender/
HARVARD
Asaf Pras | Sciencx Friday December 2, 2022 » Python Automated WhatsApp Message Sender., viewed ,<https://www.scien.cx/2022/12/02/python-automated-whatsapp-message-sender/>
VANCOUVER
Asaf Pras | Sciencx - » Python Automated WhatsApp Message Sender. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/02/python-automated-whatsapp-message-sender/
CHICAGO
" » Python Automated WhatsApp Message Sender." Asaf Pras | Sciencx - Accessed . https://www.scien.cx/2022/12/02/python-automated-whatsapp-message-sender/
IEEE
" » Python Automated WhatsApp Message Sender." Asaf Pras | Sciencx [Online]. Available: https://www.scien.cx/2022/12/02/python-automated-whatsapp-message-sender/. [Accessed: ]
rf:citation
» Python Automated WhatsApp Message Sender | Asaf Pras | Sciencx | https://www.scien.cx/2022/12/02/python-automated-whatsapp-message-sender/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.