-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create input_user.rs #208
base: master
Are you sure you want to change the base?
Create input_user.rs #208
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to expose Input types to the user of the library. The PackedChat
abstraction already covers this usecase.
pub enum MessageEntity {
Unknown(crate::types::MessageEntityUnknown),
Mention(crate::types::MessageEntityMention),
Hashtag(crate::types::MessageEntityHashtag),
BotCommand(crate::types::MessageEntityBotCommand),
Url(crate::types::MessageEntityUrl),
Email(crate::types::MessageEntityEmail),
Bold(crate::types::MessageEntityBold),
Italic(crate::types::MessageEntityItalic),
Code(crate::types::MessageEntityCode),
Pre(crate::types::MessageEntityPre),
TextUrl(crate::types::MessageEntityTextUrl),
MentionName(crate::types::MessageEntityMentionName),
InputMessageEntityMentionName(crate::types::InputMessageEntityMentionName),
Phone(crate::types::MessageEntityPhone),
Cashtag(crate::types::MessageEntityCashtag),
Underline(crate::types::MessageEntityUnderline),
Strike(crate::types::MessageEntityStrike),
BankCard(crate::types::MessageEntityBankCard),
Spoiler(crate::types::MessageEntitySpoiler),
CustomEmoji(crate::types::MessageEntityCustomEmoji),
Blockquote(crate::types::MessageEntityBlockquote),
}
pub struct InputMessageEntityMentionName {
pub offset: i32,
pub length: i32,
pub user_id: crate::enums::InputUser,
} If I am now implementing conversion for I looked at |
The content of the #[derive(Debug)]
pub enum EntityType {
Unknown,
Mention,
Hashtag,
BotCommand,
Url,
Email,
Bold,
Italic,
Code,
Pre(String),
TextUrl(String),
MentionName(i64),
InputMessageEntityMentionName(types::input_user::InputUser),
Phone,
Cashtag,
Underline,
Strike,
BankCard,
Spoiler,
CustomEmoji(i64),
Blockquote,
}
impl From<tl::enums::MessageEntity> for EntityType {
fn from(entity: tl::enums::MessageEntity) -> Self {
use tl::enums::MessageEntity as TlMessageEntity;
match entity {
TlMessageEntity::Unknown(_) => Self::Unknown,
TlMessageEntity::Mention(_) => Self::Mention,
TlMessageEntity::Hashtag(_) => Self::Hashtag,
TlMessageEntity::BotCommand(_) => Self::BotCommand,
TlMessageEntity::Url(_) => Self::Url,
TlMessageEntity::Email(_) => Self::Email,
TlMessageEntity::Bold(_) => Self::Bold,
TlMessageEntity::Italic(_) => Self::Italic,
TlMessageEntity::Code(_) => Self::Code,
TlMessageEntity::Pre(pre) => Self::Pre(pre.language.clone()),
TlMessageEntity::TextUrl(text_url) => Self::TextUrl(text_url.url.clone()),
TlMessageEntity::MentionName(user) => Self::MentionName(user.user_id),
TlMessageEntity::InputMessageEntityMentionName(user) => {
Self::InputMessageEntityMentionName(types::input_user::InputUser::_from_raw(
user.user_id.clone(),
))
}
TlMessageEntity::Phone(_) => Self::Phone,
TlMessageEntity::Cashtag(_) => Self::Cashtag,
TlMessageEntity::Underline(_) => Self::Underline,
TlMessageEntity::Strike(_) => Self::Strike,
TlMessageEntity::BankCard(_) => Self::BankCard,
TlMessageEntity::Spoiler(_) => Self::Spoiler,
TlMessageEntity::CustomEmoji(emoji) => Self::CustomEmoji(emoji.document_id),
TlMessageEntity::Blockquote(_) => Self::Blockquote,
}
}
}
#[derive(Debug)]
pub struct Entity {
r#type: EntityType,
text: String,
}
impl Entity {
pub(crate) fn _from_message(
text: &str,
message_entities: Option<Vec<tl::enums::MessageEntity>>,
) -> Vec<Self> {
let mut entities = vec![];
if let Some(original_entities) = message_entities {
for msg_entity in original_entities {
entities.push(Self::_tranform(text, &msg_entity));
}
}
entities
}
fn _tranform(message_text: &str, entity: &tl::enums::MessageEntity) -> Self {
let text_u16 = message_text
.encode_utf16()
.skip(entity.offset() as usize)
.take(entity.length() as usize)
.collect::<Vec<u16>>();
Self {
r#type: entity.clone().into(),
text: String::from_utf16(&text_u16).unwrap_or_default(),
}
}
pub fn _type(&self) -> &EntityType {
&self.r#type
}
pub fn text(&self) -> &str {
&self.text
}
} |
Telegram's current syntax for inline mentions is using a link with the We could instead support a different URL parameter, such as This way we don't need to expose entities to the user at all, but they're free to use HTML instead. I don't think "raw offsets" are that useful. Sure, it might be a bit more powerful. But HTML can get you all of that in a more readable format.
Is it really more convenient than HTML? What is your usecase? |
I don't plan to merge this, but I'll keep it open for now so we can discuss it. |
Thank you for your willingness to discuss it. My current scenario is to get the |
At present, when I deal with entities, I get the original data structure every time. Therefore, I wonder if this operation can be simplified. Otherwise, I need to use the tl_type library externally, that is, the code generated by the library. I want to avoid it as much as possible. Rely on raw data structures, but get more human-friendly data structures from client::types for ease of use |
Ah so your goal is a friendlier version of
InputUser , but a PackedChat .
|
Yes, can I change the access attribute of input_user.rs to |
Telegram should not return |
Then should I implement the MessageEntity structure? This makes me confused |
You said "get". I am assuming you fetched or received a message and want to access its formatting entities. This means
|
No description provided.