Implement a way to deal with failing inline requests

This is due to Telegram raising an error in case bots not
in DC4 try to communicate with chats that live in DC4.
This commit is contained in:
Dan 2020-08-22 09:22:14 +02:00
parent 10d2dd7159
commit bc62b3f6ae
4 changed files with 100 additions and 6 deletions

View File

@ -22,6 +22,7 @@ from pyrogram import raw
from pyrogram import types
from pyrogram import utils
from pyrogram.scaffold import Scaffold
from .inline_session import get_session
class EditInlineMedia(Scaffold):
@ -105,9 +106,14 @@ class EditInlineMedia(Scaffold):
else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 5)
return await self.send(
unpacked = utils.unpack_inline_message_id(inline_message_id)
dc_id = unpacked.dc_id
session = get_session(self, dc_id)
return await session.send(
raw.functions.messages.EditInlineBotMessage(
id=utils.unpack_inline_message_id(inline_message_id),
id=unpacked,
media=media,
reply_markup=reply_markup.write() if reply_markup else None,
**await self.parser.parse(caption, parse_mode)

View File

@ -20,6 +20,7 @@ from pyrogram import raw
from pyrogram import types
from pyrogram import utils
from pyrogram.scaffold import Scaffold
from .inline_session import get_session
class EditInlineReplyMarkup(Scaffold):
@ -51,9 +52,15 @@ class EditInlineReplyMarkup(Scaffold):
InlineKeyboardMarkup([[
InlineKeyboardButton("New button", callback_data="new_data")]]))
"""
return await self.send(
unpacked = utils.unpack_inline_message_id(inline_message_id)
dc_id = unpacked.dc_id
session = get_session(self, dc_id)
return await session.send(
raw.functions.messages.EditInlineBotMessage(
id=utils.unpack_inline_message_id(inline_message_id),
id=unpacked,
reply_markup=reply_markup.write() if reply_markup else None,
)
)

View File

@ -22,6 +22,7 @@ from pyrogram import raw
from pyrogram import types
from pyrogram import utils
from pyrogram.scaffold import Scaffold
from .inline_session import get_session
class EditInlineText(Scaffold):
@ -72,9 +73,14 @@ class EditInlineText(Scaffold):
disable_web_page_preview=True)
"""
return await self.send(
unpacked = utils.unpack_inline_message_id(inline_message_id)
dc_id = unpacked.dc_id
session = get_session(self, dc_id)
return await session.send(
raw.functions.messages.EditInlineBotMessage(
id=utils.unpack_inline_message_id(inline_message_id),
id=unpacked,
no_webpage=disable_web_page_preview or None,
reply_markup=reply_markup.write() if reply_markup else None,
**await self.parser.parse(text, parse_mode)

View File

@ -0,0 +1,75 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2020 Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from asyncio import Lock
import pyrogram
from pyrogram import raw
from pyrogram.errors import AuthBytesInvalid
from pyrogram.session import Session
from pyrogram.session.auth import Auth
lock = Lock()
session = None
dest_dc_id = 4
async def get_session(client: "pyrogram.Client", dc_id: int):
if dc_id != dest_dc_id:
return client
if dc_id == await client.storage.dc_id():
return client
async with lock:
global session
if session is not None:
return session
session = Session(
client, dest_dc_id,
await Auth(client, dest_dc_id, False).create(),
False, is_media=True
)
await session.start()
for _ in range(3):
exported_auth = await session.send(
raw.functions.auth.ExportAuthorization(
dc_id=dest_dc_id
)
)
try:
await session.send(
raw.functions.auth.ImportAuthorization(
id=exported_auth.id,
bytes=exported_auth.bytes
)
)
except AuthBytesInvalid:
continue
else:
break
else:
await session.stop()
raise AuthBytesInvalid
return session