Skip to content

Commit

Permalink
refactor: change teloxide to dev branch
Browse files Browse the repository at this point in the history
cancel support of webhook
  • Loading branch information
itsusinn committed Feb 5, 2022
1 parent 5350e29 commit 3962ba2
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 332 deletions.
19 changes: 4 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ edition = "2021"

[dependencies]
# bot
teloxide ={ version = "0.5.3",default-features = false,features = ["frunk", "macros", "auto-send","ctrlc_handler","rustls"] }
teloxide-core = { version = "0.3.3",default-features = false, features = ["rustls"] }
teloxide ={ rev = "25f8634", default-features = false,features = ["frunk", "macros", "auto-send","ctrlc_handler","rustls","dispatching2"],git = "https://github.com/teloxide/teloxide.git"}
teloxide-core = { version = "0.4.0", default-features = false, features = ["rustls","cache_me"] }
teloxide-macros = "0.4.1"

# logging
Expand All @@ -26,7 +26,6 @@ async-std = "1.10.0"
tokio = { version = "1.15.0",default-features = false,features = ["rt-multi-thread", "macros","signal"] }
tokio-util = "0.6.8"
tokio-stream = "0.1.7"
smol = "1.2.5"
futures = "0.3.17"

# error handling
Expand All @@ -46,7 +45,6 @@ once_cell = "1.8.0"
educe = "0.4.18"
num-bigint = "0.4.2"
arcstr = { version = "1.1.1", features = ["serde"] }

# webhook
warp = "0.3.1"

Expand All @@ -58,20 +56,11 @@ sled = "0.34.7"

# mesagisto
nats = "0.17.0"
# mesagisto-client = { rev = "414e33", git = "https://github.com/MeowCat-Studio/mesagisto-client-rs.git"}
mesagisto-client = { path = "../../mesagisto-client/rust" }
mesagisto-client = { rev = "414e33", git = "https://github.com/MeowCat-Studio/mesagisto-client-rs.git"}
# mesagisto-client = { path = "../../mesagisto-client/rust" }
automatic-config = { git = "https://github.com/Itsusinn/automatic-config-rs.git" }
singleton = { git = "https://github.com/Itsusinn/singleton-rs.git", rev = "bee321c" }
# singleton = { path = "/home/itsusinn/Workspace/Code/singleton-rs" }
either = "1.6.1"
# hex = "0.4"
base64-url = "1.4.10"

[dependencies.pyo3]
version = "0.15.1"
optional = true
features = ["auto-initialize"]

[features]
# default = ["yinglish"]
yinglish = ["pyo3"]
8 changes: 4 additions & 4 deletions src/bot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ops::Deref, sync::Arc};
use std::ops::Deref;

use crate::config::CONFIG;
use arcstr::ArcStr;
Expand All @@ -7,10 +7,10 @@ use teloxide::{adaptors::AutoSend, prelude::Requester, types::File as TgFile, Bo

#[derive(Singleton, Default)]
pub struct TgBot {
inner: LateInit<Arc<AutoSend<Bot>>>,
inner: LateInit<AutoSend<Bot>>,
}
impl TgBot {
pub fn init(&self, bot: Arc<AutoSend<Bot>>) {
pub fn init(&self, bot: AutoSend<Bot>) {
self.inner.init(bot)
}
// fixme use this-error
Expand All @@ -36,7 +36,7 @@ impl TgBot {
}
}
impl Deref for TgBot {
type Target = Arc<AutoSend<Bot>>;
type Target = AutoSend<Bot>;
fn deref(&self) -> &Self::Target {
&self.inner
}
Expand Down
84 changes: 43 additions & 41 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,56 @@
use crate::config::CONFIG;
use std::sync::Arc;
use teloxide::dispatching::UpdateWithCx;
use teloxide::prelude::*;

use teloxide::prelude2::*;
use teloxide::utils::command::BotCommand;

#[derive(BotCommand)]
#[derive(BotCommand, Clone)]
#[command(rename = "lowercase", description = "信使Bot支持以下命令")]
pub enum Command {
#[command(description = "显示命令帮助")]
Help,
#[command(description = "启用消息转发")]
Enable,
#[command(description = "禁用消息转发")]
Disable,
// #[command(description = "启用消息转发")]
// Enable,
// #[command(description = "禁用消息转发")]
// Disable,
#[command(description = "设置当前Group的转发地址", parse_with = "split")]
SetAddress { address: String },
}

pub async fn answer(
cx: Arc<UpdateWithCx<AutoSend<Bot>, Message>>,
command: Command,
) -> anyhow::Result<()> {
match command {
Command::Help => {
cx.answer(Command::descriptions()).await?;
}
Command::Enable => {
cx.answer("Mesagisto信使已启用").await?;
}
Command::Disable => {
cx.answer("Mesagisto信使已禁用").await?;
}
Command::SetAddress { address } => {
let sender_id = cx.update.from().unwrap().id;
let chat_id = cx.chat_id();
let admins = cx.requester.get_chat_administrators(chat_id).await?;
let mut is_admin = false;
for admin in admins {
if admin.user.id == sender_id {
is_admin = true;
break;
}
impl Command {
pub async fn answer(msg: Message, bot: AutoSend<Bot>, cmd: Command) -> anyhow::Result<()> {
match cmd {
Command::Help => {
let chat_id = msg.chat_id();
bot.send_message(chat_id, Command::descriptions()).await?;
}
if is_admin {
CONFIG.target_address_mapper.insert(chat_id, address.into());
cx.answer("成功设置当前Group的信使地址").await?;
} else {
cx.answer("权限不足,拒绝设置信使频道").await?;
// Command::Enable => {
// cx.answer("Mesagisto信使已启用").await?;
// }
// Command::Disable => {
// cx.answer("Mesagisto信使已禁用").await?;
// }
Command::SetAddress { address } => {
let sender_id = msg.from().unwrap().id;
let chat_id = msg.chat_id();
let admins = bot.get_chat_administrators(chat_id).await?;
let mut is_admin = false;
for admin in admins {
if admin.user.id == sender_id {
is_admin = true;
break;
}
}
if is_admin {
CONFIG.target_address_mapper.insert(chat_id, address.into());
bot
.send_message(chat_id, "成功设置当前Group的信使地址")
.await?;
} else {
bot
.send_message(chat_id, "权限不足,拒绝设置信使频道")
.await?;
}
}
}
};
Ok(())
};
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ pub struct WebhookConfig {

#[basic_derive]
pub struct FormatConfig {
pub msg: ArcStr
pub msg: ArcStr,
}
118 changes: 0 additions & 118 deletions src/despatch.rs

This file was deleted.

30 changes: 30 additions & 0 deletions src/dispatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use teloxide::prelude2::*;
use tracing::info;

use crate::{command::Command, message::handlers};

pub async fn start(bot: &AutoSend<Bot>) {
let handler = Update::filter_message()
.branch(
dptree::entry()
.filter_command::<Command>()
.endpoint(Command::answer),
)
.branch(
dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup())
.endpoint(handlers::send::answer_common),
);
info!("Mesagisto-Bot启动成功");
Dispatcher::builder(bot.clone(), handler)
.default_handler(|upd| async move {
log::warn!("Unhandled update: {:?}", upd);
})
// If the dispatcher fails for some reason, execute this handler.
.error_handler(LoggingErrorHandler::with_custom_text(
"An error has occurred in the dispatcher",
))
.build()
.setup_ctrlc_handler()
.dispatch()
.await;
}
24 changes: 7 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

use futures::FutureExt;
use mesagisto_client::MesagistoConfig;
use std::sync::Arc;
use teloxide::{prelude::*, Bot};
use teloxide::{prelude2::*, Bot};

use bot::TG_BOT;
use config::CONFIG;
use despatch::cmd_or_msg_repl;

#[macro_use]
extern crate educe;
Expand All @@ -19,11 +17,10 @@ extern crate singleton;
mod bot;
mod command;
mod config;
mod despatch;
mod dispatch;
pub mod ext;
mod message;
mod net;
mod webhook;

#[tokio::main]
async fn main() {
Expand All @@ -32,9 +29,9 @@ async fn main() {
#[allow(unused_must_use)]
async fn run() -> Result<(), anyhow::Error> {
let env = tracing_subscriber::EnvFilter::from("warn")
.add_directive("teloxide=info".parse()?)
.add_directive("telegram_message_source=info".parse()?)
.add_directive("mesagisto_client=info".parse()?);
.add_directive("teloxide=info".parse()?)
.add_directive("telegram_message_source=info".parse()?)
.add_directive("mesagisto_client=info".parse()?);
tracing_subscriber::fmt().with_env_filter(env).init();

if !CONFIG.enable {
Expand Down Expand Up @@ -71,15 +68,8 @@ async fn run() -> Result<(), anyhow::Error> {

let bot = Bot::with_client(CONFIG.telegram.token.clone(), net::client_from_config()).auto_send();

TG_BOT.init(Arc::new(bot));

cmd_or_msg_repl(
&*TG_BOT,
&*CONFIG.telegram.bot_name,
command::answer,
message::handler::answer_msg,
)
.await;
TG_BOT.init(bot);
dispatch::start(&TG_BOT).await;

CONFIG.save();
log::info!("Mesagisto Bot is going to shut down");
Expand Down
7 changes: 0 additions & 7 deletions src/message/handler.rs

This file was deleted.

Loading

0 comments on commit 3962ba2

Please sign in to comment.