Skip to content

Commit

Permalink
feat: recognize DMs and Rooms and behave appropriately
Browse files Browse the repository at this point in the history
Partial implementation of #6
  • Loading branch information
arcuru committed Jul 19, 2024
1 parent e9e0d16 commit 8c0931c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ There is a public Matrix room available at [#chaz:jackson.dev](https://matrix.to

Chaz will automatically accept Room invites for any user in the `allow_list`.

When it's in a room, it will watch for commands that are prefixed by `!chaz`, and any other messages will be sent to the configured backend.
When it's in a room, it will watch for commands that are prefixed by `!chaz`.
If it's a DM, it will respond to every message that it doesn't recognize as a command.
If it's in a larger room, it will only respond to messages that are sent to it using `!chaz`.

So in a larger room, send just `!chaz` and it will be sent all the recent messages in the room and asked for a response.
You can also send a request along with that, e.g. `!chaz explain that to me`, and it will receive your message and the context of the room and respond.

The commands that it recognizes are:

Expand Down
36 changes: 35 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,14 @@ async fn main() -> anyhow::Result<()> {

// The text handler is called for every non-command message
// It is also called if _only_ `!chaz` is sent. That sounds like a feature to me.
bot.register_text_handler(|sender, _, room| async move {
bot.register_text_handler(|sender, body: String, room| async move {
// If this room is not marked as a direct message, ignore messages
// Direct message detection/conversion may be buggy? Recognize a direct message by either the room setting _or_ number of members
let is_direct = room.is_direct().await.unwrap_or(false) || room.joined_members_count() < 3;
if !is_direct && !body.as_str().starts_with("!chaz") {
return Ok(());
}

if rate_limit(&room, &sender).await {
return Ok(());
}
Expand Down Expand Up @@ -544,6 +551,33 @@ async fn get_context(room: &Room) -> Result<(String, Option<String>, Vec<MediaFi
if text_content.body.starts_with("!chaz clear") {
break 'outer;
}
// if it's not a recognized command, remove the "!chaz" and add that to messages
if text_content.body.starts_with("!chaz") {
let command = text_content.body.trim_start_matches("!chaz").trim();
if command.is_empty() {
continue;
}
if let Some(command) = command.split_whitespace().next() {
// Recognized command, so skip adding it
if [
"help", "party", "send", "list", "rename", "print",
"model", "clear",
]
.contains(&command.to_lowercase().as_str())
{
continue;
}
}
if room
.client()
.user_id()
.is_some_and(|uid| sender == uid.as_str())
{
messages.push(format!("ASSISTANT: {}\n", command));
} else {
messages.push(format!("USER: {}\n", command));
}
}
} else {
// Push the sender and message to the front of the string
if room
Expand Down

0 comments on commit 8c0931c

Please sign in to comment.