Skip to content

Commit

Permalink
feat(msg): support receiving GIF
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn committed Jun 29, 2022
1 parent 57e8cee commit 16d5ac5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 21 deletions.
29 changes: 28 additions & 1 deletion Cargo.lock

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

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tokio-util = "0.7.3"
tokio-stream = "0.1.9"
futures = "0.3.21"
async-trait = "0.1.56"
reqwest = { version = "0.11.11", default-features = false, features = ["rustls","rustls-tls-native-roots","stream","multipart"] }

# error handling
thiserror = "1.0.31"
Expand All @@ -54,9 +55,7 @@ yaml-rust = "0.4.5"
linked-hash-map = "0.5.6"
either = "1.6.1"
mime = "0.3.16"

# http
reqwest = { version = "0.11.11", default-features = false, features = ["rustls","rustls-tls-native-roots","stream","multipart"] }
infer = "0.8.1"

# database
sled = "0.34.7"
Expand Down
47 changes: 37 additions & 10 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use arcstr::ArcStr;
use lateinit::LateInit;
use mesagisto_client::{cache::CACHE, net::NET, res::RES};
use std::ops::Deref;
use teloxide::payloads::SendAnimationSetters;
use teloxide::types::Message;
use teloxide::{
adaptors::{AutoSend, DefaultParseMode},
payloads::{SendMessageSetters, SendPhotoSetters},
Expand Down Expand Up @@ -89,16 +91,32 @@ impl TgBot {
pub async fn send_image(
&self,
chat_id: ChatId,
photo: InputFile,
image_path: &std::path::Path,
reply: Option<i32>,
) -> anyhow::Result<teloxide::types::Message> {
let send = self.inner.send_photo(chat_id, photo.clone());
let send = if let Some(reply) = reply {
send.reply_to_message_id(reply)
let photo = InputFile::file(image_path);
let kind = infer::get_from_path(image_path)
.expect("file read failed when refering file type")
.expect("Unkown file type");
let is_gif = "gif" == kind.extension();

let result = if is_gif {
let send = self.inner.send_animation(chat_id, photo.clone());
if let Some(reply) = reply {
send.reply_to_message_id(reply).await
} else {
send.await
}
} else {
send
let send = self.inner.send_photo(chat_id, photo.clone());
if let Some(reply) = reply {
send.reply_to_message_id(reply).await
} else {
send.await
}
};
match send.await {

match result {
Ok(ok) => return Ok(ok),
Err(e) => match e {
teloxide::RequestError::MigrateToChatId(new_id) => {
Expand All @@ -107,11 +125,20 @@ impl TgBot {
if let Some(address) = CONFIG.migrate_chat(&target, &new_id) {
handlers::receive::del(target)?;
handlers::receive::add(new_id, &address)?;
let send = TG_BOT.send_photo(ChatId(new_id), photo);
let receipt = if let Some(reply) = reply {
send.reply_to_message_id(reply).await?
let receipt: Message = if is_gif {
let send = self.inner.send_animation(chat_id, photo.clone());
if let Some(reply) = reply {
send.reply_to_message_id(reply).await?
} else {
send.await?
}
} else {
send.await?
let send = self.inner.send_photo(chat_id, photo.clone());
if let Some(reply) = reply {
send.reply_to_message_id(reply).await?
} else {
send.await?
}
};
return Ok(receipt);
} else {
Expand Down
10 changes: 3 additions & 7 deletions src/message/handlers/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use mesagisto_client::{
db::DB,
server::SERVER,
};

use teloxide::types::ChatId;
use teloxide::types::InputFile;
use teloxide::utils::html;
use tokio::sync::mpsc::UnboundedSender;
use tracing::error;
Expand Down Expand Up @@ -121,13 +121,9 @@ async fn left_sub_handler(mut message: Message, target: i64) -> anyhow::Result<(
DB.put_msg_id_ir_2(&target, &receipt.id, &message.id)?;
let receipt = if let Some(reply_to) = &message.reply {
let local_id = DB.get_msg_id_1(&target, reply_to)?;
TG_BOT
.send_image(chat_id, InputFile::file(path), local_id)
.await?
TG_BOT.send_image(chat_id, &path, local_id).await?
} else {
TG_BOT
.send_image(chat_id, InputFile::file(path), None)
.await?
TG_BOT.send_image(chat_id, &path, None).await?
};
DB.put_msg_id_1(&target, &message.id, &receipt.id)?;
}
Expand Down

0 comments on commit 16d5ac5

Please sign in to comment.