Skip to content

Commit

Permalink
Merge pull request #5 from talgat-abdraimov/feature/sentry
Browse files Browse the repository at this point in the history
added sentry dsn
  • Loading branch information
talgat-abdraimov authored Aug 3, 2024
2 parents 79c3154 + bc5e6ca commit 7f290b5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
36 changes: 34 additions & 2 deletions code/bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from functools import wraps

import emoji
from loguru import logger
Expand All @@ -11,6 +12,18 @@
anthropic = Anthropic(api_key=settings.anthropic_api_key)


def logging(func):
@wraps(func)
async def wrapper(*args, **kwargs):
try:
return await func(*args, **kwargs)
except Exception as e:
logger.error('An error occurred: {e}', e=e, function=func.__name__)

return wrapper


@logging
async def text_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if emoji.is_emoji(update.message.text):
logger.warning(
Expand All @@ -36,6 +49,7 @@ async def text_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
context.job_queue.run_once(anthropic_api_call, 1, data=data, chat_id=update.effective_chat.id)


@logging
async def anthropic_api_call(context: ContextTypes.DEFAULT_TYPE) -> str:
job = context.job

Expand All @@ -46,18 +60,22 @@ async def anthropic_api_call(context: ContextTypes.DEFAULT_TYPE) -> str:
await context.bot.editMessageText(corrected_message, job.data['chat_id'], job.data['message_id'])


@logging
async def not_text_handler(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
username = update.effective_user.full_name or update.effective_user.username

logger.warning(f'User <{username}> sent a non-text message: {update.message}')
logger.warning(
'User <{username}> sent a non-text message: {message}', username=username, message=update.message
)

await update.message.reply_text('Please send me a text message.')


@logging
async def start_handler(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
username = update.effective_user.full_name or update.effective_user.username

logger.info(f'User <{username}> started the bot.')
logger.info('User <{username}> started the bot.', username=username)

await update.message.reply_text('Send me a text message to correct.')

Expand All @@ -80,4 +98,18 @@ def run_telegram_bot(token: str):
logger.add(sys.stdout, level='INFO', serialize=True)

logger.info('Starting the bot...')

if settings.sentry_dsn:
logger.info('Sentry is enabled.')

import sentry_sdk
from sentry_sdk.integrations.loguru import LoguruIntegration

sentry_sdk.init(
settings.sentry_dsn,
traces_sample_rate=0.1,
profiles_sample_rate=0.5,
integrations=[LoguruIntegration()],
)

run_telegram_bot(settings.bot_token)
1 change: 1 addition & 0 deletions code/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class Settings:
bot_token: str
anthropic_api_key: str
sentry_dsn: str | None = None

@staticmethod
def from_env() -> 'Settings':
Expand Down
8 changes: 6 additions & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ coverage==7.6.0
# via pytest-cov
distlib==0.3.8
# via virtualenv
emoji==2.12.1
# via -r requirements.in
execnet==2.1.1
# via pytest-xdist
factory-boy==3.3.0
Expand Down Expand Up @@ -45,7 +47,7 @@ inflection==0.5.1
# via pytest-factoryboy
iniconfig==2.0.0
# via pytest
msgspec==0.18.6
loguru==0.7.2
# via -r requirements.in
nodeenv==1.9.1
# via pre-commit
Expand Down Expand Up @@ -112,7 +114,9 @@ sniffio==1.3.1
# anyio
# httpx
typing-extensions==4.12.2
# via pytest-factoryboy
# via
# emoji
# pytest-factoryboy
tzlocal==5.2
# via apscheduler
virtualenv==20.26.3
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ python-telegram-bot[job-queue]==21.3
zimran-http==1.0.0
loguru==0.7.2
emoji==2.12.1
sentry-sdk[loguru]==2.12.0
9 changes: 8 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ certifi==2024.7.4
# via
# httpcore
# httpx
# sentry-sdk
emoji==2.12.1
# via -r requirements.in
h11==0.14.0
Expand All @@ -23,13 +24,17 @@ idna==3.7
# anyio
# httpx
loguru==0.7.2
# via -r requirements.in
# via
# -r requirements.in
# sentry-sdk
python-telegram-bot==21.3
# via -r requirements.in
pytz==2024.1
# via
# apscheduler
# python-telegram-bot
sentry-sdk==2.12.0
# via -r requirements.in
six==1.16.0
# via apscheduler
sniffio==1.3.1
Expand All @@ -40,5 +45,7 @@ typing-extensions==4.12.2
# via emoji
tzlocal==5.2
# via apscheduler
urllib3==2.2.2
# via sentry-sdk
zimran-http==1.0.0
# via -r requirements.in

0 comments on commit 7f290b5

Please sign in to comment.