Skip to content

Latest commit

Β 

History

History
89 lines (74 loc) Β· 2.89 KB

File metadata and controls

89 lines (74 loc) Β· 2.89 KB

Telegram Markdown Parsing Error Fix

Problem Solved

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

Root Cause

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:

  1. Task titles and usernames - Could contain special Markdown characters (*, _, [, ], etc.)
  2. The text .env - The dot in ".env" was causing parsing issues in the message

Solution Implemented

1. Created Markdown Utility (src/bot/utils/markdown.js)

export function escapeMarkdown(text) {
  if (!text) return text;
  return text.toString().replace(/[_*[\]()~`>#+\-=|{}.!\\]/g, '\\$&');
}

This function escapes all special Markdown characters to prevent parsing errors.

2. Updated tasks.js

  • Imported the escapeMarkdown utility
  • Applied escaping to all user-generated content:
    • Task titles
    • Usernames
    • Quest titles
    • The .env text in messages

Changes Made:

// 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`;

Files Modified

  1. βœ… src/bot/utils/markdown.js - Created new utility
  2. βœ… src/bot/commands/tasks.js - Applied escaping to user data
  3. βœ… src/bot/commands/auth.js - Fixed URL issues
  4. βœ… .env - Added FRONTEND_URL documentation

Testing

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

What Characters Are Escaped

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)

Benefits

  1. πŸ›‘οΈ Prevents crashes - No more Telegram API errors from malformed Markdown
  2. πŸ”’ Secure - User input can't break message formatting
  3. πŸ“ Better UX - Users can use any characters in task names without issues
  4. βœ… Reliable - Messages always display correctly

Status

βœ… Fixed and Working!

The bot now handles all user-generated content safely and displays messages without Markdown parsing errors.