Skip to content

Commit

Permalink
Merge pull request #8 from ruben69695/feature/tgbot-contact-me
Browse files Browse the repository at this point in the history
Added Telegram integration to receive contact message in the bot chat
  • Loading branch information
ruben69695 authored Jun 25, 2022
2 parents a1ffc0d + 474102c commit 991a9fa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
19 changes: 19 additions & 0 deletions core/services/telegramService.py
Original file line number Diff line number Diff line change
@@ -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'<b>New Contact Message 📬</b>\nDear lord, you have received a new message from {name} ({email}), the message is the next one:\n\n<pre>{message}</pre>'
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

13 changes: 10 additions & 3 deletions core/views.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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")
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions webpersonal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 991a9fa

Please sign in to comment.