2019-10-04 15:44:19 +02:00
|
|
|
import logging
|
|
|
|
|
2021-01-10 04:30:07 +01:00
|
|
|
from nio import AsyncClient, MatrixRoom, RoomMessageText
|
|
|
|
|
2020-08-10 00:02:07 +02:00
|
|
|
from my_project_name.chat_functions import send_text_to_room
|
2021-01-10 04:30:07 +01:00
|
|
|
from my_project_name.config import Config
|
|
|
|
from my_project_name.storage import Storage
|
2020-08-10 00:02:07 +02:00
|
|
|
|
2019-10-04 15:44:19 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-01-10 04:30:07 +01:00
|
|
|
class Message:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
client: AsyncClient,
|
|
|
|
store: Storage,
|
|
|
|
config: Config,
|
|
|
|
message_content: str,
|
|
|
|
room: MatrixRoom,
|
|
|
|
event: RoomMessageText,
|
|
|
|
):
|
2019-10-04 15:44:19 +02:00
|
|
|
"""Initialize a new Message
|
|
|
|
|
|
|
|
Args:
|
2021-01-10 04:33:59 +01:00
|
|
|
client: nio client used to interact with matrix.
|
2019-10-04 15:44:19 +02:00
|
|
|
|
2021-01-10 04:33:59 +01:00
|
|
|
store: Bot storage.
|
2019-10-04 15:44:19 +02:00
|
|
|
|
2021-01-10 04:33:59 +01:00
|
|
|
config: Bot configuration parameters.
|
2019-10-04 15:44:19 +02:00
|
|
|
|
2021-01-10 04:33:59 +01:00
|
|
|
message_content: The body of the message.
|
2019-10-04 15:44:19 +02:00
|
|
|
|
2021-01-10 04:33:59 +01:00
|
|
|
room: The room the event came from.
|
2019-10-04 15:44:19 +02:00
|
|
|
|
2021-01-10 04:33:59 +01:00
|
|
|
event: The event defining the message.
|
2019-10-04 15:44:19 +02:00
|
|
|
"""
|
|
|
|
self.client = client
|
|
|
|
self.store = store
|
|
|
|
self.config = config
|
|
|
|
self.message_content = message_content
|
|
|
|
self.room = room
|
|
|
|
self.event = event
|
|
|
|
|
2021-01-10 04:30:07 +01:00
|
|
|
async def process(self) -> None:
|
2019-10-04 15:44:19 +02:00
|
|
|
"""Process and possibly respond to the message"""
|
2019-10-26 01:28:29 +02:00
|
|
|
if self.message_content.lower() == "hello world":
|
2019-11-08 00:31:18 +01:00
|
|
|
await self._hello_world()
|
2019-10-26 01:28:29 +02:00
|
|
|
|
2021-01-10 04:30:07 +01:00
|
|
|
async def _hello_world(self) -> None:
|
2019-10-26 01:28:29 +02:00
|
|
|
"""Say hello"""
|
|
|
|
text = "Hello, world!"
|
|
|
|
await send_text_to_room(self.client, self.room.room_id, text)
|