Skip to content

Commit

Permalink
Create signals from borrowings to bot for notifications about new bor…
Browse files Browse the repository at this point in the history
…rowing
  • Loading branch information
Kateryna-Bordonos17 committed Dec 18, 2023
1 parent dfe4615 commit 010852a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
3 changes: 3 additions & 0 deletions borrowing_service/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
class BorrowingServiceConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "borrowing_service"

def ready(self):
import borrowing_service.signals
22 changes: 22 additions & 0 deletions borrowing_service/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from notifications_service.management.commands.run_telegram_bot import (
send_notification
)
from borrowing_service.models import Borrowing
from notifications_service.models import TelegramUser


@receiver(post_save, sender=Borrowing)
def notify_new_borrowing(sender, instance, created, **kwargs):
if created:
borrowing_info = (
"New borrowing created:\n\n"
f"-Book: {instance.book}\n"
f"--Borrow date: {instance.borrow_date}\n"
f"--Expected return date: {instance.expected_return_date}\n"
)
send_notification(
TelegramUser.objects.get(user_id=instance.user_id).chat_id,
borrowing_info
)
7 changes: 4 additions & 3 deletions notifications_service/bot_commands.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from django.contrib.auth import get_user_model

from borrowing_service.models import Borrowing
from customer.models import Customer
from notifications_service.models import TelegramUser


def is_user(message):
user_email = message.text
if Customer.objects.filter(email=user_email).exists():
user = Customer.objects.get(email=user_email)
if get_user_model().objects.filter(email=user_email).exists():
user = get_user_model().objects.get(email=user_email)
try:
TelegramUser.objects.get(user_id=user)
except TelegramUser.DoesNotExist:
Expand Down
49 changes: 25 additions & 24 deletions notifications_service/management/commands/run_telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,36 @@
user_borrowings,
is_user)

bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN)


@bot.message_handler(commands=["start"])
def login(message):
if not is_user(message):
bot.send_message(message.chat.id, text="Enter your email to login ")
bot.register_next_step_handler(message, login)
else:
authorised(message)

class Command(BaseCommand):
def handle(self, *args, **options):
bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN)

def start_telegram_bot():
bot.polling()
def authorised(message):
welcome_message(bot, message)

@bot.message_handler(commands=["start"])
def login(message):
if not is_user(message):
bot.send_message(message.chat.id,
text="Enter your email to login: ")
bot.register_next_step_handler(message, login)
else:
authorised(message)

def authorised(message):
welcome_message(bot, message)
@bot.message_handler(commands=["help"])
def send_help_information(message: telebot.types.Message) -> None:
help_information(bot, message)

@bot.message_handler(commands=["help"])
def send_help_information(message: telebot.types.Message) -> None:
help_information(bot, message)

@bot.message_handler(commands=["my_borrowings"])
def send_users_borrowings(message: telebot.types.Message) -> None:
user_borrowings(bot, message)
@bot.message_handler(commands=["my_borrowings"])
def send_users_borrowings(message: telebot.types.Message) -> None:
user_borrowings(bot, message)

def send_telegram_notification(message):
bot.send_message(CHAT_ID, text=message)

start_telegram_bot()
def send_notification(chat_id, message):
bot.send_message(chat_id, message)


class Command(BaseCommand):
def handle(self, *args, **options):
bot.polling()

0 comments on commit 010852a

Please sign in to comment.