Skip to content

Reactions

Demonstrates event listeners and the Message.react() API. The bot watches for incoming messages and reactions, and responds with emoji reactions of its own.

from asyncio import Event
from nio import MatrixRoom, RoomMessageText, ReactionEvent
from matrix import Bot, Room, Message

bot = Bot()


@bot.event
async def on_message(room: Room, event: RoomMessageText) -> None:
    """
    This function listens for new messages in a room and reacts based
    on the message content.
    """
    message = await room.fetch_message(event.event_id)

    if message.body.lower().startswith("thanks"):
        await message.react("🙏")

    if message.body.lower().startswith("hello"):
        # Can also react with a text message instead of emoji
        await message.react("hi")

    if message.body.lower().startswith("❤️"):
        await message.react("❤️")


@bot.event
async def on_react(room: Room, event: ReactionEvent) -> None:
    """
    This function listens for new member reaction to messages in a room,
    and reacts based on the reaction emoji.
    """
    message = await room.fetch_message(event.reacts_to)

    if message.key == "🙏":
        await message.react("❤️")


bot.start(config="config.yaml")

Two event handlers are registered here:

  • on_message — fires on every RoomMessageText event. It fetches the full message object and reacts based on the message body.
  • on_react — fires on every ReactionEvent. It fetches the original message that was reacted to and chains a follow-up reaction.

room.fetch_message() returns a Message object. You can react with an emoji string or any short text.