Skip to content

Commit

Permalink
feat+docs: Updated notifications logic with a seperate e-mail within …
Browse files Browse the repository at this point in the history
…the config

- Updated logging
- Added missing WBS numbers in the input message
- Updated README to reflect said changes
  • Loading branch information
Vel-San committed Feb 19, 2024
1 parent 22b0b81 commit 39e253d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Alternatively, you can manually create the `configs/wbm_config.json` file with t
"XXX@protonmail.com",
"YYY@protonmail.com"
],
"notifications_email": "ZZZ@outlook.com",
"street": "Doner-Str. 123",
"zip_code": "12345",
"city": "Berlin",
Expand All @@ -85,7 +86,7 @@ Alternatively, you can manually create the `configs/wbm_config.json` file with t

## Notifications (E-mails)

The bot will be able to send you e-mail notifications (from YOURSELF) once it applies for a flat.
The bot will be able to send you e-mail notifications once it applies for a flat from the e-mail you input in `notifications_email` within the config.

To do so, you need to export `EMAIL_PASSWORD` to your environment variables. If this variable is not found, no e-mails will be sent.

Expand Down
1 change: 1 addition & 0 deletions wbmbot_v2/handlers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self, json_config):
self.zip_code = self.config.get("zip_code", "")
self.city = self.config.get("city", "")
self.emails = self.config.get("emails", [])
self.notifications_email = self.config.get("notifications_email", "")
self.phone = self.config.get("phone", "")
self.wbs = "yes" in self.config.get("wbs", "").lower()
self.wbs_date = self.config.get("wbs_date", "").replace("/", "")
Expand Down
22 changes: 15 additions & 7 deletions wbmbot_v2/helpers/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


def send_email_notification(
email: str, subject: str, body: str, attachment: str = None
send_to: str, send_from: str, subject: str, body: str, attachment: str = None
):
"""Send email notification with optional attachment.
Expand All @@ -24,15 +24,23 @@ def send_email_notification(
if not constants.email_password:
LOG.warning(
color_me.yellow(
f"E-mail password not found in the ENV variables! I will not be able to send you e-mails."
f"E-mail password not found in the ENV variables. I will not be able to send you e-mails!"
)
)
return

if "@outlook.com" not in send_from:
LOG.warning(
color_me.yellow(
f"Notifications e-mail doesn't seem be of '@outlook.com' domain, skipping notifications!"
)
)
return

try:
# Initialize yagmail SMTP connection
yag = yagmail.SMTP(
email,
send_from,
constants.email_password,
smtp_starttls=True,
smtp_ssl=False,
Expand All @@ -43,12 +51,12 @@ def send_email_notification(

# Send email with attachment if provided
if attachment:
yag.send(to=email, subject=subject, contents=body, attachments=attachment)
yag.send(to=send_to, subject=subject, contents=body, attachments=attachment)
else:
yag.send(to=email, subject=subject, contents=body)
yag.send(to=send_to, subject=subject, contents=body)

LOG.info(color_me.green(f"Email notification sent successfully to '{email}'"))
LOG.info(color_me.green(f"Email notification sent successfully to '{send_to}'"))
except Exception as e:
LOG.error(
color_me.red(f"Failed to send email notification to '{email}' | {str(e)}")
color_me.red(f"Failed to send email notification to '{send_to}' | {str(e)}")
)
7 changes: 4 additions & 3 deletions wbmbot_v2/helpers/webDriverOperations.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def accept_cookies(web_driver):

# Click the 'Accept Cookies' button
web_driver.find_element(By.XPATH, accept_button_xpath).click()
LOG.info(color_me.green("Cookies have been accepted."))
LOG.info(color_me.green("Cookies have been accepted"))
return True
except TimeoutException as e:
return False
Expand Down Expand Up @@ -340,9 +340,10 @@ def apply_to_flat(
web_driver.find_element(By.XPATH, "//button[@type='submit']").click()

# Send e-mail
if not test:
if not test and user_profile.notifications_email:
notifications.send_email_notification(
email,
user_profile.notifications_email,
f"[Applied] {flat_title}",
f"Appartment Link: {flat_link}\n\nYour Profile: {user_profile}",
pdf_path,
Expand Down Expand Up @@ -378,7 +379,7 @@ def process_flats(
time.sleep(int(refresh_internal) * 60)
continue

LOG.info(color_me.green(f"Found {len(all_flats)} flat(s) in total."))
LOG.info(color_me.green(f"Found {len(all_flats)} flat(s) in total"))

# Save locally
if not test:
Expand Down
11 changes: 8 additions & 3 deletions wbmbot_v2/utility/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ def setup_wbm_config():
emails = []
while True:
email = input(
"Please input email address and confirm with enter, type 'exit' to exit: "
"Please input an e-mail address to apply with and confirm with enter, type 'exit' to exit: "
).lower()
if email == "exit":
break
emails.append(email)
data["emails"] = emails

# Collect user e-mail that will be used for notifications
data["notifications_email"] = input(
"Please input (or leave empty) the e-mail (@outlook only) that you want to send notifications from: "
)

# Collect additional user information
data["street"] = input(
"Please input your street and street number and confirm with enter or leave empty and skip with enter: "
Expand All @@ -56,7 +61,7 @@ def setup_wbm_config():
"Until when will the WBS be valid? Please enter the date in format month/day/year: "
)
data["wbs_num"] = input(
"What WBS number does your WBS show? Please enter WBS 100 / WBS 140 / WBS 160 / WBS 180: "
"What WBS number does your WBS show? Please enter WBS 100 / WBS 140 / WBS 160 / WBS 180 / WBS 220 / WBS 240: "
)
data["wbs_rooms"] = input(
"For how many rooms is your WBS valid? Please enter a number: "
Expand Down Expand Up @@ -90,7 +95,7 @@ def setup_wbm_config():
data["filter"] = []

# Log the completion of data collection
LOG.info(color_me.green("Done! Writing config file"))
LOG.info(color_me.green("Done! Writing config file..."))

# Write the collected data to a JSON file
with open(constants.wbm_config_name, "w") as outfile:
Expand Down

0 comments on commit 39e253d

Please sign in to comment.