ayrat555 / frankenstein

Telegram bot API client for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MessageEntityType for all entities is annoying.

pxp9 opened this issue · comments

Maybe we can do a MessageEntityType for parse all entities in messages, Also send_message method panic! with unicode characters likes these ❦, ∆ would be awesome if somehow the crate could parse this characters with no complain.

Thank you !

Awesome crate for building , async bots in Rust and Telegram and very good support :D .

@pxp9

Also send_message method panic! with unicode characters likes these ❦, ∆

I just tried to send a message with your symbols. it worked fine

    let send_message_params = SendMessageParams::builder()
        .chat_id(message.chat.id)
        .text("hello ❦, ∆")
        .reply_to_message_id(message.message_id)
        .build();

Maybe we can do a MessageEntityType for parse all entities in messages

can you please elaborate? As far as I know, this type is returned by telegram API

Ok, it works with unicode symbols but i get an error if i do not parse a entity like "!"

Now for parse entities i use this function.

fn parse_string(bad: String) -> String {
    let mut parsed = String::new();
    for c in bad.chars() {
        match c {
            '[' => {
                parsed.push_str("\\[");
            }
            ']' => {
                parsed.push_str("\\]");
            }
            '.' => {
                parsed.push_str("\\.");
            }
            '!' => {
                parsed.push_str("\\!");
            }
            '-' => {
                parsed.push_str("\\-");
            }
            '=' => {
                parsed.push_str("\\=");
            }
            '#' => {
                parsed.push_str("\\#");
            }
            '*' => {
                parsed.push_str("\\*");
            }
            '(' => {
                parsed.push_str("\\(");
            }
            ')' => {
                parsed.push_str("\\)");
            }
            '+' => {
                parsed.push_str("\\+");
            }
            '}' => {
                parsed.push_str("\\}");
            }
            '{' => {
                parsed.push_str("\\{");
            }
            '_' => {
                parsed.push_str("\\_");
            }
            _ => {
                let mut b = [0; 4];
                parsed.push_str(c.encode_utf8(&mut b))
            }
        }
    }
    parsed
}

Is there a way to parse all entities (dot , * , ! , etc) in SendMessageParamsBuilder or MessageEntityType ?
if i do not call parse_string the request will panic!

This not panic

let text = parse_string(format!(
            "Hi, {}! Your city {} was not found",
            conf.username,
            conf.message.text.as_ref().unwrap()
        ));
 send_message_client(conf.chat_id, text, &conf.message, &conf.api).await?;

image

This panics because can not parse "!"

let text = format!(
            "Hi, {}! Your city {} was not found",
            conf.username,
            conf.message.text.as_ref().unwrap()
        );
 send_message_client(conf.chat_id, text, &conf.message, &conf.api).await?;

image

Function to send messages that i use

async fn send_message_client(
    chat_id: &i64,
    text: String,
    message: &Message,
    api: &AsyncApi,
) -> Result<(), Error> {
    let send_message_params = SendMessageParams::builder()
        .chat_id(*chat_id)
        .text(text)
        .reply_to_message_id(message.message_id)
        .parse_mode(ParseMode::MarkdownV2)
        .build();
    api.send_message(&send_message_params).await?;
    Ok(())
}

Thank you @ayrat555 for your support.

check src/main.rs:391:77. it contains an Result::unwrap() which panics as the error message states 🙃

I know it is panicking in that line, I am asking if the crate could parse all entities with one way in SendParamsBuilder in order to avoid to call parse_string function, maybe do something like this

async fn send_message_client(
    chat_id: &i64,
    text: String,
    message: &Message,
    api: &AsyncApi,
) -> Result<(), Error> {
    let send_message_params = SendMessageParams::builder()
        .chat_id(*chat_id)
        .text(text)
        .entities(vec![MessageEntity::builder()
                  .type_field(MessageEntityType::All)
                  .build()])
        .reply_to_message_id(message.message_id)
        .parse_mode(ParseMode::MarkdownV2)
        .build();
    api.send_message(&send_message_params).await?;
    Ok(())
}

In order to parse all entities in a message.
Right now the crate can parse all entities , but you have to create a MessageEntity for each one.

Personally I prefer using HTML over MarkdownV2 as its simpler to handle and create.

Other than that the SendMessageParams should stick closely to the actual send_message method from Telegram.
Maybe some helpers to escape or format strings according to Telegrams parse_modes could be neat. Something similar to my library for Node.js just for Rust.

ParseMode::Html work for me thank you @EdJoPaTo :D .