Initial commit

This commit is contained in:
Noa Himesaka
2026-01-29 01:27:31 +09:00
commit fe6ce755d1
11 changed files with 4469 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
MATRIX_HOMESERVER=https://matrix.org
MATRIX_USERNAME=your_username
MATRIX_PASSWORD=your_password
+45
View File
@@ -0,0 +1,45 @@
name: Docker Publish
on:
push:
branches: [ "main" ]
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: t2linux/matrix-bot
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=sha
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
+3
View File
@@ -0,0 +1,3 @@
/target
.env
docker-compose.yml
Generated
+4210
View File
File diff suppressed because it is too large Load Diff
+16
View File
@@ -0,0 +1,16 @@
[package]
name = "matrix-bot"
version = "0.1.0"
edition = "2024"
[dependencies]
matrix-sdk = "0.16.0"
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
anyhow = "1"
dotenv = "0.15"
tracing-subscriber = "0.3.22"
[dev-dependencies]
tempfile = "3.24.0"
+14
View File
@@ -0,0 +1,14 @@
FROM rust:1-slim-bookworm as builder
WORKDIR /usr/src/matrix-bot
RUN apt-get update && apt-get install -y pkg-config libssl-dev libsqlite3-dev && rm -rf /var/lib/apt/lists/*
COPY . .
RUN cargo install --path .
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y libssl-dev ca-certificates libsqlite3-0 && rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/cargo/bin/matrix-bot /usr/local/bin/matrix-bot
COPY links.json /usr/local/bin/links.json
WORKDIR /usr/local/bin
CMD ["matrix-bot"]
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 t2linux
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+10
View File
@@ -0,0 +1,10 @@
services:
matrix-bot:
image: ghcr.io/t2linux/matrix-bot:latest
environment:
- MATRIX_HOMESERVER=https://matrix.org
- MATRIX_USERNAME=your_username
- MATRIX_PASSWORD=your_password
volumes:
- ./links.json:/usr/local/bin/links.json
restart: unless-stopped
+33
View File
@@ -0,0 +1,33 @@
{
"wiki": "https://wiki.t2linux.org/",
"index": "https://wiki.t2linux.org/",
"roadmap": "https://wiki.t2linux.org/roadmap/",
"contribute": "https://wiki.t2linux.org/contribute/",
"preinstall": "https://wiki.t2linux.org/guides/preinstall/",
"postinstall": "https://wiki.t2linux.org/guides/postinstall/",
"modules": "https://wiki.t2linux.org/guides/postinstall/",
"drivers": "https://wiki.t2linux.org/guides/postinstall/",
"wifi": "https://wiki.t2linux.org/guides/wifi-bluetooth/",
"bluetooth": "https://wiki.t2linux.org/guides/wifi-bluetooth/",
"firmware": "https://wiki.t2linux.org/guides/wifi-bluetooth/",
"windows": "https://wiki.t2linux.org/guides/windows/",
"audio": "https://wiki.t2linux.org/guides/audio-config/",
"hybrid graphics": "https://wiki.t2linux.org/guides/hybrid-graphics/",
"igpu": "https://wiki.t2linux.org/guides/hybrid-graphics/",
"fan": "https://wiki.t2linux.org/guides/fan/",
"arch": "https://wiki.t2linux.org/distributions/arch/installation/",
"eos": "https://wiki.t2linux.org/distributions/endeavouros/installation/",
"fedora": "https://wiki.t2linux.org/distributions/fedora/installation/",
"manjaro": "https://wiki.t2linux.org/distributions/manjaro/installation/",
"gentoo": "https://wiki.t2linux.org/distributions/gentoo/installation/",
"nixos": "https://wiki.t2linux.org/distributions/nixos/installation/",
"ubuntu": "https://wiki.t2linux.org/distributions/ubuntu/installation/",
"mint": "https://wiki.t2linux.org/distributions/ubuntu/installation/",
"debian": "https://wiki.t2linux.org/distributions/debian/installation/",
"popos": "https://wiki.t2linux.org/distributions/debian/installation/",
"uninstall": "https://wiki.t2linux.org/guides/uninstall/",
"kernel": "https://wiki.t2linux.org/guides/kernel/",
"startup": "https://wiki.t2linux.org/guides/startup-manager/",
"refind": "https://wiki.t2linux.org/guides/refind/",
"state": "https://wiki.t2linux.org/state/"
}
+38
View File
@@ -0,0 +1,38 @@
use std::{collections::HashMap, path::Path};
use anyhow::Result;
use tokio::fs;
#[derive(Clone, Debug)]
pub struct LinkStore {
links: HashMap<String, String>,
}
impl LinkStore {
pub async fn new(path: impl AsRef<Path>) -> Result<Self> {
let content = fs::read_to_string(path).await?;
let links: HashMap<String, String> = serde_json::from_str(&content)?;
Ok(Self { links })
}
pub fn get(&self, key: &str) -> Option<&String> {
self.links.get(key)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
#[tokio::test]
async fn test_load_and_get() -> Result<()> {
let mut file = NamedTempFile::new()?;
writeln!(file, r#"{{ "foo": "bar" }}"#)?;
let store = LinkStore::new(file.path()).await?;
assert_eq!(store.get("foo"), Some(&"bar".to_string()));
assert_eq!(store.get("baz"), None);
Ok(())
}
}
+76
View File
@@ -0,0 +1,76 @@
use anyhow::Result;
use dotenv::dotenv;
use matrix_sdk::{
config::SyncSettings,
room::Room,
ruma::{events::room::message::OriginalSyncRoomMessageEvent, events::room::message::RoomMessageEventContent, events::room::message::MessageType},
Client,
};
use std::env;
use std::sync::Arc;
use crate::links::LinkStore;
mod links;
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
dotenv().ok();
// Load LinkStore
let link_store = Arc::new(LinkStore::new("links.json").await?);
let homeserver_url = env::var("MATRIX_HOMESERVER").expect("MATRIX_HOMESERVER not set");
let username = env::var("MATRIX_USERNAME").expect("MATRIX_USERNAME not set");
let password = env::var("MATRIX_PASSWORD").expect("MATRIX_PASSWORD not set");
let homeserver_url = if !homeserver_url.starts_with("http") {
format!("https://{}", homeserver_url)
} else {
homeserver_url
};
let client = Client::builder()
.homeserver_url(homeserver_url)
.build()
.await?;
client.matrix_auth().login_username(&username, &password).await?;
// Share link_store with event handler
let store_clone = link_store.clone();
client.add_event_handler(move |event: OriginalSyncRoomMessageEvent, room: Room| {
let store = store_clone.clone();
async move {
let RoomMessageEventContent { msgtype, .. } = event.content;
if let MessageType::Text(text_content) = msgtype {
let body = text_content.body;
if body.starts_with(".wiki ") {
let argument = body.trim_start_matches(".wiki ").trim();
if let Some(link) = store.get(argument) {
let response = format!("Link for {}: {}", argument, link);
// Send response
let content = RoomMessageEventContent::text_plain(response);
if let Err(e) = room.send(content).await {
eprintln!("Failed to send message: {}", e);
}
} else {
// Optional: reply not found, or just ignore
let response = format!("No wiki link found for '{}'", argument);
let content = RoomMessageEventContent::text_plain(response);
if let Err(e) = room.send(content).await {
eprintln!("Failed to send message: {}", e);
}
}
}
}
}
});
println!("Bot started!");
client.sync(SyncSettings::default()).await?;
Ok(())
}