Files
aoc/skills/helper.py
T
2026-02-18 18:27:12 +01:00

59 lines
2.1 KiB
Python

import logging
import time
import asyncio
from nio import RoomMessageText, ReactionEvent
logging.basicConfig(format='[%(asctime)s] %(process)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s', level=logging.INFO)
class BotHelper:
"""a helper class for frequently need actions"""
__AUTHOR__ = 'anima'
__VERSION__ = '0.3.2'
def __init__(self, client):
self.client = client
self.starttime = int(time.time() * 1000)
def validate_message(self, event: RoomMessageText):
"""validate incomming messages befor bot start his work"""
if not isinstance(event, RoomMessageText):
logging.debug(f"message hast wrong type")
return False
if self.client.user == event.sender:
logging.debug(f"no reaction on messages from bot it self")
return False
if self.starttime > event.server_timestamp:
logging.debug(f"no reaction on messages befor bot start")
return False
return True
def validate_reaction(self, event: ReactionEvent):
"""validate incomming reactions befor bot start his work"""
if not isinstance(event, ReactionEvent):
print(event)
print(type(event))
logging.debug(f"reaction hast wrong type")
return False
if self.client.user == event.sender:
logging.debug(f"no reaction on reactions from bot it self")
return False
if self.starttime > event.server_timestamp:
logging.debug(f"no reaction on reactions befor bot start")
return False
return True
async def send_message(self, room: str, message: str):
"""send a basic text message to a room
Args:
room (str): room id without domain part (like !roomid)
message (str): simpl text message
"""
logging.debug(f'send message: {message}')
await self.client.room_send(
room,
message_type="m.room.message",
content={
"msgtype": "m.text",
"body": message
}
)