An experimental syntax highlighter web app bot based on Telegram's WebApp update.

Overview

Syntax Highlighter WebApp

Inspired by zubiden/tg-web-bot-demo.

Try the demo bot running here: @syntaxyybot

Recently Telegram released a big update for bots: Web Apps/Bots. This is a sample Web App written in TypeScript and Deno.

Read more about the Web Apps update

Built using

  1. highlight.js — Core part of this bot. Syntax highlights code elements in the WebApp. Also, highlightjs-line-numbers.js is used with highlight.js for line number support in the pre-code elements.
  2. grammY — The Telegram Bot framework.
  3. Oak — A middleware framework for handling HTTP with Deno.
  4. pieroxy/lz-string — LZ-based compression algorithm for JavaScript. This bot uses some functionalities of this library for compressing the code block strings to URI encoded strings like Prettier Playground do.

What this bot can do?

This bot can syntax Highlight code blocks in messages. Code blocks are considered as pre or multiline code entities. It replies with a button to open webapp which shows you the highlighted preview.

I initially made this bot for testing the WebApp thingy. If you want to use a stable syntax highlighting bot, see dcdunkan/syntax-highlighter-bot.

I am not gonna work on this bot much, because this bot is supposed to be used in developer group chats, but Telegram currently does not allow these WebApp buttons to be sent in groups. It's only for private chats. So, until Telegram supports it, this bot is useless, and it will be just a waste of time.

How it works?

It's not that complicated :)

bot.ts: Bot listens for any message containing pre or code entity. Filters them into pre and multiline code. Then takes it's text maps them into an array of code blocks, and compress it to URI encoded string. It is appended as the URL hash. Then a web app button with that URL is sent.

router.ts: When the web app button is clicked, —URL is opened— router path responds with the index.html file.

index.html: When this page is requested, it gets the hash from the URL of the page, and decompresses that string back into array of code blocks. Each string will get appended to a pre-code element. Then highlight.js and the line number plugin will do their job.

Additionally, when the page is requested, it also validates the data that Telegram sent to the page. For this bot, it's not required to do though. Maybe I'll just remove it later.

Setup

Environment Variables

  • BOT_TOKEN - Bot token. You can get one from BotFather.
  • SITE_URL - Server URL. If you're using Deno Deploy, use app's url. Or, if you're running locally, you may use the HTTPS forwarding URL by ngrok or something.

Deno Deploy

The working demo bot, @syntaxyybot is running on Deno Deploy.

Click here to deploy bot on Deno Deploy.

Running Locally

  • Make sure you have Deno CLI installed.

  • Clone the repository.

    git clone https://github.com/dcdunkan/tg-webapp-syntax-highlighter.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 mod.ts

    Required permissions

    • --allow-net - To communicate with Telegram servers and receive updates.
    • --allow-env - To access environment variables.
    • --allow-read - To read static files.

If everything is done correct, you should see "[server] listening..." and "[bot] (Username) is up" in your console.

Node.js?

No, but yes. Dependencies are equally available for Node.js too. Change the imports/exports. And in env.ts, just change the Deno.env.toObject() to process.env. That should work.


MIT License

You might also like...

TVMAZE API-based webapp, receives TV show episodes with all data about that TV show.

TVMAZE API-based webapp, receives TV show episodes with all data about that TV show.

TVMAZE API-based webapp, receives TV show episodes with all data about that TV show. The Webapp has two interfaces: A home page, showing a list of Tv show episodes you can like. And a popup window with more data about the TV show that you can comment on.

Aug 9, 2022

A web app designed to keep track of activities that are done and those that are and not done. Users can add, delete, mark as completed and update the activities. Built with Javscript, html, css and webpack.e your activites

ToDoListApp A web app designed to help web keep track of activities that are done and those that are still pending. Users can add, delete, mark as com

Dec 23, 2022

Timetable app (webapp) for SLIIT students of all years

Timetable app (webapp) for SLIIT students of all years

Build status: Timetable App (for SLIIT students) This web app is made for SLIIT students of all years and all specializations. The timetables of most

Nov 26, 2022

experimental web browser optimized for rabbit-holing

experimental web browser optimized for rabbit-holing

Cartographist Cartographist is an experimental web browser optimized for rabbit-holing. Instead of opening new windows (with cmd-click), Cartographist

Jan 2, 2023

A WebApp to make Obsidian Clippers or Bookmarklets - to capture webpages or data from the web

A WebApp to make Obsidian Clippers or Bookmarklets - to capture webpages or data from the web

A WebApp to make Obsidian Clippers or Bookmarklets - to capture webpages or data from the web

Dec 4, 2022

A WebApp to preview FTML, the SCP Foundation's markup language, on the Web.

A WebApp to preview FTML, the SCP Foundation's markup language, on the Web.

Wikitext Previewer (FTML/Wikidot Web Previewer) A WebApp to preview FTML, the SCP Foundation's markup language, on the Web. "SCP-173" by Moto42, from

Jun 4, 2022

A small javascript DOM manipulation library based on Jquery's syntax. Acts as a small utility library with the most common functions.

Quantdom JS Quantdom is a very small (about 600 bytes when ran through terser & gzipped) dom danipulation library that uuses a Jquery like syntax and

Aug 16, 2022

Markdoc is a Markdown-based syntax and toolchain for creating custom documentation sites and experiences.

Markdoc is a Markdown-based syntax and toolchain for creating custom documentation sites and experiences.

A powerful, flexible, Markdown-based authoring framework. Markdoc is a Markdown-based syntax and toolchain for creating custom documentation sites and

Jan 2, 2023
Comments
Owner
Dunkan
console.log(3446)
Dunkan
My terrible attempt at a promposal. Update: She said yes LMFAO Update Update: I got friendzoned right after 😭

TypeScript Next.js example This is a really simple project that shows the usage of Next.js with TypeScript. Deploy your own Deploy the example using V

John Li (Tet) 7 Oct 27, 2022
✏️ 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
Sachit Yadav 6 Nov 3, 2022
Smooth mobile touch slider for Mobile WebApp, HTML5 App, Hybrid App

iSlider iSlider is a lightweight, high-performant, no library dependencies cross-platform slide controller. It can help handling most sliding effects,

Baidu BEFE 1.7k Nov 25, 2022
Experimental proxy and wrapper for safely embedding Web Archives (warc.gz, wacz) into web pages.

warc-embed-netlify ??️ Experimental proxy and wrapper for safely embedding Web Archives (.warc.gz, .wacz) into web pages. This particular implementati

Harvard Library Innovation Laboratory 6 Sep 1, 2022
GitHub and Markdown-Based CMS for Blogs. EXPERIMENTAL and in the "Idea" stage. I have no clue if this is feasible.

Turborepo starter This is an official pnpm starter turborepo. What's inside? This turborepo uses pnpm as a package manager. It includes the following

David Taing 2 Oct 13, 2022
An experimental webkit-based kernel exploit (Arb. R/W) for the PS5 on 4.03 & 4.50FW

PS5 4.xx Kernel Exploit Summary This repo contains an experimental WebKit ROP implementation of a PS5 kernel exploit based on TheFlow's IPV6 Use-After

Kameleon 5 Nov 17, 2022
This an CRUD app built were users can create, update and delete specific day to day tasks as they wish. The app is built using webpack and served with webpack dev server

Todo-List This an CRUD app built were users can create, update and delete specific day to day tasks as they wish. The app is built using webpack and s

Duane David 10 Sep 28, 2022