start work on a Matrix bot to expose sane-* commands to Matrix
This commit is contained in:
parent
1f2c9a9a5e
commit
5c8cca6a52
2867
pkgs/mx-sanebot/Cargo.lock
generated
Normal file
2867
pkgs/mx-sanebot/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
11
pkgs/mx-sanebot/Cargo.toml
Normal file
11
pkgs/mx-sanebot/Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "mx-sanebot"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
matrix-sdk = "0.6.2"
|
||||
tokio = { version = "1.20.1", features = ["macros", "rt-multi-thread"] }
|
60
pkgs/mx-sanebot/flake.lock
Normal file
60
pkgs/mx-sanebot/flake.lock
Normal file
|
@ -0,0 +1,60 @@
|
|||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1681202837,
|
||||
"narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "cfacdce06f30d2b68473a46042957675eebb3401",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1682173319,
|
||||
"narHash": "sha256-tPhOpJJ+wrWIusvGgIB2+x6ILfDkEgQMX0BTtM5vd/4=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "ee7ec1c71adc47d2e3c2d5eb0d6b8fbbd42a8d1c",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
"ref": "nixos-22.11",
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
},
|
||||
"systems": {
|
||||
"locked": {
|
||||
"lastModified": 1681028828,
|
||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
45
pkgs/mx-sanebot/flake.nix
Normal file
45
pkgs/mx-sanebot/flake.nix
Normal file
|
@ -0,0 +1,45 @@
|
|||
{
|
||||
description = "Sane matrix chatbot";
|
||||
inputs = {
|
||||
nixpkgs.url = "nixpkgs/nixos-22.11";
|
||||
flake-utils.url = github:numtide/flake-utils;
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, flake-utils }:
|
||||
with flake-utils.lib; eachSystem allSystems (system:
|
||||
let
|
||||
pkgs = import nixpkgs {
|
||||
inherit system;
|
||||
};
|
||||
nativeBuildInputs = with pkgs; [
|
||||
pkg-config
|
||||
];
|
||||
buildInputs = with pkgs; [
|
||||
openssl
|
||||
];
|
||||
in rec {
|
||||
packages = {
|
||||
# docs: <nixpkgs>/doc/languages-frameworks/rust.section.md
|
||||
mx-sanebot = pkgs.rustPlatform.buildRustPackage {
|
||||
name = "mx-sanebot";
|
||||
src = ./.;
|
||||
cargoLock.lockFile = ./Cargo.lock;
|
||||
# enables debug builds, if we want: https://github.com/NixOS/nixpkgs/issues/60919.
|
||||
hardeningDisable = [ "fortify" ];
|
||||
inherit buildInputs nativeBuildInputs;
|
||||
};
|
||||
};
|
||||
defaultPackage = packages.mx-sanebot;
|
||||
|
||||
devShells.default = with pkgs; mkShell {
|
||||
# enables debug builds, if we want: https://github.com/NixOS/nixpkgs/issues/60919.
|
||||
hardeningDisable = [ "fortify" ];
|
||||
|
||||
# Allow cargo to download crates.
|
||||
SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt";
|
||||
|
||||
inherit buildInputs;
|
||||
nativeBuildInputs = [ cargo ] ++ nativeBuildInputs;
|
||||
};
|
||||
});
|
||||
}
|
127
pkgs/mx-sanebot/src/main.rs
Normal file
127
pkgs/mx-sanebot/src/main.rs
Normal file
|
@ -0,0 +1,127 @@
|
|||
use std::env;
|
||||
use matrix_sdk::{
|
||||
config::SyncSettings,
|
||||
room::Room,
|
||||
ruma::events::room::member::StrippedRoomMemberEvent,
|
||||
ruma::events::room::message::{
|
||||
MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent,
|
||||
},
|
||||
Client,
|
||||
};
|
||||
use tokio::time::{sleep, Duration};
|
||||
|
||||
async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
|
||||
println!("received event");
|
||||
if let Room::Joined(room) = room {
|
||||
let text_content = match event.content.msgtype {
|
||||
MessageType::Text(t) => t,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
if text_content.body.contains("!ping") {
|
||||
let content = RoomMessageEventContent::text_plain("pong");
|
||||
|
||||
println!("sending");
|
||||
|
||||
// send our message to the room we found the "!ping" command in
|
||||
// the last parameter is an optional transaction id which we don't
|
||||
// care about.
|
||||
room.send(content, None).await.unwrap();
|
||||
|
||||
println!("message sent");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Whenever we see a new stripped room member event, we've asked our client to
|
||||
// call this function. So what exactly are we doing then?
|
||||
async fn on_stripped_state_member(
|
||||
room_member: StrippedRoomMemberEvent,
|
||||
client: Client,
|
||||
room: Room,
|
||||
) {
|
||||
if room_member.state_key != client.user_id().unwrap() {
|
||||
// the invite we've seen isn't for us, but for someone else. ignore
|
||||
return;
|
||||
}
|
||||
|
||||
// looks like the room is an invited room, let's attempt to join then
|
||||
if let Room::Invited(room) = room {
|
||||
// The event handlers are called before the next sync begins, but
|
||||
// methods that change the state of a room (joining, leaving a room)
|
||||
// wait for the sync to return the new room state so we need to spawn
|
||||
// a new task for them.
|
||||
tokio::spawn(async move {
|
||||
println!("Autojoining room {}", room.room_id());
|
||||
let mut delay = 2;
|
||||
|
||||
while let Err(err) = room.accept_invitation().await {
|
||||
// retry autojoin due to synapse sending invites, before the
|
||||
// invited user can join for more information see
|
||||
// https://github.com/matrix-org/synapse/issues/4345
|
||||
eprintln!("Failed to join room {} ({err:?}), retrying in {delay}s", room.room_id());
|
||||
|
||||
sleep(Duration::from_secs(delay)).await;
|
||||
delay *= 2;
|
||||
|
||||
if delay > 3600 {
|
||||
eprintln!("Can't join room {} ({err:?})", room.room_id());
|
||||
break;
|
||||
}
|
||||
}
|
||||
println!("Successfully joined room {}", room.room_id());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn login_and_sync(
|
||||
homeserver_url: &str,
|
||||
username: &str,
|
||||
password: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
// TODO: look into caching the messages somewhere on disk (sled; indexeddb)
|
||||
let client = Client::builder()
|
||||
.homeserver_url(homeserver_url)
|
||||
.sled_store("/home/colin/mx-sanebot", None)?
|
||||
.build()
|
||||
.await?;
|
||||
println!("client built");
|
||||
client.login_username(&username, &password).initial_device_display_name("sanebot")
|
||||
.initial_device_display_name("sanebot")
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
println!("logged in as {username}");
|
||||
|
||||
// Now, we want our client to react to invites. Invites sent us stripped member
|
||||
// state events so we want to react to them. We add the event handler before
|
||||
// the sync, so this happens also for older messages. All rooms we've
|
||||
// already entered won't have stripped states anymore and thus won't fire
|
||||
client.add_event_handler(on_stripped_state_member);
|
||||
|
||||
// An initial sync to set up state and so our bot doesn't respond to old
|
||||
// messages. If the `StateStore` finds saved state in the location given the
|
||||
// initial sync will be skipped in favor of loading state from the store
|
||||
let response = client.sync_once(SyncSettings::default()).await.unwrap();
|
||||
println!("sync'd");
|
||||
// add our CommandBot to be notified of incoming messages, we do this after the
|
||||
// initial sync to avoid responding to messages before the bot was running.
|
||||
client.add_event_handler(on_room_message);
|
||||
|
||||
// since we called `sync_once` before we entered our sync loop we must pass
|
||||
// that sync token to `sync`
|
||||
let settings = SyncSettings::default().token(response.next_batch);
|
||||
// this keeps state from the server streaming in to CommandBot via the
|
||||
// EventHandler trait
|
||||
client.sync(settings).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let password = env::var("SANEBOT_PASSWORD").unwrap_or("password".into());
|
||||
let result = login_and_sync("https://uninsane.org", "sanebot", &*password).await;
|
||||
println!("done");
|
||||
result
|
||||
}
|
Loading…
Reference in New Issue
Block a user