Skip to content

Commit 5fb499e

Browse files
committed
Fix invalid callbacks sent to chats different than the current one
1 parent 9e1c713 commit 5fb499e

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

botogram/callbacks.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def url(self, label, url):
4141

4242
def callback(self, label, callback, data=None):
4343
"""Trigger a callback when the button is pressed"""
44-
def generate_callback_data():
44+
def generate_callback_data(chat):
4545
c = ctx()
4646

4747
name = "%s:%s" % (c.component_name(), callback)
48-
return get_callback_data(c.bot, c.chat(), name, data)
48+
return get_callback_data(c.bot, chat, name, data)
4949

5050
self._content.append({
5151
"text": label,
@@ -65,7 +65,7 @@ def switch_inline_query(self, label, query="", current_chat=False):
6565
"switch_inline_query": query,
6666
})
6767

68-
def _get_content(self):
68+
def _get_content(self, chat):
6969
"""Get the content of this row"""
7070
for item in self._content:
7171
new = item.copy()
@@ -74,7 +74,7 @@ def _get_content(self):
7474
# This allows to dynamically generate field values
7575
for key, value in new.items():
7676
if callable(value):
77-
new[key] = value()
77+
new[key] = value(chat)
7878

7979
yield new
8080

@@ -90,9 +90,9 @@ def __getitem__(self, index):
9090
self._rows[index] = ButtonsRow()
9191
return self._rows[index]
9292

93-
def _serialize_attachment(self):
93+
def _serialize_attachment(self, chat):
9494
rows = [
95-
list(row._get_content()) for i, row in sorted(
95+
list(row._get_content(chat)) for i, row in sorted(
9696
tuple(self._rows.items()), key=lambda i: i[0]
9797
)
9898
]

botogram/objects/mixins.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ def _get_call_args(self, reply_to, extra, attach, notify):
6969
if attach is not None:
7070
if not hasattr(attach, "_serialize_attachment"):
7171
raise ValueError("%s is not an attachment" % attach)
72-
args["reply_markup"] = json.dumps(attach._serialize_attachment())
72+
args["reply_markup"] = json.dumps(attach._serialize_attachment(
73+
self
74+
))
7375
if not notify:
7476
args["disable_notification"] = True
7577

@@ -327,7 +329,9 @@ def edit(self, text, syntax=None, preview=True, extra=None, attach=None):
327329
if attach is not None:
328330
if not hasattr(attach, "_serialize_attachment"):
329331
raise ValueError("%s is not an attachment" % attach)
330-
args["reply_markup"] = json.dumps(attach._serialize_attachment())
332+
args["reply_markup"] = json.dumps(attach._serialize_attachment(
333+
self.chat
334+
))
331335

332336
self._api.call("editMessageText", args)
333337
self.text = text
@@ -346,7 +350,9 @@ def edit_caption(self, caption, extra=None, attach=None):
346350
if attach is not None:
347351
if not hasattr(attach, "_serialize_attachment"):
348352
raise ValueError("%s is not an attachment" % attach)
349-
args["reply_markup"] = json.dumps(attach._serialize_attachment())
353+
args["reply_markup"] = json.dumps(attach._serialize_attachment(
354+
self.chat
355+
))
350356

351357
self._api.call("editMessageCaption", args)
352358
self.caption = caption

docs/changelog/0.5.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ New features
4747
* New arguments ``file_id`` and ``url`` in :py:meth:`botogram.User.send_file`
4848
* New arguments ``file_id`` and ``url`` in :py:meth:`botogram.Chat.send_photo`
4949
* New arguments ``file_id`` and ``url`` in :py:meth:`botogram.User.send_photo`
50+
51+
Bug fixes
52+
---------
53+
54+
* Fixed invalid callbacks sent to chats different than the current one

tests/test_callbacks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_buttons(bot, sample_update):
4242
buttons[2].switch_inline_query("test 5", "wow", current_chat=True)
4343

4444
with Context(bot, hook, sample_update):
45-
assert buttons._serialize_attachment() == {
45+
assert buttons._serialize_attachment(sample_update.chat()) == {
4646
"inline_keyboard": [
4747
[
4848
{"text": "test 1", "url": "http://example.com"},

0 commit comments

Comments
 (0)