From d813198cb01385a4359cb16134efaba1e835b327 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Tue, 9 Feb 2021 22:22:09 +0000 Subject: [PATCH] Reply with executed command as quote (single commands only). --- src/bot.rs | 28 +++++++++++++++++++++++----- src/bot/event_handlers.rs | 2 +- src/matrix.rs | 24 ++++++++++++++++++------ 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index d7c6b9b..d656052 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -8,7 +8,11 @@ use crate::state::DiceBotState; use dirs; use futures::stream::{self, StreamExt}; use log::info; -use matrix_sdk::{self, identifiers::RoomId, Client, ClientConfig, JoinedRoom, SyncSettings}; +use matrix_sdk::{ + self, + identifiers::{EventId, RoomId}, + Client, ClientConfig, JoinedRoom, SyncSettings, +}; use std::clone::Clone; use std::path::PathBuf; use std::sync::{Arc, RwLock}; @@ -59,9 +63,10 @@ async fn handle_single_result( cmd_result: &ExecutionResult, respond_to: &str, room_id: &RoomId, + event_id: EventId, ) { let html = cmd_result.message_html(respond_to); - matrix::send_message(client, room_id, &html).await; + matrix::send_message(client, room_id, &html, Some(event_id)).await; } /// Handle responding to multiple commands being executed. Will print @@ -98,7 +103,7 @@ async fn handle_multiple_results( .replace("\n", "
") }; - matrix::send_message(client, room_id, &message).await; + matrix::send_message(client, room_id, &message, None).await; } impl DiceBot { @@ -176,7 +181,13 @@ impl DiceBot { Ok(()) } - async fn execute_commands(&self, room: &JoinedRoom, sender_username: &str, msg_body: &str) { + async fn execute_commands( + &self, + room: &JoinedRoom, + sender_username: &str, + msg_body: &str, + event_id: EventId, + ) { let room_name: &str = &room.display_name().await.ok().unwrap_or_default(); let room_id = room.room_id().clone(); @@ -208,7 +219,14 @@ impl DiceBot { if results.len() >= 1 { if results.len() == 1 { - handle_single_result(&self.client, &results[0].1, sender_username, &room_id).await; + handle_single_result( + &self.client, + &results[0].1, + sender_username, + &room_id, + event_id, + ) + .await; } else if results.len() > 1 { handle_multiple_results(&self.client, &results, sender_username, &room_id).await; } diff --git a/src/bot/event_handlers.rs b/src/bot/event_handlers.rs index f6f323a..cbc895c 100644 --- a/src/bot/event_handlers.rs +++ b/src/bot/event_handlers.rs @@ -211,7 +211,7 @@ impl EventEmitter for DiceBot { return; }; - self.execute_commands(&room, &sender_username, &msg_body) + self.execute_commands(&room, &sender_username, &msg_body, event.event_id.clone()) .await; } } diff --git a/src/matrix.rs b/src/matrix.rs index 525466e..d0480ab 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1,5 +1,9 @@ use log::error; -use matrix_sdk::Error as MatrixError; +use matrix_sdk::{ + events::room::message::{InReplyTo, Relation}, + identifiers::EventId, + Error as MatrixError, +}; use matrix_sdk::{ events::{ room::message::{MessageEventContent::Notice, NoticeMessageEventContent}, @@ -38,12 +42,20 @@ pub async fn get_users_in_room(client: &Client, room_id: &RoomId) -> Vec } } -pub async fn send_message(client: &Client, room_id: &RoomId, message: &str) { +pub async fn send_message( + client: &Client, + room_id: &RoomId, + message: &str, + reply_to: Option, +) { let plain = html2text::from_read(message.as_bytes(), message.len()); - let response = RoomMessage(Notice(NoticeMessageEventContent::html( - plain.trim(), - message, - ))); + let mut content = NoticeMessageEventContent::html(plain.trim(), message); + + content.relates_to = reply_to.map(|event_id| Relation::Reply { + in_reply_to: InReplyTo { event_id }, + }); + + let response = RoomMessage(Notice(content)); let result = client.room_send(&room_id, response, None).await; if let Err(e) = result {