Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pywhatkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
open_web,
sendwhatmsg,
sendwhatmsg_instantly,
sendmultiwhatmsg_instantly,
sendwhatmsg_to_group,
sendwhatmsg_to_group_instantly,
sendwhats_image,
Expand Down
33 changes: 6 additions & 27 deletions pywhatkit/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@
from webbrowser import open

import requests
from pyautogui import (
ImageNotFoundException,
click,
hotkey,
locateOnScreen,
moveTo,
press,
size,
typewrite,
)
from pyautogui import click, hotkey, press, size, typewrite

from pywhatkit.core.exceptions import InternetException

Expand All @@ -41,18 +32,6 @@ def close_tab(wait_time: int = 2) -> None:
press("enter")


def findtextbox() -> None:
"""click on text box"""
dir_path = os.path.dirname(os.path.realpath(__file__))
location = locateOnScreen(f"{dir_path}\\data\\pywhatkit_smile1.png")
try:
moveTo(location[0] + 150, location[1] + 5)
except Exception:
location = locateOnScreen(f"{dir_path}\\data\\pywhatkit_smile.png")
moveTo(location[0] + 150, location[1] + 5)
click()


def check_connection() -> None:
"""Check the Internet connection of the Host Machine"""

Expand Down Expand Up @@ -90,7 +69,6 @@ def send_message(message: str, receiver: str, wait_time: int) -> None:
hotkey("shift", "enter")
else:
typewrite(char)
findtextbox()
press("enter")


Expand Down Expand Up @@ -134,12 +112,14 @@ def copy_image(path: str) -> None:
raise Exception(f"Unsupported System: {system().lower()}")


def send_image(path: str, caption: str, receiver: str, wait_time: int) -> None:
def send_image(path: str, caption: str, receiver: str, wait_time: int, only_image: bool) -> None:
"""Sends the Image to a Contact or a Group based on the Receiver"""

_web(message=caption, receiver=receiver)
if(only_image):
_web(message=caption, receiver=receiver)

time.sleep(7)
click(WIDTH / 2, HEIGHT / 2)
#click(WIDTH / 2, HEIGHT / 2)
time.sleep(wait_time - 7)
copy_image(path=path)
if not check_number(number=receiver):
Expand All @@ -155,5 +135,4 @@ def send_image(path: str, caption: str, receiver: str, wait_time: int) -> None:
else:
hotkey("ctrl", "v")
time.sleep(1)
findtextbox()
press("enter")
83 changes: 50 additions & 33 deletions pywhatkit/whats.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-
import time
import webbrowser as web
from datetime import datetime
from re import fullmatch
from typing import Optional
import requests
import shutil
from urllib.parse import quote

from pynput.keyboard import Controller
import pyautogui as pg

from pywhatkit.core import core, exceptions, log
Expand All @@ -13,6 +16,46 @@
core.check_connection()


def save_image(url):
response = requests.get(url, stream=True)
with open('tmp.jpg', 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response

def sendmultiwhatmsg_instantly(
phone_no: str,
messages: list,
delay: int = 3,
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!")

web.open(f"https://web.whatsapp.com/send?phone={phone_no}")
time.sleep(4)
#pg.click(core.WIDTH / 2, core.HEIGHT / 2)
time.sleep(wait_time - 4)
for message in messages:

if("https://" in message):
save_image(message)
# image saved as temp.png
core.send_image(
path="tmp.jpg", caption="", receiver=phone_no, wait_time=wait_time, only_image=False
)
log.log_image(_time=time.localtime(), path=message, receiver=phone_no, caption="")
else:
Controller().type(message)
pg.press("enter")
log.log_message(_time=time.localtime(), receiver=phone_no, message=message)
time.sleep(delay)
if tab_close:
core.close_tab(wait_time=close_time)

def sendwhatmsg_instantly(
phone_no: str,
message: str,
Expand All @@ -25,15 +68,10 @@ def sendwhatmsg_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]{10}$", phone_no):
raise exceptions.InvalidPhoneNumber("Invalid Phone Number.")

web.open(f"https://web.whatsapp.com/send?phone={phone_no}&text={quote(message)}")
time.sleep(4)
pg.click(core.WIDTH / 2, core.HEIGHT / 2)
#pg.click(core.WIDTH / 2, core.HEIGHT / 2)
time.sleep(wait_time - 4)
core.findtextbox()
pg.press("enter")
log.log_message(_time=time.localtime(), receiver=phone_no, message=message)
if tab_close:
Expand All @@ -50,13 +88,10 @@ def sendwhatmsg(
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}[0-9]{10}$", 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!")

Expand Down Expand Up @@ -132,7 +167,8 @@ def sendwhatmsg_to_group_instantly(
"""Send WhatsApp Message to a Group Instantly"""

current_time = time.localtime()
time.sleep(4)

time.sleep(wait_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:
Expand All @@ -142,8 +178,6 @@ def sendwhatmsg_to_group_instantly(
def sendwhats_image(
receiver: str,
img_path: str,
time_hour: int,
time_min: int,
caption: str = "",
wait_time: int = 15,
tab_close: bool = False,
Expand All @@ -155,25 +189,8 @@ def sendwhats_image(
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
path=img_path, caption=caption, receiver=receiver, wait_time=wait_time, only_image=True
)
log.log_image(_time=current_time, path=img_path, receiver=receiver, caption=caption)
if tab_close:
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ Pillow
pyautogui
requests
wikipedia
requests
shutil
pynput