Skip to content

Commit

Permalink
Use smart-plugins in webhook example
Browse files Browse the repository at this point in the history
  • Loading branch information
z44d committed Sep 30, 2024
1 parent 5c1ae3d commit 24b9bf8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 44 deletions.
22 changes: 0 additions & 22 deletions examples/webhook/handlers.py

This file was deleted.

25 changes: 3 additions & 22 deletions examples/webhook/main.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
import uvicorn

from tgram import TgBot, filters
from tgram.handlers import Handler, Handlers
from tgram import TgBot
from tgram.types import Update

from fastapi import FastAPI
from typing import Dict

from handlers import private_handler, callback_handler

app = FastAPI(debug=True)
bots: Dict[int, TgBot] = {}


def add_handlers(bot: TgBot) -> None:
bot.add_handler(
Handler(
callback=private_handler,
type=Handlers.MESSAGE,
filters=filters.private & filters.text,
)
)
bot.add_handler(
Handler(
callback=callback_handler,
type=Handlers.CALLBACK_QUERY,
)
)


def get_tgbot_object(token: str) -> TgBot:
bot_id = int(token.split(":")[0])

if bot_id in bots:
return bots.get(bot_id)

bot = TgBot(token)
add_handlers(bot)
bot = TgBot(token, plugins="./plugins")
bot.load_plugins()
bots.update({bot_id: bot})

return bot
Expand Down
7 changes: 7 additions & 0 deletions examples/webhook/plugins/callbackdata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from tgram import TgBot
from tgram.types import CallbackQuery


@TgBot.on_callback_query()
async def on_c(bot: TgBot, query: CallbackQuery) -> bool:
return await query.answer("Hi.", show_alert=True)
13 changes: 13 additions & 0 deletions examples/webhook/plugins/start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from tgram import TgBot, filters

from tgram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton


@TgBot.on_message(filters.command("start"))
async def on_start(bot: TgBot, message: Message) -> Message:
return await message.reply_text(
"Hi from Tgram",
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton("Buttonn", callback_data="yay")]]
),
)

0 comments on commit 24b9bf8

Please sign in to comment.