2022-06-03 08:09:47 -06:00
|
|
|
import logging
|
2023-06-29 16:01:52 +02:00
|
|
|
import typing
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.core.validators import MinLengthValidator
|
|
|
|
|
from django.db import models
|
|
|
|
|
from django.db.models import Q
|
|
|
|
|
from telegram import error
|
|
|
|
|
|
|
|
|
|
from apps.alerts.models import AlertGroup
|
|
|
|
|
from apps.telegram.client import TelegramClient
|
|
|
|
|
from apps.telegram.models import TelegramMessage
|
2023-04-17 15:16:18 +08:00
|
|
|
from common.insight_log.chatops_insight_logs import ChatOpsEvent, ChatOpsTypePlug, write_chatops_insight_log
|
2022-06-03 08:09:47 -06:00
|
|
|
from common.public_primary_keys import generate_public_primary_key, increase_public_primary_key_length
|
|
|
|
|
|
2023-06-29 16:01:52 +02:00
|
|
|
if typing.TYPE_CHECKING:
|
|
|
|
|
from django.db.models.manager import RelatedManager
|
|
|
|
|
|
|
|
|
|
from apps.alerts.models import ChannelFilter
|
|
|
|
|
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_public_primary_key_for_telegram_to_at_connector() -> str:
|
|
|
|
|
prefix = "Z"
|
|
|
|
|
new_public_primary_key = generate_public_primary_key(prefix)
|
|
|
|
|
|
|
|
|
|
failure_counter = 0
|
|
|
|
|
while TelegramToOrganizationConnector.objects.filter(public_primary_key=new_public_primary_key).exists():
|
|
|
|
|
new_public_primary_key = increase_public_primary_key_length(
|
|
|
|
|
failure_counter=failure_counter, prefix=prefix, model_name="TelegramToOrganizationConnector"
|
|
|
|
|
)
|
|
|
|
|
failure_counter += 1
|
|
|
|
|
|
|
|
|
|
return new_public_primary_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TelegramToOrganizationConnector(models.Model):
|
2023-06-29 16:01:52 +02:00
|
|
|
channel_filter: "RelatedManager['ChannelFilter']"
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
public_primary_key = models.CharField(
|
|
|
|
|
max_length=20,
|
|
|
|
|
validators=[MinLengthValidator(settings.PUBLIC_PRIMARY_KEY_MIN_LENGTH + 1)],
|
|
|
|
|
unique=True,
|
|
|
|
|
default=generate_public_primary_key_for_telegram_to_at_connector,
|
|
|
|
|
)
|
|
|
|
|
organization = models.ForeignKey(
|
|
|
|
|
"user_management.Organization",
|
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
|
related_name="telegram_channel",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
is_default_channel = models.BooleanField(null=True, default=False)
|
|
|
|
|
|
|
|
|
|
channel_chat_id = models.CharField(unique=True, max_length=100)
|
|
|
|
|
channel_name = models.CharField(max_length=100, null=True, default=None)
|
|
|
|
|
|
|
|
|
|
discussion_group_chat_id = models.CharField(unique=True, max_length=100)
|
|
|
|
|
discussion_group_name = models.CharField(max_length=100, null=True, default=None)
|
|
|
|
|
|
|
|
|
|
datetime = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
|
|
NUM_GROUPED_ALERTS_IN_COMMENTS = 10
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_configured(self) -> bool:
|
|
|
|
|
return self.channel_chat_id is not None and self.discussion_group_chat_id is not None
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2023-06-29 16:01:52 +02:00
|
|
|
def get_channel_for_alert_group(cls, alert_group: AlertGroup) -> typing.Optional["TelegramToOrganizationConnector"]:
|
2022-06-03 08:09:47 -06:00
|
|
|
# TODO: add custom queryset
|
|
|
|
|
dm_messages_exist = alert_group.telegram_messages.filter(
|
|
|
|
|
~Q(chat_id__startswith="-")
|
|
|
|
|
& Q(
|
|
|
|
|
message_type__in=(
|
|
|
|
|
TelegramMessage.PERSONAL_MESSAGE,
|
|
|
|
|
TelegramMessage.FORMATTING_ERROR,
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
).exists()
|
|
|
|
|
|
|
|
|
|
if dm_messages_exist:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
default_channel = cls.objects.filter(
|
|
|
|
|
organization=alert_group.channel.organization, is_default_channel=True
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
if alert_group.channel_filter is None:
|
|
|
|
|
return default_channel
|
|
|
|
|
|
2022-06-28 09:11:19 +01:00
|
|
|
if not alert_group.channel_filter.notify_in_telegram:
|
|
|
|
|
return None
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
return alert_group.channel_filter.telegram_channel or default_channel
|
|
|
|
|
|
|
|
|
|
def make_channel_default(self, author):
|
|
|
|
|
try:
|
|
|
|
|
old_default_channel = TelegramToOrganizationConnector.objects.get(
|
|
|
|
|
organization=self.organization, is_default_channel=True
|
|
|
|
|
)
|
|
|
|
|
old_default_channel.is_default_channel = False
|
|
|
|
|
old_default_channel.save(update_fields=["is_default_channel"])
|
|
|
|
|
except TelegramToOrganizationConnector.DoesNotExist:
|
|
|
|
|
old_default_channel = None
|
|
|
|
|
|
|
|
|
|
self.is_default_channel = True
|
|
|
|
|
self.save(update_fields=["is_default_channel"])
|
2022-08-24 12:04:44 +05:00
|
|
|
write_chatops_insight_log(
|
|
|
|
|
author=author,
|
|
|
|
|
event_name=ChatOpsEvent.DEFAULT_CHANNEL_CHANGED,
|
2023-04-17 15:16:18 +08:00
|
|
|
chatops_type=ChatOpsTypePlug.TELEGRAM.value,
|
2022-08-24 12:04:44 +05:00
|
|
|
prev_channel=old_default_channel.channel_name if old_default_channel else None,
|
|
|
|
|
new_channel=self.channel_name,
|
2022-06-03 08:09:47 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def send_alert_group_message(self, alert_group: AlertGroup) -> None:
|
|
|
|
|
telegram_client = TelegramClient()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
telegram_client.send_message(
|
|
|
|
|
chat_id=self.channel_chat_id, message_type=TelegramMessage.ALERT_GROUP_MESSAGE, alert_group=alert_group
|
|
|
|
|
)
|
|
|
|
|
except error.BadRequest as e:
|
2024-08-19 14:05:40 -04:00
|
|
|
if TelegramClient.error_message_is(
|
|
|
|
|
e,
|
|
|
|
|
[
|
|
|
|
|
TelegramClient.BadRequestMessage.NEED_ADMIN_RIGHTS_IN_THE_CHANNEL,
|
|
|
|
|
TelegramClient.BadRequestMessage.CHAT_NOT_FOUND,
|
|
|
|
|
],
|
|
|
|
|
):
|
2022-06-03 08:09:47 -06:00
|
|
|
logger.warning(
|
|
|
|
|
f"Could not send alert group to Telegram channel with id {self.channel_chat_id} "
|
2024-08-19 14:05:40 -04:00
|
|
|
f"due to {e.message}. alert_group {alert_group.pk}"
|
2022-06-03 08:09:47 -06:00
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
telegram_client.send_message(
|
|
|
|
|
chat_id=self.channel_chat_id, message_type=TelegramMessage.FORMATTING_ERROR, alert_group=alert_group
|
|
|
|
|
)
|