""" This example implements Matrix bot that works with reactions. It uses environment variables to specify authorization data. Use Ctrl+C to stop the bot. """ import asyncio import os import logging from mab import * from _environment import check_environment async def on_specific_reaction(ctx: EventContext, vars: ContextVars) -> None: """This callback is called when a reaction is added.""" sender = vars[CTX_REACTION].sender reaction = vars[CTX_REACTION].reaction response = f"(Specific reaction) {sender}: {reaction}" await ctx.bot.send.text( ctx.room, response ) async def on_reaction(ctx: EventContext, vars: ContextVars) -> None: """This callback is called when a reaction is added.""" sender = vars[CTX_REACTION].sender reaction = vars[CTX_REACTION].reaction response = f"(Generic reaction) {sender}: {reaction}" await ctx.bot.send.text( ctx.room, response ) 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() & MessageReactionFilter(reaction=["❤️", "👍"]), on_specific_reaction) bot.add_callback( ~SenderIsBotFilter() & MessageReactionFilter(), on_reaction) # run until Ctrl+C try: await bot.run() except asyncio.CancelledError: pass if __name__ == "__main__": asyncio.run(main())