-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: some helper functions of teloxide
- Loading branch information
itsusinn
committed
Oct 17, 2021
1 parent
d26d6e2
commit b2a2ec6
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::{ops::Deref, sync::Arc}; | ||
|
||
use arcstr::ArcStr; | ||
use crate::config::CONFIG; | ||
use mesagisto_client::{LateInit, cache::CACHE, res::RES}; | ||
use teloxide::{Bot, adaptors::AutoSend, net::Download, prelude::Requester, types::File as TgFile}; | ||
|
||
#[derive(Singleton, Default)] | ||
pub struct TgBot { | ||
inner: LateInit<Arc<AutoSend<Bot>>>, | ||
} | ||
impl TgBot { | ||
pub fn init(&self, bot: Arc<AutoSend<Bot>>) { | ||
self.inner.init(bot) | ||
} | ||
// fixme use this-error | ||
pub async fn file(&self,uid:&ArcStr,id: &ArcStr) -> anyhow::Result<()>{ | ||
let TgFile{ file_path,.. } = self.get_file(id.as_str()).await.expect("failed to get file"); | ||
let tmp_path = RES.tmp_path(id); | ||
let mut file = tokio::fs::File::create(&tmp_path).await?; | ||
// mention: this is stream | ||
self.inner.download_file(&file_path, &mut file).await?; | ||
CACHE.put_file(uid, &tmp_path).await?; | ||
Ok(()) | ||
} | ||
pub fn get_url_by_path(&self,file_path: String) -> ArcStr{ | ||
format!( | ||
"https://api.telegram.org/file/bot{token}/{file}", | ||
token = CONFIG.telegram.token, | ||
file = file_path | ||
).into() | ||
} | ||
} | ||
impl Deref for TgBot { | ||
type Target = Arc<AutoSend<Bot>>; | ||
fn deref(&self) -> &Self::Target { | ||
&self.inner | ||
} | ||
} |