forked from projectmoon/tenebrous-dicebot
handle matrix message handling
This commit is contained in:
parent
087e20c261
commit
d0f3ec7ad2
24
src/bot.rs
24
src/bot.rs
|
@ -1,4 +1,4 @@
|
||||||
use crate::matrix::SyncCommand;
|
use crate::matrix::{SyncCommand, Event, RoomEvent, MessageContent};
|
||||||
use reqwest::{Client, Url};
|
use reqwest::{Client, Url};
|
||||||
use serde::{self, Deserialize, Serialize};
|
use serde::{self, Deserialize, Serialize};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
@ -116,7 +116,27 @@ impl DiceBot {
|
||||||
.text()
|
.text()
|
||||||
.await?;
|
.await?;
|
||||||
let sync: SyncCommand = serde_json::from_str(&body).unwrap();
|
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);
|
self.next_batch = Some(sync.next_batch);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,12 @@ pub struct TextMessage {
|
||||||
body: String,
|
body: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TextMessage {
|
||||||
|
pub fn body(&self) -> &str {
|
||||||
|
&self.body
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Need untagged because redactions are blank
|
// Need untagged because redactions are blank
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
|
@ -16,39 +22,17 @@ pub enum MessageContent {
|
||||||
Other(serde_json::Value),
|
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<String>,
|
|
||||||
},
|
|
||||||
|
|
||||||
#[serde(other)]
|
|
||||||
Other,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct RoomEvent {
|
pub struct RoomEvent {
|
||||||
pub content: MessageContent,
|
pub content: MessageContent,
|
||||||
pub event_id: String,
|
pub event_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct MemberEvent {
|
|
||||||
pub content: MemberContent,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
#[serde(rename = "m.room.message")]
|
#[serde(rename = "m.room.message")]
|
||||||
Room(RoomEvent),
|
Room(RoomEvent),
|
||||||
#[serde(rename = "m.room.member")]
|
|
||||||
Member(MemberEvent),
|
|
||||||
|
|
||||||
#[serde(other)]
|
#[serde(other)]
|
||||||
Other,
|
Other,
|
||||||
|
|
Loading…
Reference in New Issue