Skip to content

Commit e9e67c8

Browse files
committed
Add the component name to the callbacks automatically
This also renames FrozenBot.buttons to botogram.buttons
1 parent 17717bb commit e9e67c8

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

botogram/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from .runner import run
3535
from .objects import *
3636
from .utils import usernames_in
37+
from .callbacks import buttons
3738

3839

3940
# This code will simulate the Windows' multiprocessing behavior if the

botogram/callbacks.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import hashlib
2323

2424
from . import crypto
25+
from .context import ctx
2526

2627

2728
DIGEST = hashlib.md5
@@ -31,19 +32,24 @@
3132
class ButtonsRow:
3233
"""A row of an inline keyboard"""
3334

34-
def __init__(self, bot):
35+
def __init__(self):
3536
self._content = []
36-
self._bot = bot
3737

3838
def url(self, label, url):
3939
"""Open an URL when the button is pressed"""
4040
self._content.append({"text": label, "url": url})
4141

4242
def callback(self, label, callback, data=None):
4343
"""Trigger a callback when the button is pressed"""
44+
def generate_callback_data():
45+
c = ctx()
46+
47+
name = "%s:%s" % (c.component_name(), callback)
48+
return get_callback_data(c.bot, name, data)
49+
4450
self._content.append({
4551
"text": label,
46-
"callback_data": get_callback_data(self._bot, callback, data),
52+
"callback_data": generate_callback_data,
4753
})
4854

4955
def switch_inline_query(self, label, query="", current_chat=False):
@@ -59,29 +65,46 @@ def switch_inline_query(self, label, query="", current_chat=False):
5965
"switch_inline_query": query,
6066
})
6167

68+
def _get_content(self):
69+
"""Get the content of this row"""
70+
for item in self._content:
71+
new = item.copy()
72+
73+
# Replace any callable with its value
74+
# This allows to dynamically generate field values
75+
for key, value in new.items():
76+
if callable(value):
77+
new[key] = value()
78+
79+
yield new
80+
6281

6382
class Buttons:
6483
"""Factory for inline keyboards"""
6584

66-
def __init__(self, bot):
85+
def __init__(self):
6786
self._rows = {}
68-
self._bot = bot
6987

7088
def __getitem__(self, index):
7189
if index not in self._rows:
72-
self._rows[index] = ButtonsRow(self._bot)
90+
self._rows[index] = ButtonsRow()
7391
return self._rows[index]
7492

7593
def _serialize_attachment(self):
7694
rows = [
77-
row._content for i, row in sorted(
95+
list(row._get_content()) for i, row in sorted(
7896
tuple(self._rows.items()), key=lambda i: i[0]
7997
)
8098
]
8199

82100
return {"inline_keyboard": rows}
83101

84102

103+
def buttons():
104+
"""Create a new inline keyboard"""
105+
return Buttons()
106+
107+
85108
def parse_callback_data(bot, raw):
86109
"""Parse the callback data generated by botogram and return it"""
87110
raw = raw.encode("utf-8")

botogram/frozenbot.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from . import utils
2424
from . import objects
2525
from . import api as api_module
26-
from .callbacks import Buttons
2726

2827

2928
class FrozenBotError(Exception):
@@ -235,10 +234,6 @@ def register_update_processor(self, kind, processor):
235234
"""Register a new update processor"""
236235
raise FrozenBotError("Can't register new update processors at runtime")
237236

238-
def buttons(self):
239-
"""Create a new inline keyboard"""
240-
return Buttons(self)
241-
242237
# This helper manages the translation
243238

244239
def _(self, message, **args):

0 commit comments

Comments
 (0)