matrix_bridge_telegram/matrix/
command_handler.rs1use salvo::prelude::*;
2
3#[handler]
4pub async fn handle_command(req: &mut Request, res: &mut Response) {
5 let body: Option<serde_json::Value> = req.parse_json().await.ok();
6
7 if let Some(event) = body {
8 if let Some(content) = event.get("content") {
9 if let Some(body) = content.get("body").and_then(|b| b.as_str()) {
10 if body.starts_with("!tg ") {
11 let command = body.strip_prefix("!tg ").unwrap_or("");
12 let parts: Vec<&str> = command.split_whitespace().collect();
13
14 if parts.is_empty() {
15 res.render(Json(serde_json::json!({
16 "msgtype": "m.notice",
17 "body": "No command specified. Use !tg help for available commands."
18 })));
19 return;
20 }
21
22 let response = match parts[0] {
23 "ping" => handle_ping(),
24 "help" => handle_help(),
25 "bridge" => handle_bridge(&parts[1..]),
26 "unbridge" => handle_unbridge(&parts[1..]),
27 "login" => handle_login(),
28 "logout" => handle_logout(),
29 "whoami" => handle_whoami(),
30 _ => handle_unknown(parts[0]),
31 };
32
33 res.render(Json(response));
34 }
35 }
36 }
37 }
38}
39
40fn handle_ping() -> serde_json::Value {
41 serde_json::json!({
42 "msgtype": "m.notice",
43 "body": "Pong! Matrix-Telegram bridge is running."
44 })
45}
46
47fn handle_help() -> serde_json::Value {
48 serde_json::json!({
49 "msgtype": "m.notice",
50 "formatted_body": "<p><strong>Available commands:</strong></p>\n<ul>\n<li><code>!tg ping</code> - Check if bridge is running</li>\n<li><code>!tg help</code> - Show this help message</li>\n<li><code>!tg bridge <chat_id></code> - Bridge this room to a Telegram chat</li>\n<li><code>!tg unbridge</code> - Remove bridge from this room</li>\n<li><code>!tg login</code> - Log in to Telegram</li>\n<li><code>!tg logout</code> - Log out from Telegram</li>\n<li><code>!tg whoami</code> - Show your Telegram account info</li>\n</ul>",
51 "format": "org.matrix.custom.html",
52 "body": "Available commands:\n- !tg ping - Check if bridge is running\n- !tg help - Show this help message\n- !tg bridge <chat_id> - Bridge this room to a Telegram chat\n- !tg unbridge - Remove bridge from this room\n- !tg login - Log in to Telegram\n- !tg logout - Log out from Telegram\n- !tg whoami - Show your Telegram account info"
53 })
54}
55
56fn handle_bridge(args: &[&str]) -> serde_json::Value {
57 if args.is_empty() {
58 return serde_json::json!({
59 "msgtype": "m.notice",
60 "body": "Usage: !tg bridge <chat_id>"
61 });
62 }
63
64 let chat_id = args[0];
65
66 serde_json::json!({
67 "msgtype": "m.notice",
68 "body": format!("Bridging to Telegram chat {}...", chat_id)
69 })
70}
71
72fn handle_unbridge(_args: &[&str]) -> serde_json::Value {
73 serde_json::json!({
74 "msgtype": "m.notice",
75 "body": "Unbridging this room..."
76 })
77}
78
79fn handle_login() -> serde_json::Value {
80 serde_json::json!({
81 "msgtype": "m.notice",
82 "body": "To log in to Telegram, please visit the bridge web interface or use the login-qr command."
83 })
84}
85
86fn handle_logout() -> serde_json::Value {
87 serde_json::json!({
88 "msgtype": "m.notice",
89 "body": "Logging out from Telegram..."
90 })
91}
92
93fn handle_whoami() -> serde_json::Value {
94 serde_json::json!({
95 "msgtype": "m.notice",
96 "body": "You are not logged in to Telegram."
97 })
98}
99
100fn handle_unknown(cmd: &str) -> serde_json::Value {
101 serde_json::json!({
102 "msgtype": "m.notice",
103 "body": format!("Unknown command: {}. Use !tg help for available commands.", cmd)
104 })
105}
106
107pub struct CommandHandler;
108
109impl CommandHandler {
110 pub fn new() -> Self {
111 Self
112 }
113
114 pub async fn process(&self, _room_id: &str, _sender: &str, body: &str) -> Option<String> {
115 if body.starts_with("!tg ") {
116 let command = body.strip_prefix("!tg ").unwrap_or("");
117 let parts: Vec<&str> = command.split_whitespace().collect();
118
119 if !parts.is_empty() {
120 return Some(format!("Processing command: {}", parts[0]));
121 }
122 }
123 None
124 }
125}
126
127impl Default for CommandHandler {
128 fn default() -> Self {
129 Self::new()
130 }
131}