Skip to content

Commit

Permalink
Manually set random_id in requests
Browse files Browse the repository at this point in the history
Getting the response message relies on this ID.
Because requests are now immutable, and resolve sets the random_id,
the method to obtain the response message would lack the random_id,
as the original request is not touched by resolve.
  • Loading branch information
Lonami committed Feb 7, 2022
1 parent bd76755 commit 1f3ce07
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
10 changes: 7 additions & 3 deletions telethon/_client/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import typing
import warnings
import dataclasses
import os

from .._misc import helpers, utils, requestiter, hints
from ..types import _custom
Expand Down Expand Up @@ -486,7 +487,8 @@ async def send_message(
entity, message._file._media, reply_to_msg_id=reply_to, message=message._text,
entities=message._fmt_entities, reply_markup=message._reply_markup, silent=message._silent,
schedule_date=schedule, clear_draft=clear_draft,
background=background, noforwards=noforwards, send_as=send_as
background=background, noforwards=noforwards, send_as=send_as,
random_id=int.from_bytes(os.urandom(8), 'big', signed=True),
)
else:
request = _tl.fn.messages.SendMessage(
Expand All @@ -501,7 +503,8 @@ async def send_message(
reply_markup=_custom.button.build_reply_markup(buttons),
schedule_date=schedule,
noforwards=noforwards,
send_as=send_as
send_as=send_as,
random_id=int.from_bytes(os.urandom(8), 'big', signed=True),
)

result = await self(request)
Expand Down Expand Up @@ -574,7 +577,8 @@ def get_key(m):
with_my_score=with_my_score,
schedule_date=schedule,
noforwards=noforwards,
send_as=send_as
send_as=send_as,
random_id=[int.from_bytes(os.urandom(8), 'big', signed=True) for _ in chunk],
)
result = await self(req)
sent.extend(self._get_response_message(req, result, entity))
Expand Down
4 changes: 3 additions & 1 deletion telethon/types/_custom/inlineresult.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ... import _tl
from ..._misc import utils
import os


class InlineResult:
Expand Down Expand Up @@ -162,7 +163,8 @@ async def click(self, entity=None, reply_to=None, comment_to=None,
clear_draft=clear_draft,
hide_via=hide_via,
reply_to_msg_id=reply_id,
send_as=send_as
send_as=send_as,
random_id=int.from_bytes(os.urandom(8), 'big', signed=True),
)
return self._client._get_response_message(
req, await self._client(req), entity)
Expand Down
3 changes: 2 additions & 1 deletion telethon/types/_custom/messagebutton.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ async def click(self, share_phone=None, share_geo=None, *, password=None):
return await self._client.get_entity(self.button.user_id)
elif isinstance(self.button, _tl.KeyboardButtonSwitchInline):
return await self._client(_tl.fn.messages.StartBot(
bot=self._bot, peer=self._chat, start_param=self.button.query
bot=self._bot, peer=self._chat, start_param=self.button.query,
random_id=int.from_bytes(os.urandom(8), 'big', signed=True),
))
elif isinstance(self.button, _tl.KeyboardButtonUrl):
return self.button.url
Expand Down

0 comments on commit 1f3ce07

Please sign in to comment.