The Telegram bot was throwing this error:
Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 323
Telegram's Markdown parser is very strict and can fail when user-generated content or certain text patterns contain special characters that aren't properly escaped. The specific issues were:
- Task titles and usernames - Could contain special Markdown characters (*, _, [, ], etc.)
- The text
.env- The dot in ".env" was causing parsing issues in the message
export function escapeMarkdown(text) {
if (!text) return text;
return text.toString().replace(/[_*[\]()~`>#+\-=|{}.!\\]/g, '\\$&');
}This function escapes all special Markdown characters to prevent parsing errors.
- Imported the
escapeMarkdownutility - Applied escaping to all user-generated content:
- Task titles
- Usernames
- Quest titles
- The
.envtext in messages
// Before (could cause errors)
message += `π΄ ${task.title}\n`;
message += `π€ Assigned to: ${task.assignedTo?.username}\n`;
message += `π‘ Configure FRONTEND_URL in your .env to access the web dashboard`;
// After (safe from Markdown errors)
message += `π΄ ${escapeMarkdown(task.title)}\n`;
message += `π€ Assigned to: ${escapeMarkdown(task.assignedTo?.username)}\n`;
message += `π‘ Configure FRONTEND_URL in your ${escapeMarkdown('.env')} to access the web dashboard`;- β
src/bot/utils/markdown.js- Created new utility - β
src/bot/commands/tasks.js- Applied escaping to user data - β
src/bot/commands/auth.js- Fixed URL issues - β
.env- Added FRONTEND_URL documentation
Now when you use /tasks in Telegram:
- β Admin dashboard shows without errors
- β Task titles with special characters display correctly
- β Usernames with special characters work fine
- β All Markdown formatting renders properly
The following special Markdown characters are now automatically escaped:
_(underscore)*(asterisk)[](brackets)()(parentheses)~(tilde)`(backtick)>(greater than)#(hash)+(plus)-(minus)=(equals)|(pipe){}(curly braces).(dot)!(exclamation)\(backslash)
- π‘οΈ Prevents crashes - No more Telegram API errors from malformed Markdown
- π Secure - User input can't break message formatting
- π Better UX - Users can use any characters in task names without issues
- β Reliable - Messages always display correctly
β Fixed and Working!
The bot now handles all user-generated content safely and displays messages without Markdown parsing errors.