Skip to content

Commit

Permalink
move to pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
da-maltsev committed Jul 6, 2023
1 parent 0f324de commit eba1a7a
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 211 deletions.
7 changes: 0 additions & 7 deletions .flake8

This file was deleted.

5 changes: 0 additions & 5 deletions .isort.cfg

This file was deleted.

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ install-deps: deps
pip-sync requirements.txt

deps:
pip-compile requirements.in
pip-compile --resolver=backtracking --output-file=requirements.txt pyproject.toml

dev-deps: deps
pip-compile dev-requirements.in
pip-compile --resolver=backtracking --extra=dev --output-file=dev-requirements.txt pyproject.toml

lint:
flake8 *.py
Expand Down
56 changes: 27 additions & 29 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
from typing import Optional

import os
from telegram import Message, Update
from telegram.ext import CallbackContext, Dispatcher, MessageHandler, Updater
from telegram.ext.filters import BaseFilter, Filters
from telegram.ext import Application, ContextTypes, MessageHandler
from telegram.ext.filters import TEXT, BaseFilter

import rekognition
import text
from filters import ContainsLink, ContainsTelegramContact, ContainsThreeOrMoreEmojies, IsMedia, IsMessageOnBehalfOfChat, with_default_filters
from helpers import DB_ENABLED, enable_logging, in_production, init_sentry


def get_profile_picture(message: Message) -> Optional[str]:
photos = message.from_user.get_profile_photos()
async def get_profile_picture(message: Message) -> str | None:
if message.from_user:
photos = await message.from_user.get_profile_photos()

if photos is not None and photos.total_count > 0:
profile_picture = photos.photos[0][0].get_file()
return profile_picture.file_path
if photos is not None and photos.total_count > 0:
profile_picture = await photos.photos[0][0].get_file()
return profile_picture.file_path


def log_message(message: Message, action: Optional[str] = ''):
async def log_message(message: Message | None, action: str | None = ''):
"""Create a log entry for telegram message"""

if message is None or not DB_ENABLED():
if message is None or not DB_ENABLED() or not message.from_user:
return
from models import LogEntry

picture_url = await get_profile_picture(message)

LogEntry.create(
user_id=message.from_user.id,
chat_id=message.chat_id,
message_id=message.message_id,
text=message.text or '',
meta={
'tags': [
*rekognition.get_labels(image_url=get_profile_picture(message)),
*rekognition.get_labels(image_url=picture_url),
*text.Labels(message.text)(),
],
},
Expand All @@ -42,14 +44,12 @@ def log_message(message: Message, action: Optional[str] = ''):
)


def delete(update: Update, context: CallbackContext):
async def delete(update: Update, context: ContextTypes.DEFAULT_TYPE):
message = update.message or update.edited_message

log_message(message, action='delete')
message.bot.delete_message(
message_id=message.message_id,
chat_id=message.chat_id,
)
if message:
await log_message(message, action='delete')
await message.delete()


def delete_messages_that_match(*filters: BaseFilter) -> MessageHandler:
Expand All @@ -66,31 +66,29 @@ def delete_messages_that_match(*filters: BaseFilter) -> MessageHandler:
raise RuntimeError('Please set BOT_TOKEN environment variable')
app_name = os.getenv('BOT_NAME')

bot = Updater(token=bot_token)
dispatcher: Dispatcher = bot.dispatcher # type: ignore
bot = Application.builder().token(bot_token).build()

dispatcher.add_handler(delete_messages_that_match(ContainsTelegramContact()))
dispatcher.add_handler(delete_messages_that_match(ContainsLink()))
dispatcher.add_handler(delete_messages_that_match(IsMessageOnBehalfOfChat()))
dispatcher.add_handler(delete_messages_that_match(ContainsThreeOrMoreEmojies()))
dispatcher.add_handler(delete_messages_that_match(IsMedia()))
bot.add_handler(delete_messages_that_match(ContainsTelegramContact()))
bot.add_handler(delete_messages_that_match(ContainsLink()))
bot.add_handler(delete_messages_that_match(IsMessageOnBehalfOfChat()))
bot.add_handler(delete_messages_that_match(ContainsThreeOrMoreEmojies()))
bot.add_handler(delete_messages_that_match(IsMedia()))

if DB_ENABLED(): # log all not handled messages
from models import create_tables
create_tables() # type: ignore
dispatcher.add_handler(
MessageHandler(filters=Filters.text, callback=lambda update, context: log_message(update.message or update.edited_message)),
bot.add_handler(
MessageHandler(filters=TEXT, callback=lambda update, context: log_message(update.message or update.edited_message)),
)

if in_production():
init_sentry()
bot.start_webhook(
bot.run_webhook(
listen='0.0.0.0',
port=8000,
url_path=bot_token,
webhook_url=f'https://{app_name}.tough-dev.school/' + bot_token,
)
bot.idle()
else: # bot is running on the dev machine
enable_logging()
bot.start_polling()
bot.run_polling()
33 changes: 0 additions & 33 deletions dev-requirements.in

This file was deleted.

Loading

0 comments on commit eba1a7a

Please sign in to comment.