toxicity-detector

This commit is contained in:
2024-02-12 15:50:35 +03:00
parent ec82174dc9
commit 7fd358931a
10 changed files with 166 additions and 255 deletions

View File

@@ -27,10 +27,9 @@ async def show_announce(msg):
newcomer_message = get_newcomer_message(msg)
userphotos_response = await telegram_api("getUserphotos", user_id=from_id)
logger.debug(userphotos_response)
file_id = ""
if userphotos_response["ok"] and userphotos_response["result"]["total_count"] > 0:
if isinstance(userphotos_response, dict) and userphotos_response["ok"] and userphotos_response["result"]["total_count"] > 0:
logger.info("showing button with photo")
file_id = userphotos_response["result"]["photos"][0][0]["file_id"]

View File

@@ -5,15 +5,26 @@ from bot.config import BOT_TOKEN
import logging
# Create a logger instance
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('[tgbot.api] ')
logging.basicConfig(level=logging.DEBUG)
apiBase = f"https://api.telegram.org/bot{BOT_TOKEN}/"
api_base = f"https://api.telegram.org/bot{BOT_TOKEN}/"
async def telegram_api(endpoint: str, **kwargs):
async with aiohttp.ClientSession() as session:
async with session.get(apiBase + f"{endpoint}?{urlencode(kwargs)}") as response:
data = await response.json()
logger.info("Telegram API response: %s", data)
return data
async def telegram_api(endpoint: str, json_data=None, **kwargs):
try:
url = api_base + f"{endpoint}?{urlencode(kwargs)}"
is_polling = endpoint == 'getUpdates'
headers = {'Content-Type': 'application/json'}
async with aiohttp.ClientSession() as session:
url = api_base + f"{endpoint}?{urlencode(kwargs)}"
if not is_polling:
logger.info(f' >>> {url} {json_data if json_data else ""}')
async with session.get(url, data=json.dumps(json_data), headers=headers) as response:
data = await response.json()
if not is_polling:
logger.info(f' <<< {data}')
return data
except Exception as ex:
import traceback
traceback.print_exc()