Skip to content

Commit

Permalink
Upgrade to tbot 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
iovxw committed Apr 26, 2020
1 parent b1580d5 commit db46486
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 49 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,9 @@ pinyin = "0.7"
either = "1.5"

[dependencies.tbot]
version = "0.5"
default-features = false
features = ["rustls", "proxy"]

[dependencies.hyper-proxy]
version = "0.6"
default-features = false
features = ["rustls"]
features = ["rustls", "proxy"]

[dependencies.reqwest]
version = "0.10"
Expand Down
14 changes: 4 additions & 10 deletions src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::sync::{

use futures::{future::FutureExt, select_biased};
use tbot::{
connectors::Connector,
types::parameters::{self, WebPagePreviewState},
Bot,
};
Expand All @@ -22,12 +21,7 @@ use crate::client::pull_feed;
use crate::data::{Database, Feed, FeedUpdate};
use crate::messages::{format_large_msg, Escape};

pub fn start(
bot: Bot<impl Connector>,
db: Arc<Mutex<Database>>,
min_interval: u32,
max_interval: u32,
) {
pub fn start(bot: Bot, db: Arc<Mutex<Database>>, min_interval: u32, max_interval: u32) {
let mut queue = FetchQueue::new();
// TODO: Don't use interval, it can accumulate ticks
// replace it with delay_until
Expand Down Expand Up @@ -64,7 +58,7 @@ pub fn start(
}

async fn fetch_and_push_updates(
bot: Bot<impl Connector>,
bot: Bot,
db: Arc<Mutex<Database>>,
feed: Feed,
) -> Result<(), tbot::errors::MethodCall> {
Expand All @@ -85,7 +79,7 @@ async fn fetch_and_push_updates(
可能已经关闭, 请取消订阅",
Escape(&feed.link),
Escape(&feed.title),
Escape(&e.to_string())
Escape(&e.to_user_friendly())
);
push_updates(&bot, &db, feed.subscribers, parameters::Text::html(&msg)).await?;
}
Expand Down Expand Up @@ -142,7 +136,7 @@ async fn fetch_and_push_updates(
}

async fn push_updates<I: IntoIterator<Item = i64>>(
bot: &Bot<impl Connector>,
bot: &Bot,
db: &Arc<Mutex<Database>>,
subscribers: I,
msg: parameters::Text<'_>,
Expand Down
9 changes: 3 additions & 6 deletions src/gardener.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::{Arc, Mutex};

use tbot::{connectors::Connector, Bot};
use tbot::Bot;
use tokio::{
self,
time::{self, Duration},
Expand All @@ -9,7 +9,7 @@ use tokio::{
use crate::data::Database;
use crate::BOT_ID;

pub fn start_pruning(bot: Bot<impl Connector>, db: Arc<Mutex<Database>>) {
pub fn start_pruning(bot: Bot, db: Arc<Mutex<Database>>) {
let mut interval = time::interval(Duration::from_secs(1 * 24 * 60 * 60));
tokio::spawn(async move {
loop {
Expand All @@ -21,10 +21,7 @@ pub fn start_pruning(bot: Bot<impl Connector>, db: Arc<Mutex<Database>>) {
});
}

async fn prune(
bot: &Bot<impl Connector>,
db: &Mutex<Database>,
) -> Result<(), tbot::errors::MethodCall> {
async fn prune(bot: &Bot, db: &Mutex<Database>) -> Result<(), tbot::errors::MethodCall> {
let subscribers = db.lock().unwrap().all_subscribers();
for subscriber in subscribers {
let chat_id = tbot::types::chat::Id(subscriber);
Expand Down
15 changes: 7 additions & 8 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::sync::Mutex;
use either::Either;
use pinyin::{Pinyin, ToPinyin};
use tbot::{
connectors::Connector,
contexts::{Command, Text},
types::{
input_file,
Expand Down Expand Up @@ -42,7 +41,7 @@ impl MsgTarget {

pub async fn start(
_db: Arc<Mutex<Database>>,
cmd: Arc<Command<Text<impl Connector>>>,
cmd: Arc<Command<Text>>,
) -> Result<(), tbot::errors::MethodCall> {
let target = &mut MsgTarget::new(cmd.chat.id, cmd.message_id);
let msg = "命令列表:\n\
Expand All @@ -56,7 +55,7 @@ pub async fn start(

pub async fn rss(
db: Arc<Mutex<Database>>,
cmd: Arc<Command<Text<impl Connector>>>,
cmd: Arc<Command<Text>>,
) -> Result<(), tbot::errors::MethodCall> {
let chat_id = cmd.chat.id;
let channel = &cmd.text.value;
Expand Down Expand Up @@ -113,7 +112,7 @@ pub async fn rss(

pub async fn sub(
db: Arc<Mutex<Database>>,
cmd: Arc<Command<Text<impl Connector>>>,
cmd: Arc<Command<Text>>,
) -> Result<(), tbot::errors::MethodCall> {
let chat_id = cmd.chat.id;
let text = &cmd.text.value;
Expand Down Expand Up @@ -175,7 +174,7 @@ pub async fn sub(

pub async fn unsub(
db: Arc<Mutex<Database>>,
cmd: Arc<Command<Text<impl Connector>>>,
cmd: Arc<Command<Text>>,
) -> Result<(), tbot::errors::MethodCall> {
let chat_id = cmd.chat.id;
let text = &cmd.text.value;
Expand Down Expand Up @@ -216,7 +215,7 @@ pub async fn unsub(

pub async fn export(
db: Arc<Mutex<Database>>,
cmd: Arc<Command<Text<impl Connector>>>,
cmd: Arc<Command<Text>>,
) -> Result<(), tbot::errors::MethodCall> {
let chat_id = cmd.chat.id;
let channel = &cmd.text.value;
Expand Down Expand Up @@ -251,7 +250,7 @@ pub async fn export(
}

async fn update_response(
bot: &Bot<impl Connector>,
bot: &Bot,
target: &mut MsgTarget,
message: parameters::Text<'_>,
) -> Result<(), tbot::errors::MethodCall> {
Expand All @@ -272,7 +271,7 @@ async fn update_response(
}

async fn check_channel_permission(
bot: &Bot<impl Connector>,
bot: &Bot,
channel: &str,
target: &mut MsgTarget,
user_id: tbot::types::user::Id,
Expand Down
35 changes: 19 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ use std::process;
use std::sync::{Arc, Mutex}; // TODO: async Mutex

use anyhow::Context;
use hyper_proxy::{Intercept, Proxy, ProxyConnector};
use once_cell::sync::OnceCell;
use structopt::StructOpt;
use tbot;
use tbot::{
self,
proxy::{Intercept, Proxy},
};
use tokio;

mod client;
Expand Down Expand Up @@ -75,8 +77,11 @@ async fn main() -> anyhow::Result<()> {

let opt = Opt::from_args();
let db = Arc::new(Mutex::new(Database::open(opt.database)?));
let connector = init_bot_connector();
let bot = tbot::Bot::with_connector(opt.token, connector);
let bot = if let Some(proxy) = init_proxy() {
tbot::Bot::with_proxy(opt.token, proxy)
} else {
tbot::Bot::new(opt.token)
};
let me = bot
.get_me()
.call()
Expand Down Expand Up @@ -109,19 +114,17 @@ fn enable_fail_fast() {
}));
}

fn init_bot_connector() -> ProxyConnector<tbot::connectors::Https> {
fn init_proxy() -> Option<Proxy> {
// Telegram Bot API only uses https, no need to check http_proxy
let proxy = env::var("HTTPS_PROXY").or_else(|_| env::var("https_proxy"));
let mut c = ProxyConnector::new(tbot::connectors::https())
.unwrap_or_else(|e| panic!("Failed to construct a proxy connector: {}", e));
if let Ok(ref proxy) = proxy {
let uri = proxy
.try_into()
.unwrap_or_else(|e| panic!("Illegal HTTPS_PROXY: {}", e));
let proxy = Proxy::new(Intercept::All, uri);
c.add_proxy(proxy);
}
c
env::var("HTTPS_PROXY")
.or_else(|_| env::var("https_proxy"))
.map(|uri| {
let uri = uri
.try_into()
.unwrap_or_else(|e| panic!("Illegal HTTPS_PROXY: {}", e));
Proxy::new(Intercept::All, uri)
})
.ok()
}

fn print_error<E: std::error::Error>(err: E) {
Expand Down

0 comments on commit db46486

Please sign in to comment.