0
import webbrowser
import time
import datetime

name = input('Enter the contact number of a person you want to send message in WhatsApp: ')
message = input('Enter the message: ')
time1 = input('Enter time in {hh:mm:ss} format: ')
print(f'Time entered by user: {time1}')
while True:
    current_time = time.ctime()
    time_format = current_time[11:19]
    time.sleep(1)
    print(f'Current time: {time_format}')
    if time1 == time_format:
        webbrowser.open_new_tab(f'https://web.whatsapp.com/send?phone=+91{name}&text={message}')
        break
    elif time1 < time_format:
        print('Enter correct time')
        break
    else:
        print('waiting..')

I am taking message, contact number and time as a input from the user. whenever, if condition satisfies, WhatsApp is open with the contact number and message you type in before.

Only problem is, I have to manually hit the send button to send message. Everything else is working fine.

Is there any way to do that? It would be great, if you provide solution without using Selenium

Thanks in advance!

Harsh Dhamecha
  • 116
  • 3
  • 12

1 Answers1

0

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:

click_send.png

and open_whatsapp.png looks like this:

open_whatsapp.png

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.

Filip
  • 898
  • 1
  • 8
  • 24
  • Good suggestion but you did not answer his question, you could have mentioned it as a comment. – MukeshRKrish Apr 21 '20 at 04:33
  • You are right, I didn't realise it because I don't use WhatsApp so didn't get the question entirely – Filip Apr 21 '20 at 04:37
  • He is trying to hit a button after opening the browser, for which he does not want to use selenium. – MukeshRKrish Apr 21 '20 at 04:41
  • Yeah I'm looking into it, just made a WhatsApp account. – Filip Apr 21 '20 at 04:46
  • Doesn't look like there are any options outside of Selenium. I didn't find any url variable that could be set to send the message automatically and a library I found also uses Selenium. Another option would probably be using the WhatsApp Business API but you have to make your account a Business Account. – Filip Apr 21 '20 at 05:17
  • Ok, if there's no option except selenium and WhatsApp API, No problem. But thanks for informing me about schedule module. – Harsh Dhamecha Apr 21 '20 at 06:31
  • @HarshDhamecha I tried to make another script that would also send the message using selenium, but WhatsApp has a QR code to connect to the Web interface, probably to stop people from trying to do automated stuff like we tried to do here :P It also changes every 15 seconds I think so whatever solution you try to come up with will have to be faster than that. – Filip Apr 21 '20 at 07:59
  • @HarshDhamecha I added a part that could solve your problem with `pyautogui` and `selenium`. This is pretty much the easiest way to do it, although not being the most reliable. – Filip Apr 21 '20 at 09:25
  • @Filip please check, is it working on WhatsApp web or i need to install WhatsApp in my laptop. btw thanking you again for this great help – Harsh Dhamecha Apr 21 '20 at 11:33
  • @HarshDhamecha It's all good :) yeah you would need to install WhatsApp on your laptop, if you have windows on it. After installing you have to scan the QR code it gives you but you will only have to scan it once and then let the script to the rest. – Filip Apr 21 '20 at 14:43