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

53
main.py
View File

@@ -12,44 +12,39 @@ logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
state = dict()
api_url = f'https://api.telegram.org/bot{BOT_TOKEN}/'
async def fetch(session, url):
logger.debug(f"fetching: {url}")
async with session.get(url) as response:
logger.debug(response)
return await response.json()
async def start():
logger.info("\tstarted")
async with ClientSession() as session:
offset = 0 # начальное значение offset
while True:
updates = await fetch(session, f"{api_url}getUpdates?offset={offset}&allowed_updates=['message', 'join_chat_request', 'message_reaction']")
# logger.info('.' if updates['result'] == [] else updates)
for update in updates.get("result", []):
try:
message = update.get("message")
join_chat_request = update.get("join_chat_request")
message_reaction = update.get("message_reaction")
if message:
await messages_routing(message, state)
elif join_chat_request:
await handle_join_request(join_chat_request)
elif message_reaction:
await handle_reaction_on_request(message_reaction)
reponse = await telegram_api("getUpdates", offset=offset, allowed_updates=['message', 'message_reaction'])
if isinstance(reponse, dict):
result = reponse.get("result", [])
for update in result:
try:
message = update.get("message", update.get("edited_message"))
join_chat_request = update.get("join_chat_request")
message_reaction = update.get("join_chat_request")
if message:
await messages_routing(message, state)
elif join_chat_request:
await handle_join_request(join_chat_request)
elif message_reaction:
await handle_reaction_on_request(message_reaction)
except Exception as e:
logger.error(e)
import traceback
text = traceback.format_exc()
await telegram_api("sendMessage", chat_id=FEEDBACK_CHAT_ID, text=text)
except Exception as e:
logger.error(e)
import traceback
text = traceback.format_exc()
await telegram_api("sendMessage", chat_id=FEEDBACK_CHAT_ID, text=text)
offset = update["update_id"] + 1
offset = update["update_id"] + 1
await asyncio.sleep(1.0)
await asyncio.sleep(1.0)
else:
logger.error(' \n\n\n!!! getUpdates polling error\n\n\n')
await asyncio.sleep(30.0)
if __name__ == "__main__":