add from in the replay spoiler message

This commit is contained in:
2023-11-12 20:26:12 +01:00
parent 61d7a09e01
commit a46a703f7c
4 changed files with 21 additions and 13 deletions

View File

@@ -9,11 +9,12 @@ use teloxide::{
use image::{DynamicImage,GenericImageView};
use std::io::Cursor;
use tokio::io::BufStream;
use crate::telegram_utils::*;
const TOLERANCE: i16 = 10;
const PERCENT_ACEPTANCE: u8 = 5;
const WHITE : u8 = 160;
const WHITE_ACEPTANCE: u8 = 50;
const WHITE_ACEPTANCE: u8 = 40;
fn check_percent(img: DynamicImage) -> (u8,u8) {
let mut cont = 0;
@@ -38,32 +39,37 @@ fn check_percent(img: DynamicImage) -> (u8,u8) {
pub fn append_text(new_msg: MultipartRequest<SendPhoto>, old_msg: Message) -> MultipartRequest<SendPhoto> {
match old_msg.caption() {
Some(caption) => {
let mut msg_from = get_alias(&old_msg);
msg_from.push_str(": ");
msg_from = String::from("Mensaje de ") + &msg_from;
let from_offset = msg_from.encode_utf16().count();
//new_msg.caption(caption).caption_entities(generate_entity_spoiler(caption.len())).has_spoiler(true)
new_msg.caption(caption).caption_entities(generate_captions(old_msg.caption_entities().unwrap().to_vec(),caption.encode_utf16().count()))
new_msg.caption(msg_from + caption).caption_entities(generate_captions(old_msg.caption_entities().unwrap().to_vec(),caption.encode_utf16().count(), from_offset))
},
None => new_msg,
}
}
pub fn generate_captions(captions: Vec<MessageEntity>, len: usize) -> Vec<MessageEntity> {
pub fn generate_captions(captions: Vec<MessageEntity>, len: usize, offset: usize) -> Vec<MessageEntity> {
let mut ret = Vec::new();
let mut last_hole = 0;
for cap in captions {
for mut cap in captions {
match cap.kind {
MessageEntityKind::TextMention { user: ref User } => {
if cap.offset == 0 {
last_hole = cap.length;
}else if cap.offset >= last_hole {
ret.push(MessageEntity::spoiler(last_hole,cap.offset-last_hole));
ret.push(MessageEntity::spoiler(last_hole+offset,cap.offset-last_hole));
last_hole = cap.offset + cap.length;
}
cap.offset += offset;
ret.push(cap.clone());
},
_ => continue,
}
}
if last_hole < len {
ret.push(MessageEntity::spoiler(last_hole,len - last_hole));
ret.push(MessageEntity::spoiler(last_hole+offset,len - last_hole));
}
ret
}
@@ -121,3 +127,4 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> {
None => Ok(()),
}
}