Syntax Highlighter bot for Telegram.

Overview

🖍️ 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 private chat 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. 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.
  3. grammY — The Telegram Bot Framework.
  4. 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.

    • 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.

      Additionally, 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.
  • 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.

  • Forced /highlighting by replying to a message - if the message contains pre code blocks, highlight them in the usual way. If not, highlight the whole message.
  • Highlight only if the code block contains more than x number of characters. It would be a mess if someone use three backticks instead of one backtick, even for a single monospace word.
  • Automatically toggle "Send as Document" mode if there is more than x number of characters.

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.

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

You might also like...

Bot para enviar lembretes no Telegram para escalação do seu time no fantasy game Cartola

Bot para enviar lembretes no Telegram para escalação do seu time no fantasy game Cartola

©️ E aí, cartoleiro, já escalou seu time no Cartola hoje? Não deixa para última hora! como eu Entre aqui no grupo do Telegram agora mesmo ;) O que é o

Nov 23, 2022

Simple time-aware report bot for Telegram.

report bot Built using grammY. Simple time-aware report bot for Telegram. It listens for /report, /admin commands or @admin, @admins mentions in group

Oct 2, 2022

A Telegram bot which generates your intro video programmatically 📽️

A Telegram bot which generates your intro video programmatically 📽️

Features 🪶 Generate videos programmatically from telegram input High performance High quality videos Installation 📦 Installation is pretty easy, cli

Sep 6, 2022

esse bot envia sinais, do gamer double blaze, direto para chats do telegram. leave the star ⭐️

esse bot envia sinais, do gamer double blaze, direto para chats do telegram. leave the star ⭐️

Bot Blaze Double A blaze.com, site de aposta online, operada pela empresa Prolific Trade N.V. e bastante popular nas mídias sociais. Em um de seus jog

Dec 30, 2022

This is a bot for Telegram that can be used to receive webhook notifications from Gitlab. It is built with the Next.js API.

This is a bot for Telegram that can be used to receive webhook notifications from Gitlab. It is built with the Next.js API.

Telegram Bot for Gitlab Webhook 🦉 Note This project is still in development. Just relax 🙂 This is a bot for Telegram that can be used to receive web

Oct 22, 2022

Telegram BOT For TikTok/Douyin downloader (TikTok video downloader without watermark)

TikDo Telegram BOT This is BOT Telegram downloader TikTok/Douyin. Download videos without watermark by pasting share link in send message. How to depl

Dec 1, 2022

A refined tool for exploring open-source projects on GitHub with a file tree, rich Markdown and image previews, multi-pane multi-tab layouts and first-class support for Ink syntax highlighting.

A refined tool for exploring open-source projects on GitHub with a file tree, rich Markdown and image previews, multi-pane multi-tab layouts and first-class support for Ink syntax highlighting.

Ink codebase browser, "Kin" 🔍 The Ink codebase browser is a tool to explore open-source code on GitHub, especially my side projects written in the In

Oct 30, 2022
Comments
  • Better images: Try using dom-to-image like image rendering than taking screenshots

    Better images: Try using dom-to-image like image rendering than taking screenshots

    Note https://github.com/dcdunkan/highlight-render Has a potential to become the solution for this issue. It also can be deployed on Deno Deploy. So...

    Use libraries like tsayen/dom-to-image to render syntax highlighted code images like carbon-app/carbon does. From live usecases, I found (it seems like) that it provides more good quality and fast image rendering. Especially, if we use this method, we can stop using puppeteer, and probably transfer the bot from Heroku to Deno Deploy and make the image process even more faster. Have to work on the HTML side with canvases and stuff, which I am not good at :(

    Open a PR, if you like to contribute.

    Edit:

    1. html2canvas

    Nope. Nothing works as I tried. Now have to try rendering them server side with some other tools. Or, parse the html and map, and create an image? I don't know :(

    enhancement 
    opened by dcdunkan 2
  • feat(core): better image rendering + no puppeteer + deno deploy

    feat(core): better image rendering + no puppeteer + deno deploy

    This is an approach I taken to bring Deno Deploy support and to stop using puppeteer-screenshot thingy for highlighted image rendering. While the puppeteer way is easier to implement, it is not very effiecient. So I did this! (it was a lot of work though)

    It uses svg2png-wasm which is available for Deno and can be used in Deno Deploy environment. The SVG is generated manually. Still uses highlight.js for vast support of languages and themes. The generated html is then passed to linkedom, a lightweight and simple HTML to DOM parser library. Then traverse through the highlighted span elements, apply some colors and text styles by reading the provided theme CSS string. Pass the generated SVG and some fonts to the svg2png library, and there we have a nice highlighted PNG output.

    You can see https://github.com/dcdunkan/highlight-render for the previous & current attempts.

    Idea

    • https://t.me/grammyjs/57189
    • https://t.me/grammyjs/57191
    • https://t.me/grammyjs/57194

    Related to #1

    opened by dcdunkan 0
  • feat: Toggle automatic syntax highlighting.

    feat: Toggle automatic syntax highlighting.

    Enable/disable automatic highlighting using the /toggle_auto_hl command. You can still force the bot to highlight something by replying /hl to a message.

    opened by dcdunkan 0
  • Move to Shiki

    Move to Shiki

    highlight.js is good for detecting languages (not perfect, but not bad). But highlight.js sucks at its highlighting part. Shiki, which is an perfect alternative, can be used for making highlighting much better.

    Shiki: https://github.com/shikijs/shiki

    I'm not sure if Shiki works on Deno or not. If it does not, we should port it :hehe:

    opened by dcdunkan 2
Owner
Dunkan
console.log(3446)
Dunkan
✏️ Super lightweight JSX syntax highlighter, around 1KB after minified and gzipped

Sugar High Introduction Super lightweight JSX syntax highlighter, around 1KB after minified and gzipped Usage npm install --save sugar-high import { h

Jiachi Liu 67 Dec 8, 2022
Syntax Highlighter supporting multiple languages, themes, fonts, highlighting from a URL, local file or post text.

Crayon Syntax Highlighter Supports multiple languages, themes, fonts, highlighting from a URL, local file or post text. Written in PHP and jQuery. Cra

Aram Kocharyan 1.1k Nov 26, 2022
A beautiful Syntax Highlighter.

Shiki Shiki is a beautiful Syntax Highlighter. Demo. Usage npm i shiki const shiki = require('shiki') shiki .getHighlighter({ theme: 'nord' }

Shiki 4.2k Dec 31, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Github follower notifier via telegram bot

⚡ GitHub Follower Notifier ?? ⚡ Vars: ?? FEED: Go Here to get yours. DATABASE: Go HERE to create one. After creating Instance: Go to your Instance Go

Gautam Kumar 14 Nov 25, 2021
@TGMusicfy - Minimalistic Telegram music search bot written in TypeScript and based on Telegraf and Express JS.

@TGMusicfy Go to bot Deployed thanks to Heroku and New-Relic Bots are special Telegram accounts designed to handle messages automatically. Users can i

Saveliy Andronov 5 Sep 13, 2022
Telegram bot to attend Google Meet sessions on behalf of you :D

BonkMeet Telegram bot to attend Google Meet sessions on behalf of you :D How to use? You will need these 3 tokens: Bot Token from BotFather Owner's te

bunny 11 Dec 26, 2022
Holy Qur'an Telegram Bot with pagination and audio, in Indonesian language.

quran-telegram-bot Contributions are Welcome! Demo @fio_quran_bot Run Tested on Node.js v14.x.x with NPM v6.x.x, Windows 10 and Ubuntu 18.04.6 LTS. Cr

Rioze 5 Oct 21, 2022