This doesn't seem really efficient and not really good to constantly check to see if the time the user gave is the same as the current one. The schedule
module can be really helpful with this and it does help to make your code look cleaner. I'll provide 2 answers, one with the schedule
library and one without it.
Remember that if the user inputs a time that has passed, that doesn't matter to the program and it will only send the message at the next time that the time the user provided is the current device time. For example if the user inputs 13:10:00 and the current device time is 14:00:00 then the message will be sent the next time the current time is 13:10:00 which is the next day.
import os
import time
import webbrowser
from datetime import datetime
from string import ascii_letters
name = input('Enter the contact number of a person you want to send message in WhatsApp: ')
message = input('Enter the message:\n')
time1 = input('Enter time in {hh:mm:ss} format: ')
# Check if there aren't any ':' in the input time
if ":" not in time1:
print("Please input a correct time format")
os._exit(0)
# Check if there are any letters in the input time
elif ascii_letters in time1:
print("Please input a correct time format")
os._exit(0)
print(f'Time entered by user: {time1}')
# Check every .9 seconds if the current time is the same as the user input time
while True:
current_time = datetime.now().strftime("%H:%M:%S")
print(f'Current time: {current_time}')
if time1 == current_time:
webbrowser.open_new_tab(f'https://web.whatsapp.com/send?phone=+91{name}&text={message}')
break
else:
time.sleep(0.9)
And with the schedule
library this is how it would probably look like:
(this is how you would probably do it)
import os
import time
import schedule
import webbrowser
from string import ascii_letters
def send_whatsapp_msg(name, message):
webbrowser.open_new_tab(f'https://web.whatsapp.com/send?phone=+91{name}&text={message}')
return schedule.CancelJob # do this if you want to send this message only once
# or just exit the program entirely if you don't want to run any more tasks
# os._exit(0)
name = input('Enter the contact number of a person you want to send message in WhatsApp: ')
message = input('Enter the message:\n') # Message to be sent
time1 = input('Enter time in {hh:mm:ss} format: ') # Time to sent the message
# Check if there aren't any ':' in the input time
if ":" not in time1:
print("Please input a correct time format")
os._exit(0)
# Check if there are any letters in the input time
elif ascii_letters in time1:
print("Please input a correct time format")
os._exit(0)
print(f'Time entered by user: {time1}')
# Schedule the message to be sent
schedule.every().day.at(time1).do(send_whatsapp_msg, name, message)
# Wait for the tasks to run and check the time every .9 seconds
while True:
schedule.run_pending()
time.sleep(0.9)
To fix the button clicking problem I think I found a "hacky" solution, using selenium and pyautogui. On Windows, you can install the WhatsApp application and scan the QR Code only once and then this solution will probably work for you.
You will also have to install the selenium chrome webdriver and take a screenshot of the "arrow" button to send the message to on the WhatsApp app. Here is the janky solution:
import os
import time
import schedule
import pyautogui
from string import ascii_letters
from selenium import webdriver
from selenium.webdriver.commn.keys import Keys
def send_whatsapp_msg(name, message):
driver = webdriver.Chrome() # you can user different drivers like 'Firefox()' but you will have to install them first
# look at https://selenium-python.readthedocs.io/installation.html for more info
driver.get("https://api.whatsapp.com/send?phone=+91{name}&text={message}")
time.sleep(10) # Wait for everything to set up, can be assigned lower values
# Click on the 'Open WhatsApp' Prompt Button
# Much easier to do it with pyautogui since I couldn't make it work with Selenium
# Look at https://pyautogui.readthedocs.io/en/latest/quickstart.html#screenshot-functions for the '.locateCenterOnScreen' function
coords = pyautogui.locateCenterOnScreen("open_whatsapp.png")
pyautogui.click(coords[0], coords[1]) # read the docs on what '.locateCenterOnScreen' returns
time.sleep(15) # Wait for the WhatsApp dektop app to load up
coords = pyautogui.locateCenterOnScreen("click_send.png") # coordinates for the 'send' button
pyautogui.click(coords[0], coords[1])
# Your message has been sent!
return schedule.CancelJob # do this if you want to send this message only once
# or just exit the program entirely if you don't want to run any more tasks
# os._exit(0)
name = input('Enter the contact number of a person you want to send message in WhatsApp: ')
message = input('Enter the message:\n') # Message to be sent
time1 = input('Enter time in {hh:mm:ss} format: ') # Time to sent the message
# Check if there aren't any ':' in the input time
if ":" not in time1:
print("Please input a correct time format")
os._exit(0)
# Check if there are any letters in the input time
elif ascii_letters in time1:
print("Please input a correct time format")
os._exit(0)
print(f'Time entered by user: {time1}')
# Schedule the message to be sent
schedule.every().day.at(time1).do(send_whatsapp_msg, name, message)
# Wait for the tasks to run and check the time every .9 seconds
while True:
schedule.run_pending()
time.sleep(0.9)
click_send.png
looks like this:

and open_whatsapp.png
looks like this:

open_whatsapp.png
is a screenshot of the "Open WhatsApp" button that the website prompts you, but it was different in my language so I had to edit it out.
Also I don't know how reliable pyautogui
will be but it worked every time I tried to run this, so I guess it kinda works.