def send_message(self, main_website_place: int, all_websites: int, recipient_number: str) -> None: """ Fill SMS_TEMPLATE with provided data. Send sms message to recipient_number phone number or phone number from .env file. :param main_website_place: int :param all_websites: int :param recipient_number: str :return: None """ if not recipient_number: recipient_number = os.environ.get('DEFAULT_RECIPIENT_PHONE_NUMBER') text = SMS_TEMPLATE.format(main_website_place=main_website_place, all_websites=all_websites) try: message = self.client.messages.create( body=text, from_=os.environ.get('mail_template'), to=recipient_number) logger.info(f'\n[INFO][TwilioSMSSender][send_message]' f'\n{message.sid}') except Exception as e: logger.info(f'\n[ERROR][TwilioSMSSender][send_message]\n{e}')
def run(self) -> None: """ Main method in WebsiteMeasure class. Logging to logger start info. Gets arguments by get_arguments method and validate mail/phone_number if passed. Compare loading time of collected websites. Send notifications and save results of measurement. :return: None """ logger.info(f'\nApplication started!\n{str(sys.argv[1:])}') parsed = self.get_arguments(sys.argv[1:]) if parsed.mail: Validate('mail').is_valid(parsed.mail) self.mail = parsed.mail if parsed.phone_number: Validate('phone_number').is_valid(parsed.phone_number) self.sms = parsed.phone_number self.compare_websites(parsed.url, parsed.list) self.send_notifications() self.print_result() self.write_result_to_file()
def append(self, text: str): """ Create and append text file with text data. :param text: str :return: None """ with open(self.file_path, 'a') as self.file: self.file.write(text) logger.info(f'\n[INFO][WriteFile][append]' f'\nAppend file {self.file_name}\nText: {text}')
def __enter__(self): """ Context manager method. Create connection with server and login to it with provided credentials after init. :return: self """ self.server = smtplib.SMTP(self.smtp_server, self.port) self.server.ehlo() self.server.starttls() try: self.server.login(self.mail, self.password) logger.info(f'\n[INFO][MailSender][__enter__]' f'\nLogged with mail {self.mail}') except Exception as e: logger.error(f'\n[ERROR][MailSender][__enter__]' f'\nError with mail {self.mail}\n{e}') return self
def send_message(self, main_website_place: int, all_websites: int, recipient_number: str) -> None: """ Fill SMS_TEMPLATE with provided data. Print sms message with recipient_number phone number or phone number from .env file. :param main_website_place: int :param all_websites: int :param recipient_number: str :return: None """ if not recipient_number: recipient_number = os.environ.get('DEFAULT_RECIPIENT_PHONE_NUMBER') text = SMS_TEMPLATE.format(main_website_place=main_website_place, all_websites=all_websites, recipient_number=recipient_number) print('############## Dummy sms ##############') print(text) print('#######################################') logger.info(f'\n[INFO][DummySMSSender][send_message]' f'\nSMS message: {text}')
def send_message(self, main_website_place: int, all_websites: int, recipient_mail: str) -> None: """ Send prepared message to recipient_mail email address or address from .env file. :param main_website_place: int :param all_websites: int :param recipient_mail: str :return: None """ if not recipient_mail: recipient_mail = os.environ.get('DEFAULT_RECIPIENT_MAIL_ADDRESS') message = self.prepare_message(main_website_place, all_websites, recipient_mail) try: self.server.sendmail(self.mail, recipient_mail, message) logger.info(f'\n[INFO][MailSender][send_message]' f'\nEmail to {recipient_mail}\nMessage: {message}') except Exception as e: logger.info( f'\n[ERROR][MailSender][send_message]' f'\nEmail to {recipient_mail}\nMessage: {message}\n{e}')