Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature link preview #109

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ Here how to send a message to multiple users, Let's say we want to wish merry-x

*You have to include the **country code** in your number for this library to work but don't include the (+) symbol*

### Sending Links (Link Preview)

Sending messages with a link and link preview can be accomplished by setting the `wait_for_link_preview` flag in the **send_message** or **send_direct_message** function to `True`

```python
>>> from alright import WhatsApp
>>> messenger = WhatsApp()
>>> messenger.send_direct_message('25573652xxx', 'https://github.com/Kalebu/alright', True, True)
```

### Sending Images

Sending Images is nothing new, its just the fact you have to include a path to your image and the message to accompany the image instead of just the raw string characters and also you have use *send_picture()*, Here an example;
Expand Down
30 changes: 27 additions & 3 deletions alright/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,13 @@ def send_message1(self, mobile: str, message: str) -> str:
LOGGER.info(f"{msg}")
return msg

def send_message(self, message):
def send_message(self, message, wait_for_link_preview=False):
"""send_message ()
Sends a message to a target user

Args:
message ([type]): [description]
wait_for_link_preview (bool): Wait until the link preview is shown. Defaults to False.
"""
try:
inp_xpath = (
Expand All @@ -474,18 +475,41 @@ def send_message(self, message):
ActionChains(self.browser).key_down(Keys.SHIFT).key_down(
Keys.ENTER
).key_up(Keys.ENTER).key_up(Keys.SHIFT).perform()
if wait_for_link_preview:
self.wait_for_link_preview()
input_box.send_keys(Keys.ENTER)
LOGGER.info(f"Message sent successfuly to {self.mobile}")
except (NoSuchElementException, Exception) as bug:
LOGGER.exception(f"Failed to send a message to {self.mobile} - {bug}")
LOGGER.info("send_message() finished running!")

def send_direct_message(self, mobile: str, message: str, saved: bool = True):
def send_direct_message(self, mobile: str, message: str, saved: bool = True, wait_for_link_preview: bool = False):
if saved:
self.find_by_username(mobile)
else:
self.find_user(mobile)
self.send_message(message)
self.send_message(message, wait_for_link_preview)

def wait_for_link_preview(self, timeout = 30):
"""Wait until the link preview is created.

Args:
timeout (int, optional): Max time to wait in seconds. Defaults to 30.
"""
link_preview_xpath = (
'//*[@id="main"]/div[3]'
)
link_preview_element = self.wait.until(
EC.presence_of_element_located((By.XPATH, link_preview_xpath))
)
def extract_height(element):
return int(element.get_attribute("style").split("height:")[1].split("px")[0])
time_counter_s = 0
thumbnail_height = extract_height(link_preview_element)
while ( thumbnail_height == 0 and time_counter_s < timeout):
time.sleep(1)
thumbnail_height = extract_height(link_preview_element)
time_counter_s +=1

def find_attachment(self):
clipButton = self.wait.until(
Expand Down