Initial commit
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
from .helper import BotHelper
|
||||
from .skill import Skill
|
||||
@@ -0,0 +1,41 @@
|
||||
import logging
|
||||
import time
|
||||
import asyncio
|
||||
from nio import RoomMessageText
|
||||
|
||||
logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s', level=logging.INFO)
|
||||
|
||||
class BotHelper:
|
||||
"""a helper class for frequently need actions"""
|
||||
__AUTHOR__ = 'anima'
|
||||
__VERSION__ = '0.0.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 self.client.user == event.sender:
|
||||
logging.debug(f"no reaction on messgaes from bot it self")
|
||||
return False
|
||||
if self.starttime > event.server_timestamp:
|
||||
logging.debug(f"no reaction on messgaes 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
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,39 @@
|
||||
import logging
|
||||
from .helper import BotHelper
|
||||
|
||||
logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s', level=logging.INFO)
|
||||
|
||||
class Skill:
|
||||
"""abstract class for bot skills"""
|
||||
__AUTHOR__ = 'anima'
|
||||
__VERSION__ = '0.1.0'
|
||||
__NAME__ = 'Example Skill'
|
||||
|
||||
registry: dict[str, Skill] = {}
|
||||
def __init_subclass__(cls, **kwargs):
|
||||
"""add loaded subclasses to regestry"""
|
||||
super().__init_subclass__(**kwargs)
|
||||
if cls.__name__ != "Skill" and issubclass(cls, Skill):
|
||||
logging.info(f"add skill {cls.__name__} to regestry")
|
||||
Skill.registry[cls.__name__] = cls
|
||||
|
||||
def __init__(self, client: type, config: dict = None):
|
||||
"""minimal init for a skill class
|
||||
|
||||
Args:
|
||||
client (AsyncClient): bot client handler
|
||||
config (dict, optional): additional configs from config file. Defaults to None.
|
||||
"""
|
||||
self.client = client
|
||||
self.config = config
|
||||
self.helper = BotHelper(self.client)
|
||||
|
||||
def load(self):
|
||||
"""if skill activate it will run this class"""
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
skill = Skill(None)
|
||||
print(f"Load {skill.__NAME__} version {skill.__VERSION__}")
|
||||
skill.load()
|
||||
Reference in New Issue
Block a user