cmd410 / OrigamiBot

A pythonic Telegram bot API library
MIT License
75 stars 5 forks source link

Markup submodule support #26

Closed SKY-ALIN closed 2 years ago

SKY-ALIN commented 2 years ago

Hello! I want to table a proposal to add a special submodule to generate Telegram-specific markup easily.

Motivation

A common markdown format that every programmer uses is really simple and doesn’t require special utils for that, but MarkdownV2 and HTML which let include spoiler, strikethrough and other telegram innovations are different. For example, MarkdownV2 also requires to escape 18 characters, and if it’s not, reject the request. This may make the task of designing markup generation for bots that include business logic complicated enough and I suppose it’s better to have a ready-made out-of-the-box solution for this.

Implementation suggestion

General explanation

I have recently worked on a related challenge and I’m writing this to propose to re-use my solution. It’s the telegram-text module. The general structure in the context of the origamibot module I propose to implement is the following:

Why telegram-text module?

Example of the module:

from telegram_text import Bold, Italic, Underline

text = Underline(Bold("Bold") + "and" + Italic("italic") + "with underline.")

Usage example in the origamibot context

from origamibot import OrigamiBot as Bot
from origamibot.listener import Listener
from origamibot.text import Spoiler, InlineCode, PlainText

TOKEN: str

class BotsCommands:
    def __init__(self, bot: Bot):
        self.bot = bot

    def echo(self, message, value: str):
        self.bot.send_message(message.chat.id, Spoiler(value))  # Spoiler text

class MessageListener(Listener):
    def __init__(self, bot: Bot):
        self.bot = bot

    def on_command_failure(self, message, err=None):
        if err is None:
            self.bot.send_message(message.chat.id, 'Command failed to bind arguments!')  # A std str object
        else:
            self.bot.send_message(message.chat.id, PlainText('Error in command:') + InlineCode(err))  # Combined plain and code text

if __name__ == '__main__':
    bot = Bot(TOKEN)

    bot.add_listener(MessageListener(bot))
    bot.add_commands(BotsCommands(bot))
    bot.start()

    while True:
        pass
SKY-ALIN commented 2 years ago

Cool! 😎👍