method = eval('cv.TM_CCOEFF_NORMED')
url = 'https://web.whatsapp.com/'
screenshot_name = 'images/whats-img.png'
def get_image_coordinates(image_source="", image_target=""):
if image_source != "" and image_target != '':
img = cv.imread(image_source, cv.IMREAD_GRAYSCALE)
img = (255 - img)
template = cv.imread(image_target, cv.IMREAD_GRAYSCALE)
template = (255 - template)
w, h = template.shape[::-1]
res = cv.matchTemplate(img, template, method)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
return [top_left, bottom_right]
def get_click_coordinate(top_left, bottom_right, x_percentage, y_percentage):
click_x_coordinate = int(((bottom_right[0] - top_left[0]) * x_percentage) + top_left[0])
click_y_coordinate = int(((bottom_right[1] - top_left[1]) * y_percentage) + top_left[1])
return click_x_coordinate, click_y_coordinate
def send_message(contact_name_param, contact_message_param=''):
# Step 2. Identify Search bar
print("Taking screenshot...")
screenshot = pyautogui.screenshot()
screenshot.save(screenshot_name)
time.sleep(1)
print("Searching search bar...")
search_bar_top_left, search_bar_bottom_right = get_image_coordinates(screenshot_name, 'images/search-bar.png')
click_x, click_y = get_click_coordinate(search_bar_top_left, search_bar_bottom_right, 0.5, 0.5)
pyautogui.click(click_x, click_y)
time.sleep(1)
print("Searching contact...")
pyautogui.typewrite(contact_name_param)
time.sleep(1)
# Step 3. Select first contact
print("Taking screenshot...")
screenshot = pyautogui.screenshot()
screenshot.save(screenshot_name)
time.sleep(1)
print("Searching chats bar...")
chats_bar_top_left, chats_bar_bottom_right = get_image_coordinates(screenshot_name, 'images/chats-bar.png')
click_x, click_y = get_click_coordinate(chats_bar_top_left, chats_bar_bottom_right, 0.5, 1.08)
pyautogui.click(click_x, click_y)
time.sleep(1)
pyautogui.keyDown('shift')
pyautogui.press('home')
pyautogui.keyUp('shift')
time.sleep(1)
print("Writing message...")
pyautogui.typewrite(contact_message_param)
time.sleep(1)
# Step 3. Click on send button
print("Taking screenshot...")
screenshot = pyautogui.screenshot()
screenshot.save(screenshot_name)
time.sleep(1)
print("Click on send button...")
send_button_top_left, send_button_bottom_right = get_image_coordinates(screenshot_name, 'images/send-button.png')
click_x, click_y = get_click_coordinate(send_button_top_left, send_button_bottom_right, 0.5, 0.5)
pyautogui.click(click_x, click_y)