matrix-rust-sdk
matrix-rust-sdk copied to clipboard
How to tell if a user was addressed in a message
If I send a message in a room addressing a certain user, for instance:
@my-bot do some work
how can I detect in the code this message was actually for my-bot?
I dumped the whole SyncMessageEvent and I could not find any field to indicate that.
The only place I can see the address is in the formatted body but I don't feel confortable parsing that and looking for user names. Is there a standard way to figure out this information from the message?
SyncMessageEvent {
content: RoomMessageEventContent {
msgtype: Text(
TextMessageEventContent {
body: "user1: hello",
formatted: Some(
FormattedBody {
format: Html,
body: "<a href=\"https://matrix.to/#/@user1:matrix.org\">user2</a>: hello",
},
),
},
),
relates_to: Some(
_Custom,
),
},
event_id: "$4tWH8L7mzUA8_QdlJxavJXoqXjf5UplKWaEHaIpdoyM",
sender: "@user2:matrix.org",
origin_server_ts: MilliSecondsSinceUnixEpoch(
1648479360128,
),
unsigned: Unsigned {
age: None,
transaction_id: None,
},
}
Hi!
The part about mentions in the Matrix spec is here. So for a proper mention you actually need to check the formatted body for an <a>
tag with a Matrix URI.
If you use the SDK as a git dependency you can actually try to parse the link as a MatrixUri
or a MatrixToUri
and that should give you the user with the id()
method.
I think there is also the simpler convention of using !mybot {cmd}
. This forces you to have a rather unique name for the prefix to have no conflict with other bots, but then you just have to check the start of the body
.
I think that this has been answered.