Skip to content

Commit

Permalink
#50, #51 Message threads support (#57)
Browse files Browse the repository at this point in the history
* #50, #51 Telegram thread id support.
  • Loading branch information
butorov committed May 6, 2024
1 parent bcf9747 commit 616f7da
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions sentry_telegram/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class TelegramNotificationsOptionsForm(notify.NotificationConfigurationForm):
receivers = forms.CharField(
label=_('Receivers'),
widget=forms.Textarea(attrs={'class': 'span6'}),
help_text=_('Enter receivers IDs (one per line). Personal messages, group chats and channels also available.'))

help_text=_('Enter receivers IDs (one per line). Personal messages, group chats and channels also available. '
'If you want to specify a thread ID, separate it with "/" (e.g. "12345/12").'),
)
message_template = forms.CharField(
label=_('Message template'),
widget=forms.Textarea(attrs={'class': 'span4'}),
Expand Down Expand Up @@ -87,7 +88,8 @@ def get_config(self, project, **kwargs):
'name': 'receivers',
'label': 'Receivers',
'type': 'textarea',
'help': 'Enter receivers IDs (one per line). Personal messages, group chats and channels also available.',
'help': 'Enter receivers IDs (one per line). Personal messages, group chats and channels also available. '
'If you want to specify a thread ID, separate it with "/" (e.g. "12345/12").',
'validators': [],
'required': True,
},
Expand Down Expand Up @@ -153,14 +155,16 @@ def build_url(self, project):
def get_message_template(self, project):
return self.get_option('message_template', project)

def get_receivers(self, project):
receivers = self.get_option('receivers', project)
def get_receivers(self, project) -> list[list[str, str]]:
receivers = self.get_option('receivers', project).strip()
if not receivers:
return []
return list([line.strip() for line in receivers.strip().splitlines() if line.strip()])
return list([line.strip().split('/', maxsplit=1) for line in receivers.splitlines() if line.strip()])

def send_message(self, url, payload, receiver):
payload['chat_id'] = receiver
def send_message(self, url, payload, receiver: list[str, str]):
payload['chat_id'] = receiver[0]
if len(receiver) > 1:
payload['message_thread_id'] = receiver[1]
self.logger.debug('Sending message to %s' % receiver)
response = safe_urlopen(
method='POST',
Expand All @@ -174,7 +178,7 @@ def send_message(self, url, payload, receiver):
def notify_users(self, group, event, fail_silently=False, **kwargs):
self.logger.debug('Received notification for event: %s' % event)
receivers = self.get_receivers(group.project)
self.logger.debug('for receivers: %s' % ', '.join(receivers or ()))
self.logger.debug('for receivers: %s' % ', '.join(['/'.join(item) for item in receivers] or ()))
payload = self.build_message(group, event)
self.logger.debug('Built payload: %s' % payload)
url = self.build_url(group.project)
Expand Down

0 comments on commit 616f7da

Please sign in to comment.