telegram_bots/aggregator/signals.py

24 lines
672 B
Python
Raw Normal View History

2019-01-30 20:52:05 +00:00
from pyrogram import Error
from .client import get_client
from .models import Chat, AggregationSource
from .tasks import collect_new_messages
def aggregation_source_deleted(sender, instance: AggregationSource, **kwargs):
if not AggregationSource.objects.filter(chat_id=instance.chat_id):
Chat.objects.filter(chat_id=instance.chat_id).delete()
def chat_created(sender, instance: Chat, created, **kwargs):
if created:
2019-01-30 21:14:12 +00:00
collect_new_messages.delay(instance.pk)
2019-01-30 20:52:05 +00:00
def chat_deleted(sender, instance: Chat, **kwargs):
with get_client() as client:
try:
client.leave_chat(instance.chat_id)
except Error:
pass