oncall-engine/engine/apps/telegram/decorators.py

86 lines
2.5 KiB
Python
Raw Permalink Normal View History

import logging
from functools import wraps
from telegram import error
from apps.telegram.client import TelegramClient
logger = logging.getLogger(__name__)
def handle_missing_token(f):
@wraps(f)
def decorated(*args, **kwargs):
try:
TelegramClient()
except error.InvalidToken as e:
logger.warning(
"Tried to initialize a Telegram client, but TELEGRAM_TOKEN live setting is invalid or missing. "
f"Exception: {e}"
)
return
else:
return f(*args, **kwargs)
return decorated
def ignore_bot_deleted(f):
@wraps(f)
def decorated(*args, **kwargs):
try:
return f(*args, **kwargs)
except error.Unauthorized:
logger.warning(f"Tried to send Telegram message, but user deleted the bot. args: {args}, kwargs: {kwargs}")
return decorated
def ignore_message_unchanged(f):
@wraps(f)
def decorated(*args, **kwargs):
try:
return f(*args, **kwargs)
except error.BadRequest as e:
fix `apps.telegram.tasks.send_log_and_actions_message` retrying tasks (#4851) # What this PR does It _appears_ like Telegram may have changed one of the error messages they return for `telegram.error.BadRequest`. This _may_ be causing us to infinitely retry some of these tasks. Previously we were checking for two variants of the same type of error message: - "Message to reply not found" - "Replied message not found" _However_, if I search for the following [in the logs](https://ops.grafana-ops.net/goto/hMgBb8CSR?orgId=1): ```logql {namespace="amixr-prod"} |~ `(Message to be replied not found|Message to reply not found|Replied message not found)` ```` I _only_ see references to "Message to be replied not found". I have updated references to the former to this new error log message we are seeing. Also: - deduplicate some of the words we check for in `telegram.error.BadRequest` and `telegram.error.Unauthorized` into `apps.telegram.client.TelegramClient.BadRequestMessage` and `apps.telegram.client.TelegramClient.UnauthorizedMessage` respectively - deduplicate some of the wording we use in the `reason` arg passed to `TelegramToUserConnector.create_telegram_notification_error` into `apps.telegram.models.connectors.personal.TelegramToUserConnector.NotificationErrorReason` - standardize how we check the `message` attribute of `telegram.error.TelegramError`s into a new `error_message_is` static method on `apps.telegram.client.TelegramClient` - previously we would check these error messages in two different ways: ```python3 # style 1 if "error message to check" in e.message: # do something # style 2 if error.message == "error message to check": # do something ``` ## Which issue(s) this PR closes Closes https://github.com/grafana/oncall-private/issues/2868 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes.
2024-08-19 14:05:40 -04:00
if TelegramClient.error_message_is(e, [TelegramClient.BadRequestMessage.MESSAGE_IS_NOT_MODIFIED]):
logger.warning(
f"Tried to change Telegram message, but update is identical to original message. "
f"args: {args}, kwargs: {kwargs}"
)
else:
raise e
return decorated
def ignore_message_to_edit_deleted(f):
@wraps(f)
def decorated(*args, **kwargs):
try:
return f(*args, **kwargs)
except error.BadRequest as e:
fix `apps.telegram.tasks.send_log_and_actions_message` retrying tasks (#4851) # What this PR does It _appears_ like Telegram may have changed one of the error messages they return for `telegram.error.BadRequest`. This _may_ be causing us to infinitely retry some of these tasks. Previously we were checking for two variants of the same type of error message: - "Message to reply not found" - "Replied message not found" _However_, if I search for the following [in the logs](https://ops.grafana-ops.net/goto/hMgBb8CSR?orgId=1): ```logql {namespace="amixr-prod"} |~ `(Message to be replied not found|Message to reply not found|Replied message not found)` ```` I _only_ see references to "Message to be replied not found". I have updated references to the former to this new error log message we are seeing. Also: - deduplicate some of the words we check for in `telegram.error.BadRequest` and `telegram.error.Unauthorized` into `apps.telegram.client.TelegramClient.BadRequestMessage` and `apps.telegram.client.TelegramClient.UnauthorizedMessage` respectively - deduplicate some of the wording we use in the `reason` arg passed to `TelegramToUserConnector.create_telegram_notification_error` into `apps.telegram.models.connectors.personal.TelegramToUserConnector.NotificationErrorReason` - standardize how we check the `message` attribute of `telegram.error.TelegramError`s into a new `error_message_is` static method on `apps.telegram.client.TelegramClient` - previously we would check these error messages in two different ways: ```python3 # style 1 if "error message to check" in e.message: # do something # style 2 if error.message == "error message to check": # do something ``` ## Which issue(s) this PR closes Closes https://github.com/grafana/oncall-private/issues/2868 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes.
2024-08-19 14:05:40 -04:00
if TelegramClient.error_message_is(e, [TelegramClient.BadRequestMessage.MESSAGE_TO_EDIT_NOT_FOUND]):
logger.warning(
f"Tried to edit Telegram message, but message was deleted. args: {args}, kwargs: {kwargs}"
)
else:
raise e
return decorated
def ignore_reply_to_message_deleted(f):
@wraps(f)
def decorated(*args, **kwargs):
try:
return f(*args, **kwargs)
except error.BadRequest as e:
fix `apps.telegram.tasks.send_log_and_actions_message` retrying tasks (#4851) # What this PR does It _appears_ like Telegram may have changed one of the error messages they return for `telegram.error.BadRequest`. This _may_ be causing us to infinitely retry some of these tasks. Previously we were checking for two variants of the same type of error message: - "Message to reply not found" - "Replied message not found" _However_, if I search for the following [in the logs](https://ops.grafana-ops.net/goto/hMgBb8CSR?orgId=1): ```logql {namespace="amixr-prod"} |~ `(Message to be replied not found|Message to reply not found|Replied message not found)` ```` I _only_ see references to "Message to be replied not found". I have updated references to the former to this new error log message we are seeing. Also: - deduplicate some of the words we check for in `telegram.error.BadRequest` and `telegram.error.Unauthorized` into `apps.telegram.client.TelegramClient.BadRequestMessage` and `apps.telegram.client.TelegramClient.UnauthorizedMessage` respectively - deduplicate some of the wording we use in the `reason` arg passed to `TelegramToUserConnector.create_telegram_notification_error` into `apps.telegram.models.connectors.personal.TelegramToUserConnector.NotificationErrorReason` - standardize how we check the `message` attribute of `telegram.error.TelegramError`s into a new `error_message_is` static method on `apps.telegram.client.TelegramClient` - previously we would check these error messages in two different ways: ```python3 # style 1 if "error message to check" in e.message: # do something # style 2 if error.message == "error message to check": # do something ``` ## Which issue(s) this PR closes Closes https://github.com/grafana/oncall-private/issues/2868 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes.
2024-08-19 14:05:40 -04:00
if TelegramClient.error_message_is(e, [TelegramClient.BadRequestMessage.MESSAGE_TO_BE_REPLIED_NOT_FOUND]):
logger.warning(
f"Tried to reply to Telegram message, but message was deleted. args: {args}, kwargs: {kwargs}"
)
else:
raise e
return decorated