#Vidit import subprocess import win32gui import os import time import webbrowser as web from datetime import datetime from re import fullmatch from typing import List from urllib.parse import quote import paperclip import pyautogui as pg import pyperclip import keyboard from pywhatkit.core import core, exceptions, log from typing import Union pg.FAILSAFE = False core.check_connection() def get_default_browser(): command = 'powershell -Command "$defaultBrowser = (Get-ItemProperty \'HKCU:\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice\').ProgId; echo $defaultBrowser"' result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=True) if result.returncode != 0: print("Error executing PowerShell command:") print(result.stderr.strip()) return None output = result.stdout.strip() return output def set_frontmost_process(browser_name): if browser_name.lower() == 'microsoft edge': # For Microsoft Edge, let's try a different approach to bring it to the foreground subprocess.run('start microsoft-edge:', shell=True) return # Find the window handle of the application handle = win32gui.FindWindow(None, browser_name) if handle == 0: print(f"Error: {browser_name} window not found.") return # Bring the window to the foreground win32gui.SetForegroundWindow(handle) # Test the function default_browser = get_default_browser() if default_browser: browser_mapping = { 'FirefoxURL': 'Firefox', 'ChromeHTML': 'Google Chrome', 'MSEdgeHTM': 'Microsoft Edge' } browser_name = browser_mapping.get(default_browser, 'Unknown') print(f"The default browser is: {browser_name}") set_frontmost_process(browser_name) def sendwhatmsg_instantly( phone_no: str, message: str, wait_time: int = 15, tab_close: bool = False, close_time: int = 3, ) -> None: """Send WhatsApp Message Instantly""" if not core.check_number(number=phone_no): raise exceptions.CountryCodeException("Country Code Missing in Phone Number!") phone_no = phone_no.replace(" ", "") if not fullmatch(r"^\+?[0-9]{2,4}\s?[0-9]{9,15}", phone_no): raise exceptions.InvalidPhoneNumber("Invalid Phone Number.") web.open(f"https://web.whatsapp.com/send?phone={phone_no}") time.sleep(wait_time) index = 0 length = len(message) while index < length: letter = message[index] pg.write(letter) if letter == ":": index += 1 while index < length: letter = message[index] if letter == ":": pg.press("enter") break pg.write(letter) index += 1 index += 1 default_browser= get_default_browser() time.sleep(5) if default_browser != "Unknown": # Set the frontmost process to the default browser set_frontmost_process(default_browser) else: print("set your default browser to chrome or firefox or microsoft egde to use this feature.") get_default_browser() pg.press('enter') log.log_message(_time=time.localtime(), receiver=phone_no, message=message) if tab_close: core.close_tab(wait_time=close_time) def sendimg_or_video_immediately( phone_no: str, path: str, wait_time: int = 15, tab_close: bool = False, close_time: int = 3, ) -> None: """Send WhatsApp Message Instantly""" if not core.check_number(number=phone_no): raise exceptions.CountryCodeException("Country Code Missing in Phone Number!") phone_no = phone_no.replace(" ", "") if not fullmatch(r"^\+?[0-9]{2,4}\s?[0-9]{9,15}", phone_no): raise exceptions.InvalidPhoneNumber("Invalid Phone Number.") web.open(f"https://web.whatsapp.com/send?phone={phone_no}") time.sleep(wait_time) core.find_link() time.sleep(1) core.find_photo_or_video() pyperclip.copy(os.path.abspath(path)) print("Copied") time.sleep(1) keyboard.press("ctrl") keyboard.press("v") keyboard.release("v") keyboard.release("ctrl") time.sleep(1) keyboard.press("enter") keyboard.release("enter") time.sleep(1) keyboard.press("enter") keyboard.release("enter") if tab_close: core.close_tab(wait_time=close_time) def sendwhatdoc_immediately( phone_no: str, path: str, wait_time: int = 15, tab_close: bool = True, close_time: int = 3, ) -> None: """Send WhatsApp Message Instantly""" if not core.check_number(number=phone_no): raise exceptions.CountryCodeException("Country Code Missing in Phone Number!") phone_no = phone_no.replace(" ", "") if not fullmatch(r"^\+?[0-9]{2,4}\s?[0-9]{9,15}", phone_no): raise exceptions.InvalidPhoneNumber("Invalid Phone Number.") web.open(f"https://web.whatsapp.com/send?phone={phone_no}") time.sleep(wait_time) core.find_link() time.sleep(1) core.find_document() pyperclip.copy(os.path.abspath(path)) print("Copied") time.sleep(1) keyboard.press("ctrl") keyboard.press("v") keyboard.release("v") keyboard.release("ctrl") time.sleep(1) keyboard.press("enter") keyboard.release("enter") time.sleep(1) keyboard.press("enter") keyboard.release("enter") if tab_close: core.close_tab(wait_time=close_time) def sendwhatmsg( phone_no: str, message: Union[list, str], time_hour: int, time_min: int, wait_time: int = 15, tab_close: bool = False, close_time: int = 3, ) -> None: """Send a WhatsApp Message at a Certain Time""" if not core.check_number(number=phone_no): raise exceptions.CountryCodeException("Country Code Missing in Phone Number!") phone_no = phone_no.replace(" ", "") if not fullmatch(r'^\+?[0-9]{2,4}\s?[0-9]{9,15}', phone_no): raise exceptions.InvalidPhoneNumber("Invalid Phone Number.") if time_hour not in range(25) or time_min not in range(60): raise Warning("Invalid Time Format!") current_time = time.localtime() left_time = datetime.strptime( f"{time_hour}:{time_min}:0", "%H:%M:%S" ) - datetime.strptime( f"{current_time.tm_hour}:{current_time.tm_min}:{current_time.tm_sec}", "%H:%M:%S", ) if left_time.seconds < wait_time: raise exceptions.CallTimeException( "Call Time must be Greater than Wait Time as WhatsApp Web takes some Time to Load!" ) sleep_time = left_time.seconds - wait_time print( f"In {sleep_time} Seconds WhatsApp will open and after {wait_time} Seconds Message will be Delivered!" ) time.sleep(sleep_time) if isinstance(message, list): core.send_message_list(message=message, receiver=phone_no, wait_time=wait_time) else: core.send_message(message=message, receiver=phone_no, wait_time=wait_time) log.log_message(_time=current_time, receiver=phone_no, message=message) if tab_close: core.close_tab(wait_time=close_time) def sendwhatmsg_to_group( group_id: str, message: str, time_hour: int, time_min: int, wait_time: int = 15, tab_close: bool = False, close_time: int = 3, ) -> None: """Send WhatsApp Message to a Group at a Certain Time""" if time_hour not in range(25) or time_min not in range(60): raise Warning("Invalid Time Format!") current_time = time.localtime() left_time = datetime.strptime( f"{time_hour}:{time_min}:0", "%H:%M:%S" ) - datetime.strptime( f"{current_time.tm_hour}:{current_time.tm_min}:{current_time.tm_sec}", "%H:%M:%S", ) if left_time.seconds < wait_time: raise exceptions.CallTimeException( "Call Time must be Greater than Wait Time as WhatsApp Web takes some Time to Load!" ) sleep_time = left_time.seconds - wait_time print( f"In {sleep_time} Seconds WhatsApp will open and after {wait_time} Seconds Message will be Delivered!" ) time.sleep(sleep_time) core.send_message(message=message, receiver=group_id, wait_time=wait_time) log.log_message(_time=current_time, receiver=group_id, message=message) if tab_close: core.close_tab(wait_time=close_time) def sendwhatmsg_to_group_instantly( group_id: str, message: str, wait_time: int = 15, tab_close: bool = False, close_time: int = 3, ) -> None: """Send WhatsApp Message to a Group Instantly""" current_time = time.localtime() time.sleep(4) core.send_message(message=message, receiver=group_id, wait_time=wait_time) log.log_message(_time=current_time, receiver=group_id, message=message) if tab_close: core.close_tab(wait_time=close_time) def sendwhatsmsg_to_all( phone_nos: List[str], message: str, time_hour: int, time_min: int, wait_time: int = 15, tab_close: bool = False, close_time: int = 3, ): for phone_no in phone_nos: sendwhatmsg( phone_no, message, time_hour, time_min, wait_time, tab_close, close_time ) def sendwhats_image( receiver: str, img_path: str, time_hour: int, time_min: int, caption: str = "", wait_time: int = 15, tab_close: bool = False, close_time: int = 3, ) -> None: """Send Image to a WhatsApp Contact or Group at a Certain Time""" if (not receiver.isalnum()) and (not core.check_number(number=receiver)): raise exceptions.CountryCodeException("Country Code Missing in Phone Number!") current_time = time.localtime() left_time = datetime.strptime( f"{time_hour}:{time_min}:0", "%H:%M:%S" ) - datetime.strptime( f"{current_time.tm_hour}:{current_time.tm_min}:{current_time.tm_sec}", "%H:%M:%S", ) if left_time.seconds < wait_time: raise exceptions.CallTimeException( "Call Time must be Greater than Wait Time as WhatsApp Web takes some Time to Load!" ) sleep_time = left_time.seconds - wait_time print( f"In {sleep_time} Seconds WhatsApp will open and after {wait_time} Seconds Image will be Delivered!" ) time.sleep(sleep_time) core.send_image( path=img_path, caption=caption, receiver=receiver, wait_time=wait_time ) log.log_image(_time=current_time, path=img_path, receiver=receiver, caption=caption) if tab_close: core.close_tab(wait_time=close_time) def open_web() -> bool: """Opens WhatsApp Web""" try: web.open("https://web.whatsapp.com") except web.Error: return False else: return True