-
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(handler): a abstraction of receiving and sending message
- Loading branch information
itsusinn
committed
Oct 17, 2021
1 parent
bcf0477
commit fc4036f
Showing
6 changed files
with
179 additions
and
22 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
use std::sync::Arc; | ||
use teloxide::prelude::*; | ||
|
||
use super::handlers::send::answer_common; | ||
pub async fn answer_msg(cx: Arc<UpdateWithCx<AutoSend<Bot>, Message>>) -> anyhow::Result<()> { | ||
answer_common(cx).await | ||
} |
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,2 @@ | ||
mod receive; | ||
pub mod send; |
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,62 @@ | ||
use crate::CONFIG; | ||
use crate::TG_BOT; | ||
use mesagisto_client::{ | ||
cache::CACHE, | ||
data::{message::MessageType, message::Message,Packet}, | ||
db::DB | ||
}; | ||
use teloxide::payloads::SendMessageSetters; | ||
use teloxide::prelude::Requester; | ||
|
||
use teloxide::types::InputFile; | ||
|
||
pub async fn receive_from_server(message: nats::asynk::Message, target: i64) -> anyhow::Result<()> { | ||
log::trace!("Receive from {}",target); | ||
let packet = Packet::from_cbor(&message.data)?; | ||
match packet { | ||
either::Left(msg) => { | ||
handle_receive_message(msg,target).await?; | ||
} | ||
either::Right(_) => {} | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub async fn handle_receive_message(mut message: Message, target: i64) -> anyhow::Result<()> { | ||
|
||
for single in message.chain { | ||
log::trace!("handling element in chain"); | ||
let sender_name = if message.profile.nick.is_some() { | ||
message.profile.nick.take().unwrap() | ||
} else if message.profile.username.is_some() { | ||
message.profile.username.take().unwrap() | ||
} else { | ||
message.profile.id.to_string() | ||
}; | ||
match single { | ||
MessageType::Text { content } => { | ||
let content = format!("{}: {}", sender_name, content); | ||
let receipt = if let Some(reply_to) = &message.reply { | ||
let local_id = DB.get_msg_id_1(&target, reply_to)?; | ||
match local_id { | ||
Some(local_id) => TG_BOT.send_message(target, content).reply_to_message_id(local_id).await?, | ||
None => TG_BOT.send_message(target, content).await? | ||
} | ||
} else { | ||
TG_BOT.send_message(target, content).await? | ||
}; | ||
DB.put_msg_id_1(&target, &message.id, &receipt.id)?; | ||
}, | ||
MessageType::Image { id,url } => { | ||
let channel = CONFIG.mapper(&target).expect("Channel don't exist"); | ||
let path = CACHE.file(&id, &url, &channel).await?; | ||
let receipt = TG_BOT.send_message(target, format!("{} :",sender_name)).await?; | ||
DB.put_msg_id_ir_2(&target, &receipt.id,&message.id)?; | ||
let receipt = TG_BOT.send_photo(target, InputFile::File(path)).await?; | ||
DB.put_msg_id_1(&target, &message.id, &receipt.id)?; | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
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,102 @@ | ||
use crate::config::CONFIG; | ||
use crate::bot::TG_BOT; | ||
use crate::message::handlers::receive::receive_from_server; | ||
use crate::message::Cx; | ||
use arcstr::ArcStr; | ||
use mesagisto_client::EitherExt; | ||
use mesagisto_client::data::message::{MessageType, Profile}; | ||
use mesagisto_client::data::{message, Packet}; | ||
use mesagisto_client::server::SERVER; | ||
use mesagisto_client::res::RES; | ||
use mesagisto_client::db::DB; | ||
use std::sync::Arc; | ||
use teloxide::prelude::*; | ||
|
||
#[cfg(feature = "yinglish")] | ||
pub fn yinglish(text: String) -> String { | ||
use pyo3::{types::PyModule, Python}; | ||
|
||
Python::with_gil(|py| { | ||
let yinglish = PyModule::import(py, "yinglish").unwrap(); | ||
let res: String = yinglish | ||
.call_method1("chs2yin", (text,)) | ||
.unwrap() | ||
.extract() | ||
.unwrap(); | ||
res | ||
}) | ||
} | ||
|
||
pub async fn answer_common(cx: Arc<Cx>) -> anyhow::Result<()> { | ||
let udp = &cx.update; | ||
#[cfg(feature = "yinglish")] | ||
if text.starts_with("!") { | ||
let mut content = text.clone().to_string(); | ||
content.remove(0); | ||
let reply_content = yinglish(content); | ||
cx.reply_to(reply_content).await?; | ||
} | ||
|
||
let target = cx.chat_id(); | ||
if !CONFIG.target_address_mapper.contains_key(&target) { | ||
return Ok(()); | ||
} | ||
let address = CONFIG.target_address_mapper.get(&target).unwrap().clone(); | ||
let sender = match cx.update.from(){ | ||
Some(v) => v, | ||
//fixme | ||
None => return Ok(()) | ||
}; | ||
// let avatar = bot_client().get_user_profile_photos(sender.id).await?; | ||
let profile = Profile { | ||
id: sender.id, | ||
username: sender.username.clone(), | ||
nick: Some( | ||
sender | ||
.full_name() | ||
.replace(|c: char| !c.is_alphanumeric(), ""), | ||
), | ||
}; | ||
let mut chain = Vec::<MessageType>::new(); | ||
if let Some(text) = udp.text() { | ||
chain.push(MessageType::Text{ content: text.to_string() }); | ||
} else if let Some(image) = udp.photo() { | ||
let photo = image.last().unwrap(); | ||
let file_id:ArcStr = photo.file_id.to_owned().into(); | ||
let uid:ArcStr = photo.file_unique_id.to_owned().into(); | ||
RES.store_photo_id(&uid, &file_id); | ||
TG_BOT.file(&uid,&file_id).await?; | ||
chain.push(MessageType::Image { | ||
id: uid,url:None | ||
}) | ||
} else if let Some(sticker) = udp.sticker() { | ||
let file_id:ArcStr = sticker.file_id.to_owned().into(); | ||
let uid:ArcStr = sticker.file_unique_id.to_owned().into(); | ||
RES.store_photo_id(&uid, &file_id); | ||
TG_BOT.file(&uid,&file_id).await?; | ||
chain.push(MessageType::Image { | ||
id:uid,url:None | ||
}) | ||
} | ||
|
||
let reply = match udp.reply_to_message(){ | ||
Some(v) => { | ||
let local_id = v.id.to_be_bytes().to_vec(); | ||
DB.get_msg_id_2(&target, &local_id).unwrap_or(None) | ||
}, | ||
None => None | ||
}; | ||
DB.put_msg_id_0(&udp.chat_id(),&udp.id, &udp.id)?; | ||
let message = message::Message { | ||
profile, | ||
id:udp.id.to_be_bytes().to_vec(), | ||
chain, | ||
reply, | ||
}; | ||
let packet = Packet::encrypt_from(message.tl())?; | ||
|
||
SERVER | ||
.send_and_receive(target, address, packet, receive_from_server) | ||
.await?; | ||
Ok(()) | ||
} |
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,6 @@ | ||
use teloxide::{adaptors::AutoSend, prelude::UpdateWithCx, types::Message, Bot}; | ||
|
||
pub mod handler; | ||
pub mod handlers; | ||
|
||
type Cx = UpdateWithCx<AutoSend<Bot>, Message>; |