/
home
/
liman
/
liman-backend
/
utility
/
messenger
/
File Upload :
llllll
Current File: /home/liman/liman-backend/utility/messenger/Messenger.py
""" Messenger Model""" import os import smtplib import string from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from os.path import basename import firebase_admin from firebase_admin import messaging, credentials from zeep import Client as SoapClient from celery import Celery from limanAuth.models import User from config import Config from .models import NotificationHistory, SMSHistory from .SMS import SMS from celery import shared_task app = Celery() class Messenger: defaultFirebaseApp = None @staticmethod def initialize_firebase(): """ initialize firebase Connection.""" if Messenger.defaultFirebaseApp is None: dir_path = os.path.dirname(os.path.realpath(__file__)) firebase_certificate = credentials.Certificate( dir_path + "/assets/firebase-cert.json" ) Messenger.defaultFirebaseApp = firebase_admin.initialize_app(firebase_certificate) @staticmethod @shared_task(name='send_public_notification') def send_public_notification(users, title: string, message: string, data: dict = None): Messenger.initialize_firebase() messages = [] if len(users) > 0: for firebase_user in users: messages.append( messaging.Message( notification=messaging.Notification(title=title, body=message), token=firebase_user.fcm_token, data=data ) ) NotificationHistory.objects.create( firebase_user=firebase_user, title=title, message=message, data=data, was_public=True) response = messaging.send_all(messages) return response return False @staticmethod @shared_task(name='send_private_notification_to_user') def send_private_notification_to_user(model_name: string, user: User, device_type: int, title: string, message: string, data: dict = None, app_name: string = "account"): """ Send a message to a specific user based on model name, user ID, and device type """ Messenger.initialize_firebase() msg = messaging.Message( notification=messaging.Notification(title=title, body=message), token=user.fcm_token, data=data ) NotificationHistory.objects.create( user=user, title=title, message=message, data=data, was_public=False) response = messaging.send(msg) return response class EmailAttachment: def __init__(self, file_content, file_name): self.fileName = file_name self.fileContent = file_content def get_attachment_for_send(self): return MIMEApplication(self.fileContent, Name=basename(self.fileName)) @staticmethod @shared_task(name='send_email_to_email_address') def send_email_to_email_address(address: string, title: string, message_plain_text_part: string = None, message_html_part: string = None, sender_email=None, attachments=[]): parts = [] if message_plain_text_part is not None: parts.append(MIMEText(message_plain_text_part, "plain")) if message_html_part is not None: parts.append(MIMEText(message_html_part, "html")) message = MIMEMultipart("alternative") message["Subject"] = title if sender_email is None: message["From"] = Config.SMTP.USERNAME else: message["From"] = sender_email message["To"] = address for p in parts: message.attach(p) if attachments is not None: for attachment in attachments: message.attach(attachment.get_attachment_for_send()) with smtplib.SMTP_SSL(Config.SMTP.SERVER, Config.SMTP.PORT) as server: # server.starttls() server.login(Config.SMTP.USERNAME, Config.SMTP.PASSWORD) server.sendmail(message["From"], address, message.as_string()) return True @staticmethod # @shared_task(name='send_sms_to_phone_number') def send_sms_to_phone_number(phone_number, message=None, country_prefix="98", pattern=None, tokens=None, driver=Config.SMS.DRIVERS['ACTIVE']): phone_number = standard_mobile_number(phone_number) if not pattern: history = SMSHistory.objects.create( message=message, to_phone_numbers=[phone_number], from_number=Config.SMS.DRIVERS[Config.SMS.DRIVERS['ACTIVE']]['NUMBERS'][0] ) else: history = SMSHistory.objects.create( message={'pattern': pattern, 'tokens': {**tokens}}, to_phone_numbers=[phone_number], from_number=Config.SMS.DRIVERS[Config.SMS.DRIVERS['ACTIVE']]['NUMBERS'][0] ) try: driver = getattr(SMS, driver) response = driver.send_message(phone_number=phone_number, message=message, pattern=pattern, tokens=tokens) try: history.service_response = str(response) except Exception as e: pass history.save() return True, response except Exception as e: print("Error 39b5a7t23 :", str(e)) history.is_get_error = True history.error_body = str(e) history.save() return False, None def standard_mobile_number(phone_number): country_prefix = '98' if phone_number[0] == "+": if phone_number[1:3] == "00": phone_number = phone_number[3:] else: phone_number = phone_number[1:] elif phone_number[0:2] == "00": phone_number = phone_number[2:] elif phone_number[0] == "0": phone_number = country_prefix + phone_number[1:] elif phone_number[0:len(country_prefix)] != country_prefix: phone_number = country_prefix + phone_number return phone_number
Copyright ©2k19 -
Hexid
|
Tex7ure