Skip to content

Instantly share code, notes, and snippets.

@yudanta
Created January 11, 2021 16:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yudanta/0af837f273aabb7b44d1358d10fe6c7c to your computer and use it in GitHub Desktop.
Save yudanta/0af837f273aabb7b44d1358d10fe6c7c to your computer and use it in GitHub Desktop.
Simple script to automate sending text to multiple recipients in WA (blast)
#!/usr/bin/env python
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
# txt to be sent
msg_template = """Hello, {name}!"""
# print(msg)
dest = [
{'phone': '62811200000', 'name': 'Luke'},
{'phone': '628564300000', 'name': 'Skywalker'}
]
# first ask selenium to open browser, in this case use firefox (geckodriver)
driver = webdriver.Firefox(executable_path='/<absolute_path_to_gecko_driver>/geckodriver')
# open whatsapp web, then scan the QR with you mobile whatsapp
driver.get("https://web.whatsapp.com/")
# after authenticate then press any key to proceed data on destination
input("Enter any keyword after scanning QR code.\n ")
for item in dest:
# https://web.whatsapp.com/send?phone={phone} << this url will redirect to new chat if number is registered for WA
driver.get('https://web.whatsapp.com/send?phone={phone}'.format(phone=item['phone']))
# wait for successfully loading page
time.sleep(10)
# format msg
msg = msg_template.format(name=item['name'])
try:
# get input box
text_box = driver.find_element_by_xpath('//*[@id="main"]//footer//div[contains(@contenteditable, "true")]')
text_box.click()
for line in msg.split('\n'):
text_box.send_keys(line)
# this action chain below is to support new line message by using shift + enter
ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.ENTER).key_up(Keys.SHIFT).perform()
# perform enter key
ActionChains(driver).send_keys(Keys.ENTER).perform()
print('done send {}'.format(wa_number))
except Exception as e:
print('something went wrong...')
print(e)
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment