diff --git a/core/services/telegramService.py b/core/services/telegramService.py new file mode 100644 index 0000000..4717cef --- /dev/null +++ b/core/services/telegramService.py @@ -0,0 +1,19 @@ +import requests +from django.conf import settings + +class TelegramService: + + def __init__(self): + self.BASE_URL = 'https://api.telegram.org/bot' + self.TOKEN = settings.TG_TOKEN + self.CHAT_ID = settings.TG_CHAT_ID + + def sendContactNotification(self, name, email, message): + url = self.BASE_URL + self.TOKEN + '/sendMessage' + message = f'New Contact Message 📬\nDear lord, you have received a new message from {name} ({email}), the message is the next one:\n\n
{message}' + payload = {'chat_id': self.CHAT_ID, 'parse_mode': 'HTML', 'text': message } + r = requests.post(url, json=payload, timeout=5.0) + print(r.status_code) + print(r.text) + return r.status_code == requests.codes.ok + diff --git a/core/views.py b/core/views.py index 86113d4..89fe788 100644 --- a/core/views.py +++ b/core/views.py @@ -1,6 +1,7 @@ from django.shortcuts import render, HttpResponse from .forms import ContactMessageForm from .services.emailService import EmailService +from .services.telegramService import TelegramService from django.conf import settings # Create your views here. @@ -20,10 +21,16 @@ def createContactMessage(request): new_message = form.save() fromEmail = settings.DEFAULT_FROM_EMAIL subject = new_message.name + ', with email (' + new_message.email + ') needs your attention' - service = EmailService(fromEmail, [fromEmail], subject, new_message.message) - service.send() + email_service = EmailService(fromEmail, [fromEmail], subject, new_message.message) + email_service.send() - if service.sended: + if email_service.sended: print('Email sended correctly') + + if settings.TG_ACTIVE: + tg_service = TelegramService() + sent_ok = tg_service.sendContactNotification(new_message.name, new_message.email, new_message.message) + if sent_ok: + print('Telegram notification sended correctly') return render(request, "core/contact.html") diff --git a/docker-compose.yml b/docker-compose.yml index 803b22f..1ad0d80 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -32,6 +32,9 @@ services: - EMAIL_HOST_PASSWORD=${EMAIL_HOST_PASSWORD} - EMAIL_USE_TLS=${EMAIL_USE_TLS} - DEFAULT_FROM_EMAIL=${DEFAULT_FROM_EMAIL} + - TG_ACTIVE=${TG_ACTIVE} + - TG_TOKEN=${TG_TOKEN} + - TG_CHAT_ID=${TG_CHAT_ID} depends_on: - db diff --git a/requirements.txt b/requirements.txt index 58efeb2..731d965 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ psycopg2-binary==2.9.3 dj-database-url==0.5.0 gunicorn==20.1.0 whitenoise==6.2.0 +requests==2.28.0 diff --git a/webpersonal/settings.py b/webpersonal/settings.py index 8ff982e..fdf1545 100644 --- a/webpersonal/settings.py +++ b/webpersonal/settings.py @@ -139,3 +139,8 @@ EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD', None) EMAIL_USE_TLS = False if os.environ.get('EMAIL_USE_TLS', 'False') == 'False' else True DEFAULT_FROM_EMAIL = os.environ.get('DEFAULT_FROM_EMAIL', None) + +# Telegram +TG_ACTIVE = False if os.environ.get('TG_ACTIVE', 'False') == 'False' else True +TG_TOKEN = os.environ.get('TG_TOKEN', None) +TG_CHAT_ID = int(os.environ.get('TG_CHAT_ID', 0))