Skip to content

Commit

Permalink
🐛 fix async (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
j1g5awi committed Oct 27, 2024
1 parent 7987609 commit c3dbcab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
8 changes: 5 additions & 3 deletions nonebot/adapters/telegram/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ async def poll(self, bot: Bot):
if update_offset is not None:
for update in updates:
update_offset = update.update_id + 1
await self.__handle_update(
bot, update.model_dump(by_alias=True, exclude_none=True)
asyncio.create_task(
self.__handle_update(
bot, update.model_dump(by_alias=True, exclude_none=True)
)
)
elif updates:
update_offset = updates[0].update_id
Expand All @@ -118,7 +120,7 @@ async def handle_http(self, request: Request) -> Response:
if bot.secret_token == token:
if request.content:
update: dict = json.loads(request.content)
await self.__handle_update(bot, update)
asyncio.create_task(self.__handle_update(bot, update))
return Response(204)
return Response(401)

Expand Down
29 changes: 29 additions & 0 deletions nonebot/adapters/telegram/utils.py
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}"

0 comments on commit c3dbcab

Please sign in to comment.