diff --git a/src/bot.rs b/src/bot.rs index 6bd80d3..7a158c9 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,4 +1,4 @@ -use crate::matrix::SyncCommand; +use crate::matrix::{SyncCommand, Event, RoomEvent, MessageContent}; use reqwest::{Client, Url}; use serde::{self, Deserialize, Serialize}; use std::fs; @@ -116,7 +116,27 @@ impl DiceBot { .text() .await?; let sync: SyncCommand = serde_json::from_str(&body).unwrap(); - println!("{:#?}", sync); + // First join invited rooms + for room in sync.rooms.invite.keys() { + let mut join_url = self.url(&format!("/_matrix/client/r0/rooms/{}/join", room), &[]); + self.client.post(join_url) + .header("user-agent", USER_AGENT) + .send() + .await?; + } + + for (room_id, room) in sync.rooms.join.iter() { + for event in &room.timeline.events { + if let Event::Room(RoomEvent{ + content: MessageContent::Text(message), + .. + }) = event { + // TODO: create command parser (maybe command.rs) to parse !roll/!r commands + // and reply + println!("Body: {}", message.body()); + } + } + } self.next_batch = Some(sync.next_batch); Ok(()) } diff --git a/src/matrix.rs b/src/matrix.rs index 4d5c8ba..637003f 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -8,6 +8,12 @@ pub struct TextMessage { body: String, } +impl TextMessage { + pub fn body(&self) -> &str { + &self.body + } +} + // Need untagged because redactions are blank #[derive(Serialize, Deserialize, Debug)] #[serde(untagged)] @@ -16,39 +22,17 @@ pub enum MessageContent { Other(serde_json::Value), } -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "membership")] -pub enum MemberContent { - #[serde(rename = "invite")] - Invite { - // TODO: maybe leave empty? - #[serde(default)] - #[serde(alias = "displayname")] - display_name: Option, - }, - - #[serde(other)] - Other, -} - #[derive(Serialize, Deserialize, Debug)] pub struct RoomEvent { pub content: MessageContent, pub event_id: String, } -#[derive(Serialize, Deserialize, Debug)] -pub struct MemberEvent { - pub content: MemberContent, -} - #[derive(Serialize, Deserialize, Debug)] #[serde(tag = "type")] pub enum Event { #[serde(rename = "m.room.message")] Room(RoomEvent), - #[serde(rename = "m.room.member")] - Member(MemberEvent), #[serde(other)] Other,