mx-sanebot: format the help message in html
This commit is contained in:
parent
25d2234c69
commit
10a100c961
|
@ -49,7 +49,7 @@ enum Event {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Action {
|
enum Action {
|
||||||
AcceptInvite(OwnedRoomId),
|
AcceptInvite(OwnedRoomId),
|
||||||
SendMessage(OwnedRoomId, String),
|
SendMessage(OwnedRoomId, String /* text */, Option<String> /* html */),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Runner {
|
impl Runner {
|
||||||
|
@ -184,7 +184,7 @@ impl Runner {
|
||||||
Event::Invitation(room_id) => Action::AcceptInvite(room_id),
|
Event::Invitation(room_id) => Action::AcceptInvite(room_id),
|
||||||
Event::Message(room_id, _sender_id, body) => {
|
Event::Message(room_id, _sender_id, body) => {
|
||||||
let resp = MessageHandler.on_msg(&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());
|
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 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();
|
room.send(resp_content, None).await.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,10 +151,9 @@ impl MessageHandler {
|
||||||
/// parse any message directed to me, and return text to present to the user who messaged me.
|
/// 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.
|
/// 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.
|
/// 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 req = self.parse_msg(msg).unwrap_or(Request::Help);
|
||||||
let resp = req.evaluate();
|
req.evaluate()
|
||||||
resp.to_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_msg(&self, msg: &str) -> Result<Request, ()> {
|
fn parse_msg(&self, msg: &str) -> Result<Request, ()> {
|
||||||
|
@ -214,12 +213,31 @@ impl Request {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Response {
|
pub enum Response {
|
||||||
Help,
|
Help,
|
||||||
Bt(String),
|
Bt(String),
|
||||||
BtSearch(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 <phrase></code> => search for torrents</li>
|
||||||
|
</ul>
|
||||||
|
"#.to_owned()
|
||||||
|
),
|
||||||
|
// not yet implemented
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Response {
|
impl fmt::Display for Response {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
|
@ -235,3 +253,4 @@ impl fmt::Display for Response {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user