Home

Awesome

Syntax Highlighter Bot

Source code of @syntaxybot.

Inspired by Piterden/syntax-highlighter-bot.

Minimal syntax highlighting bot for Telegram. Use it in private chats or add to group chats. Send text inside three backticks, or any message containing pre or multiline code entities, and the bot will reply you with syntax highlighted images of that piece of code. Useful in Development groups.

Try the running bot here: Syntax Highlighter Bot 🚀

Written in TypeScript and grammY and runs on Deno.

Built Using

Thanks to these tools and libraries.

  1. highlight.js — Syntax highlighting for the Web. Behind-the-scenes of this bot.
  2. svg2png-wasm — SVG to PNG converter JS library made with WASM + resvg (no native dependencies). Used for generating PNG images from custom made SVG.
  3. grammY — The Telegram Bot Framework.
  4. Deta.sh Base — Free and unlimited Cloud Database service.

puppeteer had a great place in the history of most of the syntax highlighter bots including this one. So, thank you. (Since v0.5.0 we don't use it anymore).

Puppeteer — Puppeteer is a library which provides a high-level API to control Chrome, Chromium, or Firefox Nightly over the DevTools Protocol. Also a core part of this bot, used for generating syntax highlighted images.

You can still access the puppeteer edition from the puppeteer branch and host it if you want to - (not maintained)

Features

Syntax Highlighting

Syntax Highlighting for almost 200 languages with automatic language detection - power of highlight.js!

<a id="language-detection" href="#language-detection"></a> Language Detection: Tries to detect and use the detected language for more accurate results.

NOTE: The <code> should be a <samp>pre</samp> formatted code block could be a <samp>pre</samp> entity formatted code block, or a multiline <samp>code</samp> entity.

🎨 Customization

<a id="custom-theming" href="#custom-theming"></a> Custom theming for the images. Use the <samp>/theme</samp> command to set any theme from this list. See themes in action here.

<a id="multiple-fonts" href="#multiple-fonts"></a> Multiple fonts support. See the <samp>/font</samp> command in chat for the list of available fonts. Add a font that you like by putting them in the assets/fonts/ and opening a PR.

<a id="as-documents" href="#as-documents"></a> Send images as documents. Sometimes long code might make the image blurry due to the default Telegram image compression. Sending them as documents fixes the issue. Use either <samp>/as_doc</samp> or <samp>/as_document</samp> command.

Better User Experience

<a id="forced-highlighting" href="#forced-highlighting"></a> Forced Highlighting: Replying <samp>/highlight</samp> or <samp>/hl</samp> to a message containing text or caption, will

<h4 id="forced-highlighting-optional-args" href="#forced-highlighting-optional-args">Optional Arguments</h4>

You can optionally pass arguments separated by commas or white spaces. The accepted arguments are integers corresponding to the position of the pre/code entity in the message. Starting from 1. See the example below.

Passing w or no-wrap, or nw will highlight the text without wrapping it. The image will scale to the maximum content length. It is useful when highlighting some terminal logs, etc.

Introduced in <kbd>v0.4.0</kbd>

You can also pass any of 0, full, f to get the whole message highlighted. (why?: If you ever need to highlight the full message which contains pre/code entities).

Take this message as an example:

Lorem ipsum <code (inline)> dolor sit amet.

<code (multi line)>

Nunc in ligula vehicula quam efficitur vehicula at lacinia erat.

<pre>

Now, replying,

NOTE: <samp>/hl</samp> is the same as <samp>/highlight</samp>. It's just a short form.

NOTE: /hl 0 1 only highlights the full message; not both full message and 1st pre/code entity.

<a id="toggle-auto-shl" href="#toggle-auto-shl"></a> Toggle Automatic Syntax Highlighting: You can disable auto syntax highlighting by using the <samp>/toggle_auto_hl</samp> command. (Use the same command to re-enable it). You don't always need the bot to highlight even the small codeblocks. So, when you need the highlighting, you can force it to highlight the message/code blocks. Checkout the "Forced Highlighting" feature.

Introduced in <kbd>v0.3.0</kbd> • See grammyjs#57178.

<a id="stats" href="#stats"></a> Stats: Not a very useful feature, I know. Use <samp>/stats</samp> command to find how many times the bot has sent syntax highlighted images for you.

"Maybe" features that I'd like to add if possible.

Setup

Running Locally

Make sure you have installed Deno CLI.

Clone the repository.

git clone https://github.com/dcdunkan/syntax-highlighter-bot.git

Go to to the cloned repository folder. Create a .env file and set environment variables like in example.env.

Run the bot using the command below.

deno run --allow-net --allow-env --allow-read local.ts

Required permissions

If everything is done correct, you should see "(Username) started" in your console.

Deploy to Deno Deploy

The working bot, @syntaxybot is currently deployed on Deno Deploy free account. It's pretty easy to setup.

Click here to deploy to Deno Deploy.

If you're having issues with deploying using the above link, try forking this repository and deploying from Deno Deploy Dashboard by connecting to the forked repository.

After deploying you will get a link to your application, in the format https://<appname>.deno.dev.

Open browser and go to the link down below.

https://api.telegram.org/bot<BOT_TOKEN>/setWebhook?url=<APP_URL>/<BOT_TOKEN>&drop_pending_updates=true

This will set the bot's webhook to the deployed application, so Telegram will send updates there and it will be able to handle them there.

Environment Variables

VariableRequired?Description
BOT_TOKENYes.The API token of the Bot. Chat with https://t.me/BotFather to get one.
DETA_KEYYes.Project Key of Deta.sh Project. Sign up and create a project at https://deta.sh.

Translating

If you like to translate this bot into your language, please follow the English translation file. This project uses Fluent for localization.

Contributing

Feel free to contribute! And if you are having issues or if you want suggest something, please open an issue here: dcdunkan/syntax-highlighter-bot/issues. Or, open a PQ!

<h3 align="center">2 🦕 22</h3>