mx-sanebot: split out some request/response interface
This commit is contained in:
parent
5d0630cad4
commit
b282e5beb2
|
@ -1,4 +1,7 @@
|
||||||
|
mod msg_handler;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use matrix_sdk::{
|
use matrix_sdk::{
|
||||||
config::SyncSettings,
|
config::SyncSettings,
|
||||||
room::Room,
|
room::Room,
|
||||||
|
@ -10,26 +13,35 @@ use matrix_sdk::{
|
||||||
};
|
};
|
||||||
use tokio::time::{sleep, Duration};
|
use tokio::time::{sleep, Duration};
|
||||||
|
|
||||||
|
use msg_handler::MessageHandler;
|
||||||
|
|
||||||
async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
|
async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
|
||||||
println!("received event");
|
println!("received event");
|
||||||
if let Room::Joined(room) = room {
|
if let Room::Joined(room) = room {
|
||||||
let text_content = match event.content.msgtype {
|
let text_content = match event.content.msgtype {
|
||||||
MessageType::Text(t) => t,
|
MessageType::Text(t) => t,
|
||||||
_ => return,
|
_ => return, // not of interest
|
||||||
};
|
};
|
||||||
|
|
||||||
if text_content.body.contains("!ping") {
|
let sender = event.sender;
|
||||||
let content = RoomMessageEventContent::text_plain("pong");
|
let msg = text_content.body;
|
||||||
|
println!("message from {sender}: {msg}\n");
|
||||||
|
|
||||||
println!("sending");
|
if sender.as_str() == "@sanebot:uninsane.org" {
|
||||||
|
return; // don't respond to myself!
|
||||||
// send our message to the room we found the "!ping" command in
|
|
||||||
// the last parameter is an optional transaction id which we don't
|
|
||||||
// care about.
|
|
||||||
room.send(content, None).await.unwrap();
|
|
||||||
|
|
||||||
println!("message sent");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let resp = MessageHandler.on_msg(&msg);
|
||||||
|
println!("response: {}", resp);
|
||||||
|
|
||||||
|
let resp_content = RoomMessageEventContent::text_plain(&resp);
|
||||||
|
|
||||||
|
// send our message to the room we found the "!ping" command in
|
||||||
|
// the last parameter is an optional transaction id which we don't
|
||||||
|
// care about.
|
||||||
|
room.send(resp_content, None).await.unwrap();
|
||||||
|
|
||||||
|
println!("response sent");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
48
pkgs/mx-sanebot/src/msg_handler.rs
Normal file
48
pkgs/mx-sanebot/src/msg_handler.rs
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
pub struct MessageHandler;
|
||||||
|
|
||||||
|
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 {
|
||||||
|
let req = self.parse_msg(msg).unwrap_or(Request::Help);
|
||||||
|
let resp = req.evaluate();
|
||||||
|
resp.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_msg(&self, msg: &str) -> Result<Request, ()> {
|
||||||
|
let msg = msg.trim();
|
||||||
|
if msg == "!help" {
|
||||||
|
Ok(Request::Help)
|
||||||
|
} else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
enum Request {
|
||||||
|
Help,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Request {
|
||||||
|
fn evaluate(self) -> Response {
|
||||||
|
match self {
|
||||||
|
Request::Help => Response::About,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Response {
|
||||||
|
About,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Response {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "commands:\n")?;
|
||||||
|
write!(f, " !help => show this message\n")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user