Skip to content

Commit

Permalink
Update simple_telethon.py
Browse files Browse the repository at this point in the history
  • Loading branch information
artembakhanov authored Jan 13, 2021
1 parent 8013eb6 commit 0fc0368
Showing 1 changed file with 8 additions and 41 deletions.
49 changes: 8 additions & 41 deletions examples/simple_telethon.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,25 @@
import json
from typing import List, Dict, Callable

api_id = 1234567
api_hash = "api-hash-here"
bot_token = "bot-token-here"
api_id = 123456
api_hash = "1233456789"
bot_token = "token"

bot = TelegramClient("bot", api_id, api_hash)


def build_buttons(markup: str) -> List[List[Button]]:
# Telethon uses Telegram's MTProto protocol directly
# instead of relying on the Bot API. This means
# that telethon doesn't send or recieve data in json
# and so we have to parse the text and callback data
# of buttons to proper objects.

button_markup_data: Dict = json.loads(markup)

buttons: List[List[Button]] = []
for button_row in button_markup_data["inline_keyboard"]:
button_row: List[Dict]
buttons.append(
[
Button.inline(text=str(but["text"]), data=but["callback_data"])
for but in button_row
]
)

return buttons


def callback_filter(calendar_id=0) -> Callable:
# Due to differences in design, we cannot use
# DetailedTelegramCalendar.func() directly for telethon

def inner(callback_data: bytes) -> bool:
start = telegram_bot_calendar.base.CB_CALENDAR + "_" + str(calendar_id)
return callback_data.decode("utf-8").startswith(start)

return inner


@bot.on(events.NewMessage(pattern="/start"))
async def reply_handler(event):
calendar, step = DetailedTelegramCalendar().build()
await event.respond(f"Select {LSTEP[step]}", buttons=build_buttons(calendar))
calendar, step = DetailedTelegramCalendar(telethon=True).build()
await event.respond(f"Select {LSTEP[step]}", buttons=calendar)


@bot.on(events.CallbackQuery(pattern=callback_filter()))
@bot.on(events.CallbackQuery(pattern=DetailedTelegramCalendar.func(telethon=True)))
async def calendar_handler(event):
result, key, step = DetailedTelegramCalendar().process(event.data.decode("utf-8"))
result, key, step = DetailedTelegramCalendar(telethon=True).process(event.data.decode("utf-8"))

if not result and key:
await event.edit(f"Select {LSTEP[step]}", buttons=build_buttons(key))
await event.edit(f"Select {LSTEP[step]}", buttons=key)
elif result:
await event.edit(f"You selected {result}")

Expand Down

0 comments on commit 0fc0368

Please sign in to comment.