telegram-bot-sdk icon indicating copy to clipboard operation
telegram-bot-sdk copied to clipboard

Bad Request: can't parse entities in message text

Open cat24max opened this issue 7 years ago • 11 comments

Hey, I know this "issue" was already brought up a couple times, but I don't seem to understand why.

Telegram\Bot\Exceptions\TelegramOtherException: Bad Request: can't parse entities in message text: Can't find end of the entity starting at byte offset 52 File "app/Console/Commands/SendConfirmationMailCommand.php", line 64, in handle 'text' => '*E-Mail Verification*'. PHP_EOL . 'Nachricht an '.$email->email.' wurde gesendet.'

This error keeps popping up SOMETIMES, but not every time. Is has worked for months now and suddenly it throws this exception. Nothing has changed in the code and the variables passed are always simple strings (in this case, an email address).

Why would that happen so randomly?

cat24max avatar Nov 02 '17 19:11 cat24max

Looks like you use Markdown for the parse_mode and there may be conflict with a syntax. For example with the italic syntax _. I mean if some of your users have email with the character _ in odd amounts Telegram can't parse it...

zvermafia avatar Dec 06 '17 10:12 zvermafia

Removing of 3 signs <<`*_>> from text solved my problem.

p.s. parse_mode='markdown'

chebotarevmichael avatar Mar 09 '19 11:03 chebotarevmichael

@zvermafia Years later, you managed to end my hours of headache.

Was passing "telegram.send_message() integration test" with markdown (note the single underscore) Changing to "telegram.send_message() integration test" (quoted with backtick) resolved my problem. Thank you so much!

JGMEYER avatar Jul 04 '20 14:07 JGMEYER

In my case it was needed to escape:

function toEscapeMSg(str: string): string {
    return str
        .replace(/_/gi, "\\_")
        .replace(/-/gi, "\\-")
        .replace("~", "\\~")
        .replace(/`/gi, "\\`")
        .replace(/\./g, "\\.");
}

CruiseMan avatar Apr 04 '21 05:04 CruiseMan

In my case it was needed to escape:

function toEscapeMSg(str: string): string {
    return str
        .replace(/_/gi, "\\_")
        .replace(/-/gi, "\\-")
        .replace("~", "\\~")
        .replace(/`/gi, "\\`")
        .replace(/\./g, "\\.");
}

Hi CruiseMan! Could you please help me with this? I tried to add these lines on Telegram script (Admin > Media types) but no success. Now I get this error: "SyntaxError: parse error (line 8)".

I'm using HTML parse, characters "<" and ">" are causing the "Sending failed: Bad Request: can't parse entities: Unsupported start tag "" at byte offset" error, so I need to escape them somehow.

Thanks!

avmagrini avatar Apr 12 '21 23:04 avmagrini

You need to add

.replace(/\</g, "\\<")
.replace(/\>/g, "\\>");

CruiseMan avatar Apr 12 '21 23:04 CruiseMan

I copied your full function and still get the error. Am I correct in adding these lines to the Telegram script in Media Types? Is there a specific position where these lines are to be added?

Thank you!

avmagrini avatar Apr 12 '21 23:04 avmagrini

  1. What is full function looks like ?
  2. What is your string you trying to escape looks like ?

CruiseMan avatar Apr 12 '21 23:04 CruiseMan

  1. Do you mean the whole script? It's the oficial script from Zabbix 5.2, the only modification was the function you suggested:

`var Telegram = { token: null, to: null, message: null, proxy: null, parse_mode: null,

export function toEscapeMsg(str: string): string { return str .replace(//gi, "\") .replace(/-/gi, "\-") .replace("~", "\~") .replace(//gi, "\\") .replace(/./g, "\.") .replace(/|/g, "\|"); }

sendMessage: function() {
    var params = {
        chat_id: Telegram.to,
        text: Telegram.message,
        disable_web_page_preview: true,
        disable_notification: false
    },
    data,
    response,
    request = new CurlHttpRequest(),
    url = 'https://api.telegram.org/bot' + Telegram.token + '/sendMessage';

    if (Telegram.parse_mode !== null) {
        params['parse_mode'] = Telegram.parse_mode;
    }

    if (Telegram.proxy) {
        request.SetProxy(Telegram.proxy);
    }

    request.AddHeader('Content-Type: application/json');
    data = JSON.stringify(params);

    // Remove replace() function if you want to see the exposed token in the log file.
    Zabbix.Log(4, '[Telegram Webhook] URL: ' + url.replace(Telegram.token, '<TOKEN>'));
    Zabbix.Log(4, '[Telegram Webhook] params: ' + data);
    response = request.Post(url, data);
    Zabbix.Log(4, '[Telegram Webhook] HTTP code: ' + request.Status());

    try {
        response = JSON.parse(response);
    }
    catch (error) {
        response = null;
    }

    if (request.Status() !== 200 || typeof response.ok !== 'boolean' || response.ok !== true) {
        if (typeof response.description === 'string') {
            throw response.description;
        }
        else {
            throw 'Unknown error. Check debug log for more information.'
        }
    }
}

}

try { var params = JSON.parse(value);

if (typeof params.Token === 'undefined') {
    throw 'Incorrect value is given for parameter "Token": parameter is missing';
}

Telegram.token = params.Token;

if (params.HTTPProxy) {
    Telegram.proxy = params.HTTPProxy;
} 

if (['Markdown', 'HTML', 'MarkdownV2'].indexOf(params.ParseMode) !== -1) {
    Telegram.parse_mode = params.ParseMode;
}

Telegram.to = params.To;
Telegram.message = params.Subject + '\n' + params.Message;
Telegram.sendMessage();

return 'OK';

} catch (error) { Zabbix.Log(4, '[Telegram Webhook] notification failed: ' + error); throw 'Sending failed: ' + error + '.'; }`

  1. All triggers that have "<" and ">" symbols in their name. I noticed the problem and tested with the trigger: "Server has been restarted (uptime < 10m)". If I manually remove the symbol "<", the message is sent with success.

avmagrini avatar Apr 12 '21 23:04 avmagrini

You still need to add https://github.com/irazasyed/telegram-bot-sdk/issues/493#issuecomment-818308379 to you escape function, I don't see it here in your code. Don't do stupid copy paste, think a bit.

CruiseMan avatar Apr 12 '21 23:04 CruiseMan

Sure, I know that. Sorry if I didn't make myself clear. As I had already done this and had the mentioned error, i tried to copy exactly as you indicated to eliminate the possibility that it was my fault. As soon as it worked, I would add the lines with the symbols I need to escape. Except this, did I add the function correctly?

avmagrini avatar Apr 13 '21 00:04 avmagrini