J-Rios / TLG_JoinCaptchaBot

Telegram Bot to verify if users joining a group are human. The Bot sends an image captcha for each new user and kicks any of them who can't solve the captcha in a specified time.
GNU General Public License v3.0
516 stars 217 forks source link

cmd_welcome_msg supports use_markdown option #58

Closed ghost closed 3 years ago

ghost commented 3 years ago

Satisfies this request: https://github.com/J-Rios/TLG_JoinCaptchaBot/issues/57

Adds a new default option "Welcome_Msg_Use_Markdown", adds an additional argument to tlg_send_selfdestruct_msg_in, comments provided. If my implementation will be deemed good, I can also update translations in some languages.

ghost commented 3 years ago

Yes, I tested my code. We can store "Welcome_Msg" as a dict, where there will be fields "message" and "type". With that not only Markdown become supported but also Markdown2 and HTML-style. If "Welcome"Msg" is a string, then treat it as plain text.

ghost commented 3 years ago

I need your opinion.

J-Rios commented 3 years ago

Next weekend I will be available and take a look on this :)

J-Rios commented 3 years ago

Hi,

Yes, if we want to support any text format (markdown or html), it makes sense to also store the type of the text in group config...

Going back to reception of command arguments, you have to check if python-telegram-bot framework support something to give the arguments parsed as markdown/html (i'm not sure about it), otherwise, you must parse them by hand from the getUpdate message "entities" (text" field is just raw), before store in the group config JSON, take a look from next getUpdate response.

Send: /welcome_msg Hi $user, welcome to the group and remember to be respectful with other users.

getUpdate response:

{
    "ok": true,
    "result": [
        {
            "update_id": 193048959,
            "message": {
                "message_id": 4096,
                "from": {
                    "id": 231874677,
                    "is_bot": false,
                    "first_name": "JoseR",
                    "username": "JoseTLG",
                    "language_code": "es"
                },
                "chat": {
                    "id": 231874677,
                    "first_name": "JoseR",
                    "username": "JoseTLG",
                    "type": "private"
                },
                "date": 1596208990,
                "text": "/welcome_msg Hi $user, welcome to the group and remember to be respectful with other users.",
                "entities": [
                    {
                        "offset": 0,
                        "length": 12,
                        "type": "bot_command"
                    },
                    {
                        "offset": 23,
                        "length": 7,
                        "type": "bold"
                    },
                    {
                        "offset": 48,
                        "length": 25,
                        "type": "code"
                    }
                ]
            }
        }
    ]
}

BR.

J-Rios commented 3 years ago

mmm, seems that python-telegram-bot parsed to all formats (markdown, makdownv2, html) by itself. So instead getting /welcome_msg command arguments as CallbackContext.args list, just get the text from the message from the Update object:

update.message.text_html update.message.text_html_urled update.message.text_markdown update.message.text_markdown_urled update.message.text_markdown_v2 update.message.text_markdown_v2_urled

So there could be no need to know the type of text neither store it, we can just take one of them, for example text_markdown, and that's all, then store it in the JSON and send the welcome message as {"parse_mode": "Markdown"}.

ghost commented 3 years ago

Your idea is great, I found this feature in the bot's documentation. Thank you!

J-Rios commented 3 years ago

This has been implemented in this commit, thanks for your help.