telegram-bot-sdk
telegram-bot-sdk copied to clipboard
Bad Request: can't parse entities in message text
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?
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...
Removing of 3 signs <<`*_>> from text solved my problem.
p.s. parse_mode='markdown'
@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!
In my case it was needed to escape:
function toEscapeMSg(str: string): string {
return str
.replace(/_/gi, "\\_")
.replace(/-/gi, "\\-")
.replace("~", "\\~")
.replace(/`/gi, "\\`")
.replace(/\./g, "\\.");
}
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!
You need to add
.replace(/\</g, "\\<")
.replace(/\>/g, "\\>");
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!
- What is full function looks like ?
- What is your string you trying to escape looks like ?
- 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 + '.'; }`
- 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.
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.
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?