Added ability to add reactions

This commit is contained in:
2026-11-13 16:11:42 +03:00
parent 4b4c434bd8
commit 05a286b57a
2 changed files with 33 additions and 3 deletions

View File

@@ -14,7 +14,7 @@ from mab import *
from _environment import check_environment from _environment import check_environment
async def on_specific_reaction(ctx: EventContext, vars: ContextVars) -> None: async def on_specific_reaction(ctx: EventContext, vars: ContextVars) -> None:
"""This callback is called when a reaction is added.""" """This callback is called when a specific reaction is added."""
sender = vars[CTX_REACTION].sender sender = vars[CTX_REACTION].sender
reaction = vars[CTX_REACTION].reaction reaction = vars[CTX_REACTION].reaction
response = f"(Specific reaction) {sender}: {reaction}" response = f"(Specific reaction) {sender}: {reaction}"
@@ -33,6 +33,10 @@ async def on_reaction(ctx: EventContext, vars: ContextVars) -> None:
response response
) )
async def on_message(ctx: EventContext, vars: ContextVars) -> None:
"""This callback is called when a message is received."""
await ctx.bot.send.reaction(ctx.room, vars[CTX_ROOM_EVENT].event_id, "👀")
async def main() -> None: async def main() -> None:
"""Application entry point""" """Application entry point"""
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
@@ -51,6 +55,9 @@ async def main() -> None:
bot.add_callback( bot.add_callback(
~SenderIsBotFilter() & MessageReactionFilter(), ~SenderIsBotFilter() & MessageReactionFilter(),
on_reaction) on_reaction)
bot.add_callback(
~SenderIsBotFilter() & NewMessageFilter(),
on_message)
# run until Ctrl+C # run until Ctrl+C
try: try:

View File

@@ -100,7 +100,8 @@ class ClientSender:
async def content(self, async def content(self,
room: MatrixRoom | str, room: MatrixRoom | str,
content: dict) -> RoomSendResponse: content: dict,
message_type: str = "m.room.message") -> RoomSendResponse:
""" """
Send raw `content` JSON to `room`. Send raw `content` JSON to `room`.
@@ -117,7 +118,7 @@ class ClientSender:
try: try:
result = await self._client.room_send( result = await self._client.room_send(
room_id=room.room_id if isinstance(room, MatrixRoom) else room, room_id=room.room_id if isinstance(room, MatrixRoom) else room,
message_type="m.room.message", message_type=message_type,
content=content content=content
) )
except OlmUnverifiedDeviceError: except OlmUnverifiedDeviceError:
@@ -134,6 +135,28 @@ class ClientSender:
else: else:
raise RuntimeError("Unknown error has occured", result) raise RuntimeError("Unknown error has occured", result)
async def reaction(self, room: MatrixRoom | str, message_id: str, reaction: str) -> str:
"""
Add `reaction` to the message `message_id` in `room`.
Args:
- room - the room of `message_id`
- message_id - the message to add the `reaction` to
- reaction - emoji of reaction
Returns:
- `event_id` of added reaction on success
- Raises an exception on error
"""
content = {
"m.relates_to": {
"event_id": message_id,
"key": reaction,
"rel_type": "m.annotation"
}
}
return (await self.content(room, content, message_type="m.reaction")).event_id
async def text(self, async def text(self,
room: MatrixRoom | str, room: MatrixRoom | str,
text: str, text: str,