Skip to main content

Python library for building Telegram bots

Project description

Telegram-botup

Python library for building Telegram bots

Install

$ pip install telegram-botup

Example

# app.py

from web_lib import App, Request

from botup.dispatcher import Dispatcher
from botup.api import Api
from botup.types import BaseContext

TOKEN = "token"

app = App()
api = Api(TOKEN)
dispatcher = Dispatcher()


@dispatcher.message_handler('hello')
async def hello_handler(ctx: BaseContext):
    await api.send_message(ctx.chat_id, f'Hello {ctx.update.message.from_.first_name}')


@app.post(f'/{TOKEN}')
async def index(request: Request):
    await dispatcher.handle(await request.json())
    return ""

Widgets example

# app.py

from asyncio import gather

from web_lib import App, Request

from botup import Widget, Context, Dispatcher, Api, Bot, Navigation
from botup.widgets.date_picker import DatePicker
from botup.mixins.echo import EchoMixin
from botup.types import InlineKeyboardMarkup, InlineKeyboardButton

TOKEN = "token"
WEBHOOK = f'https://url/{TOKEN}'


class TestMixin:

    def build(self, dispatcher: Dispatcher):
        dispatcher.register_command_handler('/test', self.cmd_test)

    @staticmethod
    async def cmd_test(ctx: Context):
        await ctx.api.send_message(
            chat_id=ctx.chat_id,
            text='Test'
        )


class RootWidget(Widget, TestMixin, EchoMixin):
    async def entry(self, ctx: Context, **kwargs):
        botup_date_picker_result = kwargs.get(DatePicker.DEFAULT_RESULT_KEY)
        if botup_date_picker_result:
            await ctx.api.send_message(
                chat_id=ctx.chat_id,
                text=f'date: {botup_date_picker_result}'
            )
            return

    def build(self, dispatcher: Dispatcher):
        TestMixin.build(self, dispatcher)
        dispatcher.register_command_handler('/start', self.cmd_start)
        dispatcher.register_callback_handler('ready', self.clb_ready)
        EchoMixin.build(self, dispatcher)

    @staticmethod
    async def cmd_start(ctx: Context):
        message = await ctx.api.send_message(
            chat_id=ctx.chat_id,
            text='Click for view date picker',
            reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton(callback_data='ready', text='Click')]])
        )
        await ctx.state_manager.set(
            chat_id=ctx.chat_id,
            key='message_id',
            value=str(message.message_id)
        )

    @staticmethod
    async def clb_ready(ctx: Context):
        _, message_id, nav = await gather(
            ctx.api.answer_callback_query(ctx.update.callback_query.id),
            ctx.state_manager.get(
                chat_id=ctx.chat_id,
                key='message_id'
            ),
            Navigation.of(ctx)
        )

        await nav.push(DatePicker.__name__, message_id=int(message_id))


bot = Bot(
    token=TOKEN,
    root=RootWidget(children=[DatePicker()])
)
app = App()


@app.on_event("startup")
async def startup_event():
    async with Api(TOKEN) as api:
        await api.set_webhook(WEBHOOK)


@app.on_event("shutdown")
async def shutdown_event():
    async with Api(TOKEN) as api:
        await api.delete_webhook()

    await bot.close_session()


@app.post(f'/{TOKEN}')
async def index(request: Request):
    await bot.handle(await request.json())
    return ""

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

telegram_botup-1.0.6-py3-none-any.whl (41.2 kB view details)

Uploaded Python 3

File details

Details for the file telegram_botup-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: telegram_botup-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 41.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for telegram_botup-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d04a0eb9bc10c5a9f6a754d3018660c8b40d26a701eac52af2caeba38d404343
MD5 2d05cf91d772de5fcea5d6f1e51005af
BLAKE2b-256 e422af1e6e2024173e37eb1437d490ff9ca6899905acb265f987e6157fc89c30

See more details on using hashes here.

Supported by

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