add mentions support

This commit is contained in:
2026-05-12 14:01:32 -05:00
parent a9b1115269
commit da84226a45
+26 -10
View File
@@ -1,10 +1,14 @@
use matrix_sdk::{
Room,
event_handler::Ctx,
ruma::events::{
reaction::ReactionEventContent,
relation::Annotation,
room::message::{OriginalSyncRoomMessageEvent, RoomMessageEventContent},
ruma::{
UserId,
events::{
Mentions,
reaction::ReactionEventContent,
relation::Annotation,
room::message::{OriginalSyncRoomMessageEvent, RoomMessageEventContent},
},
},
};
@@ -26,14 +30,17 @@ pub async fn start(
) -> anyhow::Result<()> {
tracing::debug!("Executing !start command");
let user = event.sender.to_string();
let ids = vec![UserId::parse(user.clone()).unwrap()];
tracing::debug!("Command invoked by: {}", user);
// set to listening
match ctx.0.status {
MagbotStatus::Running => {
tracing::info!("Bot status already Running, not changing state");
let mentions = Mentions::with_user_ids(ids);
let msg = RoomMessageEventContent::text_plain(format!(
"{user} Magbot is already listening for titles!"
));
))
.add_mentions(mentions);
room.send(msg).await?;
}
MagbotStatus::Sleeping => {
@@ -56,6 +63,7 @@ pub async fn stop(
) -> anyhow::Result<()> {
tracing::debug!("Executing !stop command");
let user = event.sender.to_string();
let ids = vec![UserId::parse(user.clone()).unwrap()];
tracing::debug!("Command invoked by: {}", user);
tracing::debug!("Clearing {} saved titles", ctx.0.titles.len());
@@ -66,9 +74,11 @@ pub async fn stop(
ctx.0.status = MagbotStatus::Sleeping;
// send message that magbot is no longer listening
let mentions = Mentions::with_user_ids(ids);
let msg = RoomMessageEventContent::text_plain(format!(
"{user} Magbot is no longer listening for titles!"
));
))
.add_mentions(mentions);
room.send(msg).await?;
Ok(())
@@ -82,13 +92,16 @@ pub async fn help(
) -> anyhow::Result<()> {
tracing::debug!("Executing !help command");
let user = event.sender.to_string();
let ids = vec![UserId::parse(user.clone()).unwrap()];
tracing::debug!("Command invoked by: {}", user);
tracing::debug!("Sending help message with current status: {}", ctx.0.status);
// send message with available commands
let mentions = Mentions::with_user_ids(ids);
let msg = RoomMessageEventContent::text_plain(format!(
"{user} available commands are:\n{HELP_MSG}\nBot current status: {}",
ctx.0.status
));
))
.add_mentions(mentions);
room.send(msg).await?;
Ok(())
}
@@ -150,17 +163,20 @@ pub async fn get_titles(
match ctx.0.status {
MagbotStatus::Running => {
for title in ctx.0.titles {
let id = vec![UserId::parse(&title.user).unwrap()];
let mentions = Mentions::with_user_ids(id);
let msg = RoomMessageEventContent::text_plain(format!(
"{} : {}",
title.user, title.title
));
))
.add_mentions(mentions);
let response = room.send(msg).await?;
let reaction = ReactionEventContent::new(Annotation::new(
response.event_id.clone(),
response.response.event_id.clone(),
"👍".to_string(),
));
room.send(reaction).await?;
ctx.0.message_ids.push(response.event_id);
ctx.0.message_ids.push(response.response.event_id);
}
}
MagbotStatus::Sleeping => {