Skip to main content

Python telegram API adapter for FastAPI and asyncio

Project description

A library for simultaneous operation of a large number of telegram bots with a single code base.

Python - telegram API adapter. Based in Pydantic model libraries

You need to implement a web interface for a webhook yourself. I recommend FastAPI.

pip install megabot

  1. Fill in the dispatcher tokens in a convenient way for you in the format dict(int(bot_id), str(token)
  2. Define callback, message, and command handlers
  3. Connect the dispatcher to processing incoming webhooks.
  4. Done!

The filtering of the callbacks was taken from aiogram, the syntax is identical

example main.py

from megabot.dispatcher import Dispatcher
from megabot.poster import MessageService
from megabot.utils.callback_data import CallbackData

menu_cat = CallbackData('cat', 'id') # Callback fabric 
message_service = MessageService()

async def hello_word(callback: CallbackQuery):
    print(callback.data)
    await message_service.callback_answer(self.dp.tokens[bot_id], callback.id)
    
dp = Dispatcher()

bot_id = 9***9
token = '9***9:AA*****AA'
dp.tokens[bot_id] = token

dp.register_handler_callback(hello_word, callback_filter=menu_cat.filter())

webhook router file:

from main import dp

async def data_router(self, bot_id: int, data: dict) -> NoReturn:
    if not(dp.tokens.get(bot_id, None)):
        dp.tokens[bot_id] = token

    await dp.message_router(bot_id, data)

for local testing: NOT FOR PROD!!! add to main.py

from megabot.poster import get_updates
offset = 0

async def check_updates():
    global offset
    while True:
        result, error = await get_updates(token, offset)
        if result:
            result_list = result['result']
            if len(result_list) > 0:
                print(result)
                offset = result_list[-1]['update_id']+1
            await dp.message_router(bot_id, result)
        else:
            print(error)
        await asyncio.sleep(1)


await def start_bot()
    asyncio.create_task(check_updates())

add start_bot() to main file web_hooks

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

megabot-0.0.6.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

megabot-0.0.6-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file megabot-0.0.6.tar.gz.

File metadata

  • Download URL: megabot-0.0.6.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for megabot-0.0.6.tar.gz
Algorithm Hash digest
SHA256 3b975b85d86800f195dc854269687bcbd2b8eb92a1d86c396310067ea562b29f
MD5 de2f7f900841a9d166cdb4417f626fe9
BLAKE2b-256 e992018a8ecb4813f2a2a2bd46b59de0dc73a0606c86ef495ecc96f06bb3a0d1

See more details on using hashes here.

File details

Details for the file megabot-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: megabot-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 16.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for megabot-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e0efd7b7c7cc7ec46dd6f8a98978e006b712404d976611af9a2415f12528b10d
MD5 d06e497d8c89fa14367d2805de0c28f6
BLAKE2b-256 3616419fc9fb9225c4fc52209abae73b430095e667f38beb3dfbe9d1ac168b26

See more details on using hashes here.

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