Skip to main content

A python library to generate navigation menus using Telegram Bot API

Project description

telegram_menu package

A python library to generate navigation menus using Telegram Bot API.

Base classes MenuMessage and AppMessage help defining
applications buttons to navigate in a message tree.

Features:

  • Menu navigation using tree structure, unlimited depth
  • Support for sending pictures, notifications, and polls
  • Session manager when multiple users connect to the same bot
  • Automatic deletion of messages when configurable timer has expired
  • Integration of markdown format + emojis

Installation

pip install telegram_menu

Getting Started

You first need to create a Telegram bot, then you can refer to sample code in tests\test_connection.py to run a complete use-case.

Following code block creates a Hello, World! message:

from telegram_menu import BaseMessage, SessionManager

API_KEY = "put_your_telegram_bot_api_key_here"

class StartMessage(BaseMessage):
    """Start menu, create all app sub-menus."""

    LABEL = "start"

    def __init__(self, navigation: NavigationManager) -> None:
        """Init StartMessage class."""
        super().__init__(navigation, StartMessage.LABEL)

    def content_updater(self) -> str:
        """Update message content."""
        return "Hello, world!"

SessionManager(API_KEY).start(StartMessage)

You can add any button in StartMessage, using self.add_button() method:

# run_and_notify() is a class method which runs something and returns a string as Telegram notification
self.add_button("Action", self.run_and_notify)

# new_menu is a class derived from MenuMessage or AppMessage, which will generate a new menu or app message
self.add_button("NewMenu", new_menu_app)

An application message can contain several inlined buttons, behavior is similar to MenuMessage buttons.

# run_and_notify() is a class method which execute any python function returning a string as Telegram notification
self.add_button("Action", self.run_and_notify)

# get_content() is a class method which generates some text to display, eventually with markdown formatting
self.add_button("Display content", self.get_content, ButtonType.MESSAGE)

# get_picture() is a class method which returns the path of a picture to display
self.add_button("Show picture", self.get_picture, ButtonType.PICTURE)

Structure

Classes in package telegram_menu are stored in 2 python files:

  • navigation.py: main interface, menu and message generation and management
  • models.py: menu and messages model, classes definition

Following class diagram describes all public interfaces:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telegram_menu-0.2.1.tar.gz (2.2 MB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page