-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import base64 | ||
import struct | ||
import binascii | ||
|
||
|
||
def _decode_telegram_base64(string): | ||
""" | ||
来自 [telethon](https://github.com/LonamiWebs/Telethon/blob/v1/telethon/utils.py#L1086) | ||
""" | ||
return base64.urlsafe_b64decode(string + "=" * (len(string) % 4)) | ||
|
||
|
||
def _encode_telegram_base64(string): | ||
""" | ||
来自 [telethon](https://github.com/LonamiWebs/Telethon/blob/v1/telethon/utils.py#L1102) | ||
""" | ||
try: | ||
return base64.urlsafe_b64encode(string).rstrip(b"=").decode("ascii") | ||
except (binascii.Error, ValueError, TypeError): | ||
return None # not valid base64, not valid ascii, not a string | ||
|
||
|
||
def resolve_inline_message_id(inline_msg_id: str): | ||
""" | ||
来自 [telethon](https://github.com/LonamiWebs/Telethon/blob/v1/telethon/utils.py#L1304) | ||
从 inline_msg_id 解析出 message_id, peer, dc_id, access_hash | ||
""" | ||
_, _, pid, _ = struct.unpack("<iiiq", _decode_telegram_base64(inline_msg_id)) | ||
return f"-100{-pid}" if pid > 0 else f"{pid}" |