Make telegram actually work

This commit is contained in:
asonix 2022-11-02 18:57:08 -05:00
parent e705a90244
commit 1f87c0f913
3 changed files with 36 additions and 16 deletions

2
Cargo.lock generated
View file

@ -279,7 +279,7 @@ checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6"
[[package]] [[package]]
name = "ap-relay" name = "ap-relay"
version = "0.3.30" version = "0.3.31"
dependencies = [ dependencies = [
"activitystreams", "activitystreams",
"activitystreams-ext", "activitystreams-ext",

View file

@ -1,7 +1,7 @@
[package] [package]
name = "ap-relay" name = "ap-relay"
description = "A simple activitypub relay" description = "A simple activitypub relay"
version = "0.3.30" version = "0.3.31"
authors = ["asonix <asonix@asonix.dog>"] authors = ["asonix <asonix@asonix.dog>"]
license = "AGPL-3.0" license = "AGPL-3.0"
readme = "README.md" readme = "README.md"

View file

@ -1,13 +1,22 @@
use crate::db::Db; use crate::db::Db;
use std::sync::Arc; use std::sync::Arc;
use teloxide::{prelude::*, utils::command::BotCommands}; use teloxide::{
dispatching::{Dispatcher, UpdateFilterExt},
requests::{Requester, ResponseResult},
types::{Message, Update},
utils::command::BotCommands,
Bot,
};
#[derive(BotCommands, Clone)] #[derive(BotCommands, Clone, Debug)]
#[command( #[command(
rename_rule = "lowercase", rename_rule = "lowercase",
description = "These commands are for administering AodeRelay" description = "These commands are for administering AodeRelay"
)] )]
enum Command { enum Command {
#[command(description = "Display this text.")]
Start,
#[command(description = "Display this text.")] #[command(description = "Display this text.")]
Help, Help,
@ -29,18 +38,28 @@ pub(crate) fn start(admin_handle: String, db: Db, token: &str) {
let admin_handle = Arc::new(admin_handle); let admin_handle = Arc::new(admin_handle);
actix_rt::spawn(async move { actix_rt::spawn(async move {
teloxide::repl(bot, move |bot: Bot, msg: Message, cmd: Command| { let command_handler = teloxide::filter_command::<Command, _>().endpoint(
move |bot: Bot, msg: Message, cmd: Command| {
let admin_handle = admin_handle.clone(); let admin_handle = admin_handle.clone();
let db = db.clone(); let db = db.clone();
async move { async move {
if !is_admin(&admin_handle, &msg) { if !is_admin(&admin_handle, &msg) {
bot.send_message(msg.chat.id, "You are not authorized")
.await?;
return Ok(()); return Ok(());
} }
answer(bot, msg, cmd, db).await answer(bot, msg, cmd, db).await
} }
}) },
);
let message_handler = Update::filter_message().branch(command_handler);
Dispatcher::builder(bot, message_handler)
.build()
.dispatch()
.await; .await;
}); });
} }
@ -53,9 +72,10 @@ fn is_admin(admin_handle: &str, message: &Message) -> bool {
.unwrap_or(false) .unwrap_or(false)
} }
#[tracing::instrument(skip(bot, msg, db))]
async fn answer(bot: Bot, msg: Message, cmd: Command, db: Db) -> ResponseResult<()> { async fn answer(bot: Bot, msg: Message, cmd: Command, db: Db) -> ResponseResult<()> {
match cmd { match cmd {
Command::Help => { Command::Help | Command::Start => {
bot.send_message(msg.chat.id, Command::descriptions().to_string()) bot.send_message(msg.chat.id, Command::descriptions().to_string())
.await?; .await?;
} }
@ -79,7 +99,7 @@ async fn answer(bot: Bot, msg: Message, cmd: Command, db: Db) -> ResponseResult<
} }
Command::Disallow { domain } => { Command::Disallow { domain } => {
if db.remove_allows(vec![domain.clone()]).await.is_ok() { if db.remove_allows(vec![domain.clone()]).await.is_ok() {
bot.send_message(msg.chat.id, format!("{} has been disallwoed", domain)) bot.send_message(msg.chat.id, format!("{} has been disallowed", domain))
.await?; .await?;
} }
} }