Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hello_world/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion hello_world/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{env, process::exit};

use ruma::{
OwnedRoomAliasId, TransactionId,
api::client::{alias::get_alias, membership::join_room_by_id, message::send_message_event},
events::room::message::RoomMessageEventContent,
OwnedRoomAliasId, TransactionId,
};

type HttpClient = ruma::client::http_client::HyperNativeTls;
Expand Down
2 changes: 1 addition & 1 deletion joke_bot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "joke_bot"
version = "0.1.0"
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
45 changes: 19 additions & 26 deletions joke_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ use futures_util::future::{join, join_all};
use http_body_util::BodyExt as _;
use hyper_util::rt::TokioExecutor;
use ruma::{
OwnedRoomId, OwnedUserId, TransactionId, UserId,
api::client::{
filter::FilterDefinition, membership::join_room_by_id, message::send_message_event,
sync::sync_events,
},
assign, client,
events::{
room::message::{MessageType, RoomMessageEventContent},
AnySyncMessageLikeEvent, AnySyncTimelineEvent, SyncMessageLikeEvent,
room::message::{MessageType, RoomMessageEventContent},
},
presence::PresenceState,
serde::Raw,
OwnedRoomId, OwnedUserId, TransactionId, UserId,
};
use serde_json::Value as JsonValue;
use tokio::fs;
Expand Down Expand Up @@ -161,37 +161,30 @@ async fn create_matrix_session(
async fn handle_message(
http_client: &HttpClient,
matrix_client: &MatrixClient,
e: &Raw<AnySyncTimelineEvent>,
ev: &Raw<AnySyncTimelineEvent>,
room_id: OwnedRoomId,
bot_user_id: &UserId,
) -> Result<(), Box<dyn Error>> {
if let Ok(AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(
SyncMessageLikeEvent::Original(m),
))) = e.deserialize()
))) = ev.deserialize()
// workaround because Conduit does not implement filtering
&& m.sender != bot_user_id
&& let MessageType::Text(t) = m.content.msgtype
{
// workaround because Conduit does not implement filtering.
if m.sender == bot_user_id {
return Ok(());
}

if let MessageType::Text(t) = m.content.msgtype {
println!("{}:\t{}", m.sender, t.body);
if t.body.to_ascii_lowercase().contains("joke") {
let joke = match get_joke(http_client).await {
Ok(joke) => joke,
Err(_) => "I thought of a joke... but I just forgot it.".to_owned(),
};
let joke_content = RoomMessageEventContent::text_plain(joke);
println!("{}:\t{}", m.sender, t.body);
if t.body.to_ascii_lowercase().contains("joke") {
let joke = match get_joke(http_client).await {
Ok(joke) => joke,
Err(_) => "I thought of a joke... but I just forgot it.".to_owned(),
};
Comment on lines +177 to +180
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use .unwrap_or_else() here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Send a follow-up PR :)

let joke_content = RoomMessageEventContent::text_plain(joke);

let txn_id = TransactionId::new();
let req = send_message_event::v3::Request::new(
room_id.to_owned(),
txn_id,
&joke_content,
)?;
// Do nothing if we can't send the message.
let _ = matrix_client.send_request(req).await;
}
let txn_id = TransactionId::new();
let req =
send_message_event::v3::Request::new(room_id.to_owned(), txn_id, &joke_content)?;
// Do nothing if we can't send the message.
let _ = matrix_client.send_request(req).await;
}
}

Expand Down
2 changes: 1 addition & 1 deletion message_log/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "message_log"
version = "0.1.0"
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
25 changes: 9 additions & 16 deletions message_log/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use ruma::{
api::client::{filter::FilterDefinition, sync::sync_events},
assign,
events::{
room::message::{MessageType, RoomMessageEventContent, TextMessageEventContent},
AnySyncMessageLikeEvent, AnySyncTimelineEvent, OriginalSyncMessageLikeEvent,
SyncMessageLikeEvent,
AnySyncMessageLikeEvent, AnySyncTimelineEvent, SyncMessageLikeEvent,
room::message::MessageType,
},
presence::PresenceState,
};
Expand Down Expand Up @@ -51,21 +50,15 @@ async fn log_messages(
{
// Filter out the text messages
if let AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(
SyncMessageLikeEvent::Original(OriginalSyncMessageLikeEvent {
content:
RoomMessageEventContent {
msgtype:
MessageType::Text(TextMessageEventContent {
body: msg_body, ..
}),
..
},
sender,
..
}),
SyncMessageLikeEvent::Original(m),
)) = event
&& let MessageType::Text(t) = m.content.msgtype
{
println!("{sender} in {room_id}: {msg_body}");
println!(
"{sender} in {room_id}: {msg_body}",
sender = m.sender,
msg_body = t.body
);
}
}
}
Expand Down
Loading