mx-sanebot: format the help message in html

This commit is contained in:
Colin 2023-04-29 09:16:51 +00:00
parent 25d2234c69
commit 10a100c961
2 changed files with 30 additions and 8 deletions

View File

@ -49,7 +49,7 @@ enum Event {
#[derive(Debug)]
enum Action {
AcceptInvite(OwnedRoomId),
SendMessage(OwnedRoomId, String),
SendMessage(OwnedRoomId, String /* text */, Option<String> /* html */),
}
impl Runner {
@ -184,7 +184,7 @@ impl Runner {
Event::Invitation(room_id) => Action::AcceptInvite(room_id),
Event::Message(room_id, _sender_id, body) => {
let resp = MessageHandler.on_msg(&body);
Action::SendMessage(room_id, resp)
Action::SendMessage(room_id, resp.to_string(), resp.html())
}
}
}
@ -221,10 +221,13 @@ impl Runner {
println!("Successfully joined room {}", room.room_id());
});
}
Action::SendMessage(room_id, msg) => {
Action::SendMessage(room_id, text, html) => {
let room = self.client.get_joined_room(&room_id).unwrap();
let resp_content = RoomMessageEventContent::text_plain(&msg);
let resp_content = match html {
None => RoomMessageEventContent::text_plain(&text),
Some(html) => RoomMessageEventContent::text_html(&text, &html),
};
room.send(resp_content, None).await.unwrap();
}
}

View File

@ -151,10 +151,9 @@ impl MessageHandler {
/// parse any message directed to me, and return text to present to the user who messaged me.
/// the message passed here may or may not be a "valid" request.
/// if invalid, expect an error message or help message, still meant for the user.
pub fn on_msg(&self, msg: &str) -> String {
pub fn on_msg(&self, msg: &str) -> Response {
let req = self.parse_msg(msg).unwrap_or(Request::Help);
let resp = req.evaluate();
resp.to_string()
req.evaluate()
}
fn parse_msg(&self, msg: &str) -> Result<Request, ()> {
@ -214,12 +213,31 @@ impl Request {
}
}
enum Response {
pub enum Response {
Help,
Bt(String),
BtSearch(String),
}
impl Response {
pub fn html(&self) -> Option<String> {
match self {
Response::Help => Some(
r#"
commands:
<ul>
<li><code>!help</code> => show this message</li>
<li><code>!bt</code> => show torrent statuses</li>
<li><code>!bt search &lt;phrase&gt;</code> => search for torrents</li>
</ul>
"#.to_owned()
),
// not yet implemented
_ => None,
}
}
}
impl fmt::Display for Response {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@ -235,3 +253,4 @@ impl fmt::Display for Response {
Ok(())
}
}