add from in the replay spoiler message

This commit is contained in:
Guillermo Roche 2023-11-12 20:26:12 +01:00
parent 61d7a09e01
commit a46a703f7c
Signed by: groche97
GPG Key ID: 041FB85BEEA4B9B0
4 changed files with 21 additions and 13 deletions

View File

@ -5,6 +5,7 @@ mod pole_dialogue;
mod ban_stiker; mod ban_stiker;
mod database; mod database;
mod spoiler_mangas; mod spoiler_mangas;
mod telegram_utils;
#[derive(BotCommands, Clone)] #[derive(BotCommands, Clone)]
#[command(rename_rule = "lowercase", description = "These commands are supported:")] #[command(rename_rule = "lowercase", description = "These commands are supported:")]

View File

@ -2,6 +2,7 @@ use teloxide::prelude::*;
use chrono::Local; use chrono::Local;
use std::cmp::Ordering::Equal; use std::cmp::Ordering::Equal;
use std::str; use std::str;
use crate::telegram_utils::*;
mod database; mod database;
fn change_day(last_day: &str) -> bool{ fn change_day(last_day: &str) -> bool{
@ -32,13 +33,6 @@ fn do_fail(group_id: &str, user_id: &str, user_name: &str){
data.write_points(group_id, user_id, user_name, &Local::now().format("%Y-%m-%d").to_string(),Rewards::FAIL as i64); data.write_points(group_id, user_id, user_name, &Local::now().format("%Y-%m-%d").to_string(),Rewards::FAIL as i64);
} }
fn get_alias(msg: &teloxide::prelude::Message) -> String {
match &msg.from().unwrap().username {
Some(alias) => format!("@{}",alias),
None => format!("{}",&*msg.from().unwrap().first_name),
}
}
fn check_user_points(msg: &teloxide::prelude::Message, rw: Rewards) -> bool{ fn check_user_points(msg: &teloxide::prelude::Message, rw: Rewards) -> bool{
let data: database::DatabasePole = database::DatabasePole::get_database(); let data: database::DatabasePole = database::DatabasePole::get_database();
let ret = data.check_user_pole(&msg.chat.id.to_string(), let ret = data.check_user_pole(&msg.chat.id.to_string(),

View File

@ -9,11 +9,12 @@ use teloxide::{
use image::{DynamicImage,GenericImageView}; use image::{DynamicImage,GenericImageView};
use std::io::Cursor; use std::io::Cursor;
use tokio::io::BufStream; use tokio::io::BufStream;
use crate::telegram_utils::*;
const TOLERANCE: i16 = 10; const TOLERANCE: i16 = 10;
const PERCENT_ACEPTANCE: u8 = 5; const PERCENT_ACEPTANCE: u8 = 5;
const WHITE : u8 = 160; const WHITE : u8 = 160;
const WHITE_ACEPTANCE: u8 = 50; const WHITE_ACEPTANCE: u8 = 40;
fn check_percent(img: DynamicImage) -> (u8,u8) { fn check_percent(img: DynamicImage) -> (u8,u8) {
let mut cont = 0; 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> { pub fn append_text(new_msg: MultipartRequest<SendPhoto>, old_msg: Message) -> MultipartRequest<SendPhoto> {
match old_msg.caption() { match old_msg.caption() {
Some(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_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, 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 ret = Vec::new();
let mut last_hole = 0; let mut last_hole = 0;
for cap in captions { for mut cap in captions {
match cap.kind { match cap.kind {
MessageEntityKind::TextMention { user: ref User } => { MessageEntityKind::TextMention { user: ref User } => {
if cap.offset == 0 { if cap.offset == 0 {
last_hole = cap.length; last_hole = cap.length;
}else if cap.offset >= last_hole { }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; last_hole = cap.offset + cap.length;
} }
cap.offset += offset;
ret.push(cap.clone()); ret.push(cap.clone());
}, },
_ => continue, _ => continue,
} }
} }
if last_hole < len { if last_hole < len {
ret.push(MessageEntity::spoiler(last_hole,len - last_hole)); ret.push(MessageEntity::spoiler(last_hole+offset,len - last_hole));
} }
ret ret
} }
@ -121,3 +127,4 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> {
None => Ok(()), None => Ok(()),
} }
} }

View File

@ -0,0 +1,6 @@
pub fn get_alias(msg: &teloxide::prelude::Message) -> String {
match &msg.from().unwrap().username {
Some(alias) => format!("@{}",alias),
None => format!("{}",&*msg.from().unwrap().first_name),
}
}