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
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 `link_preview_timeout` to a value above zero in the **send_message** or **send_direct_message** function. The `link_preview_timeout` is the maximum time in seconds to wait until a link preview is generated. The time needed to create the preview depends on the specific link and the used system.

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

### 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, link_preview_timeout:int=0):
"""send_message ()
Sends a message to a target user

Args:
message ([type]): [description]
link_preview_timeout (int, optional): Wait until the link preview is shown. Defaults to 0.
"""
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 link_preview_timeout > 0:
self.wait_for_link_preview(link_preview_timeout)
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, link_preview_timeout: int = 0):
if saved:
self.find_by_username(mobile)
else:
self.find_user(mobile)
self.send_message(message)
self.send_message(message, link_preview_timeout)

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

Args:
timeout (int): Max time to wait in seconds.
"""
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