An open API service indexing awesome lists of open source software.

https://github.com/tishka17/sulguk

HTML to telegram entities converter
https://github.com/tishka17/sulguk

html sulguk telegram

Last synced: 3 months ago
JSON representation

HTML to telegram entities converter

Awesome Lists containing this project

README

          

Sulguk - HTML to telegram entities converter
================================================

[![PyPI version](https://badge.fury.io/py/sulguk.svg)](https://badge.fury.io/py/sulguk)
[![downloads](https://img.shields.io/pypi/dm/sulguk.svg)](https://pypistats.org/packages/sulguk)
[![license](https://img.shields.io/github/license/Tishka17/sulguk.svg)](https://github.com/Tishka17/sulguk/blob/master/LICENSE)

Need to deliver formatted content to your bot clients?
Having a hangover after trying to fit HTML into telegram?
Beautifulsoup is too complicated and not helping with messages?

Try `sulguk` (술국, a hangover
soup) - [delivered since 1800s](https://en.wikipedia.org/wiki/Food_delivery).

## Problem

Telegram supports `parse_mode="html"`, but:

* Telegram processes spaces and new lines incorrectly. So we cannot format HTML source for more readability.
* Amount of supported tags is very low
* It does not ignore additional attributes in supported tags.

Let's imagine we have HTML like this:

```html
This is a demo of Sulguk

Underlined
Italic
Bold
```

This is how it is rendered in browser (expected behavior):

![](https://github.com/tishka17/sulguk/blob/master/images/problem_browser.png?raw=True)

But this is how it is rendered in Telegram with `parse_mode="html"`:

![](https://github.com/tishka17/sulguk/blob/master/images/problem_telegram.png?raw=True)

To solve this we can convert HTML to telegram entities with `sulguk`. So that's how it looks now:

![](https://github.com/tishka17/sulguk/blob/master/images/problem_sulguk.png?raw=True)

## Example

1. Create your nice HTML:

```html


  1. some item

  2. other item


Some text in a paragraph


```

2. Convert it into text and entities

```python
result = transform_html(raw_html)
```

3. Send it to telegram.

Depending on your library you may need to convert entities from dict into
proper type

```python
await bot.send_message(
chat_id=CHAT_ID,
text=result.text,
entities=result.entities,
)
```

## Example for aiogram users

1. Add `SulgukMiddleware` to your bot

```python
from sulguk import AiogramSulgukMiddleware

bot.session.middleware(AiogramSulgukMiddleware())
```

2. Create your nice HTML:

```html


  1. some item

  2. other item


Some text in a paragraph


```

3. Send it using `sulguk` as a `parse_mode`:

```python
from sulguk import SULGUK_PARSE_MODE

await bot.send_message(
chat_id=CHAT_ID,
text=raw_html,
parse_mode=SULGUK_PARSE_MODE,
)
```

## Supported tags:

For all supported tags unknown attributes are ignored as well as unknown classes.
Unsupported tags are raising an error.

#### Standard telegram tags (with some changes):
* `` - a hyperlink with `href` attribute
* ``, `` - a bold text
* ``, `` - an italic text
* ``, ``, `` - a strikethrough text
* ``, `` - an underlined text
* `` - an inline element with optional attribute `class="tg-spoiler"` to make a spoiler
* `` - a telegram spoiler
* `

` with optional `class="language-"` - a preformatted block with code. `` will be sent as a language attribute in telegram.

* `` - an inline preformatted element.
* `` - rendered as an expandable blockquote
* `` - treated as a paragraph, typically used as first child of `` for the heading

**Note:** In standard Telegram HTML you can set a preformatted text language nesting `` in `

` tag. This works when it is an only child. But any additional symbol outside of `` breaks it.

The same behavior is supported in sulguk. Otherwise, you can set the language on `
` tag itself.

#### Additional tags:
* `
` - new line
* `


` - horizontal line
* `` - word break opportunity
* `

    ` - unordered list
    * `
      ` - ordered list with optional attributes
      * `reversed` - to reverse numbers order
      * `type` (`1`/`a`/`A`/`i`/`I`) - to set numbering style
      * `start` - to set starting number
      * `
    1. ` - list item, with optional `value` attribute to change number. Nested lists have
      indentation
      * `
      ` - a block (not inline) element
      * `

      ` - a paragraph, emphasized with empty lines
      * `` - a quoted text
      * `

      ` - a block quote. Like a paragraph with indentation
      * `
      ` - a block quote with expandable
      * `

      `-`

      ` - text headers, styled using available telegram options
      * `` - contents is shown as not scripting is supported
      * ``, `` - italic
      * ``, `` are rendered using emoji (🟩🟩🟩🟨⬜️⬜️)
      * ``, `` - preformatted text
      * `` - as a link with picture emoji before. `alt` text is used if provided.
      * `` - as italic text
      * `` - as symbols ✅⬜️(checkbox), 🔘⚪️(radio) or `_______`/value in other cases

      #### Tags which are treated as block elements (like `

      `):
      ``, ``, ``, ``, ``

      #### Tags which are treated as inline elements (like ``):
      ``, ``, ``, ``, `

      #### Tags which contents is ignored:

      ``, ``, ``, ``, `<style>`, `<template>`, `<title>`

      ## Command line utility for channel management

      1. Install with addons
      ```shell
      pip install 'sulguk[cli]'
      ```

      2. Set environment variable `BOT_TOKEN`

      ```shell
      export BOT_TOKEN="your telegram token"
      ```

      3. Send HTML file as a message to your channel. Additional files will be sent as comments to the first one. You can provide a channel name or a public link

      ```shell
      sulguk send @chat_id file.html
      ```

      4. If you want to, edit using the link from shell or from your tg client. Edition of comments is supported as well.

      ```shell
      sulguk edit 'https://t.me/channel/1?comment=42' file.html
      ```