Added ability to add reactions
This commit is contained in:
@@ -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:
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user