- Improved README.md - Added examples - Added `session_storage/` to .gitignore so that examples do not introduce leftover files after execution
51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
"""
|
|
This example implements Matrix bot that echoes all text messages it receives.
|
|
|
|
It uses environment variables to specify authorization data. Use Ctrl+C to stop
|
|
the bot.
|
|
"""
|
|
|
|
import asyncio
|
|
import os
|
|
import logging
|
|
|
|
from mab import (
|
|
MatrixBot,
|
|
MatrixBotConfig,
|
|
RoomEventData,
|
|
BodyExistsFilter,
|
|
MessageTypeFilter,
|
|
SenderIsBotFilter,
|
|
MessageType
|
|
)
|
|
|
|
from _environment import check_environment
|
|
|
|
async def on_text_message(data: RoomEventData) -> None:
|
|
"""This callback is called when a text message arrives."""
|
|
await data.bot.send_text(data.room, data.event.body) # type: ignore
|
|
|
|
async def main() -> None:
|
|
"""Application entry point"""
|
|
logging.basicConfig(level=logging.INFO)
|
|
logging.getLogger("nio").setLevel(logging.CRITICAL+1)
|
|
check_environment()
|
|
|
|
config = MatrixBotConfig(
|
|
matrix_homeserver_url=os.environ["MATRIX_HOMESERVER"],
|
|
matrix_username_localpart=os.environ["MATRIX_USERNAME"],
|
|
storage_directory="session_storage"
|
|
)
|
|
bot = MatrixBot(config)
|
|
bot.add_callback(
|
|
~SenderIsBotFilter() & BodyExistsFilter() & MessageTypeFilter(MessageType.TEXT),
|
|
on_text_message)
|
|
|
|
# run until Ctrl+C
|
|
try:
|
|
await bot.run()
|
|
except asyncio.CancelledError:
|
|
pass
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main()) |