🖍️ Syntax Highlighter Bot
Kind of a copy; highly inspired from
Piterden/syntax-highlighter-bot -
Telegram Bot here
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.
Written in TypeScript and
grammY and runs on Deno.
Built Using
Thanks to these tools and libraries.
- highlight.js — Syntax highlighting for the Web. Behind-the-scenes of
this bot.
- 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.
- grammY — The Telegram Bot Framework.
- Deta.sh Base — Free and unlimited Cloud Database service.
Features
(and usage)
-
🖍️ · Syntax
Highlighting for almost 200 languages with automatic language detection -
power of highlight.js!
-
🎨 · Custom theming for
the images. Use the /theme command to set any theme
from this list.
See themes in action here.
-
🗛 · Multiple fonts
support. See the /font command in chat for the list of
available fonts.
-
🗎 · 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 /as_doc or
/as_document command.
-
👀 · Language
Detection: Tries to detect and use the language for more accurate results.
bot.ts <code>
- Detects ts.ts <code>
- Detects ts.<code>
- Auto detection by highlight.js.
NOTE: The <code>
should be a pre formatted code block could
be a pre entity formatted code block, or a multiline
code entity.
-
🖌️ · Forced
Highlighting: Replying
/highlight or
/hl to a message containing text or caption, will
- check for
pre
and code
(multiline) entities and if there is any, only
highlights those as it normally do. Useful if the original message was
edited later. - If no
pre
or code
(multiline) entities were found, highlights the whole
message. Useful if you forgot to format them before sending.
Optional Arguments
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.
-
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: /hl is the same as
/highlight. It's just a short form.
/hl
will highlight <code (multi line)>
and <pre>
(Default)./hl 1
will only highlight the <code (inline)>
./hl 2
will only highlight the <code (multi line)>
./hl 3
will only highlight the <pre>
./hl 1 3
will highlight both <code (inline)>
and <pre>
./hl 0
or /hl f
or /hl full
will highlight the whole message.
NOTE: /hl 0 1
only highlights the full message; not both full message
and 1st pre
/code
entity.
-
🚫 · Toggle Automatic
Syntax Highlighting: You can disable auto syntax highlighting by using the
/toggle_auto_hl 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.
v0.3.0 • See gmy#57178.
-
📊 Stats: Not a very useful feature; use
/stats 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.
-
Clone the repository.
git clone https://github.com/dcdunkan/syntax-highlighter-bot.git
-
Change directory (cd
) to the cloned repository.
-
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 --allow-write --allow-run --unstable local.ts
Required permissions
- --allow-net - To communicate with Telegram servers and receive
updates.
- --allow-env - To access environment variables.
- --allow-read - To read translations,
styles and
.env file.
- --allow-write - Required by puppeteer for writing temp files.
- --allow-run - To run launch Google Chrome (via puppeteer)
If everything is done correct, you should see "(Username) started" in your
console.
Deploy to Heroku
The working bot, @syntaxybot is currently
deployed on Heroku free web dynos. It's pretty easy to
setup.
Click the button to deploy to Heroku.
Or
click
here
After deploying you will get a link to your application, in the format
https://<appname>.herokuapp.com/
.
Open browser and go to the link down below.
- Replace the
<BOT_TOKEN>
with your BOT_TOKEN
. - Replace
<APP_URL>
with the link to your application.
https://api.telegram.org/bot<BOT_TOKEN>/setWebhook?url=<APP_URL>
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
Variable | Required? | Description |
---|
BOT_TOKEN | Yes. | The API token of the Bot. Chat with https://t.me/BotFather to get one. |
DETA_KEY | Yes. | Project Key of Deta.sh Project. Sign up and create a project at https://deta.sh. |
License
MIT License. Copyright (c) 2022 dcdunkan (Dunkan)
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!
Translating
If you like to translate this bot into your language, please follow
the English translation file. This project uses
Fluent for localization.
Made with ❤️ and ☕