diff --git a/mautrix_telegram/bot.py b/mautrix_telegram/bot.py index 4506f22c..6758d0a3 100644 --- a/mautrix_telegram/bot.py +++ b/mautrix_telegram/bot.py @@ -14,7 +14,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from typing import Awaitable, Callable, Dict, List, Optional, Pattern, TYPE_CHECKING +from typing import Awaitable, Callable, Dict, List, Optional, Pattern, Tuple, TYPE_CHECKING import logging import re @@ -23,7 +23,7 @@ from telethon.tl.types import ( ChannelParticipantAdmin, ChannelParticipantCreator, ChatForbidden, ChatParticipantAdmin, ChatParticipantCreator, InputChannel, InputUser, MessageActionChatAddUser, MessageActionChatDeleteUser, MessageEntityBotCommand, PeerChannel, PeerChat, TypePeer, - UpdateNewChannelMessage, UpdateNewMessage, MessageActionChatMigrateTo) + UpdateNewChannelMessage, UpdateNewMessage, MessageActionChatMigrateTo, User) from telethon.tl.functions.messages import GetChatsRequest, GetFullChatRequest from telethon.tl.functions.channels import GetChannelsRequest, GetParticipantRequest from telethon.errors import ChannelInvalidError, ChannelPrivateError @@ -60,6 +60,14 @@ class Bot(AbstractUser): self.tg_whitelist = [] # type: List[int] self.whitelist_group_admins = (config["bridge.relaybot.whitelist_group_admins"] or False) # type: bool + self._me_info = None # type: Optional[User] + self._me_mxid = None # type: Optional[MatrixUserID] + + async def get_me(self, use_cache: bool = True) -> Tuple[User, MatrixUserID]: + if not use_cache or not self._me_mxid: + self._me_info = await self.client.get_me() + self._me_mxid = pu.Puppet.get_mxid_from_id(TelegramID(self._me_info.id)) + return self._me_info, self._me_mxid async def init_permissions(self) -> None: whitelist = config["bridge.relaybot.whitelist"] or [] diff --git a/mautrix_telegram/commands/telegram/auth.py b/mautrix_telegram/commands/telegram/auth.py index 815dce98..754301c3 100644 --- a/mautrix_telegram/commands/telegram/auth.py +++ b/mautrix_telegram/commands/telegram/auth.py @@ -46,11 +46,9 @@ async def ping(evt: CommandEvent) -> Optional[Dict]: async def ping_bot(evt: CommandEvent) -> Optional[Dict]: if not evt.tgbot: return await evt.reply("Telegram message relay bot not configured.") - bot_info = await evt.tgbot.client.get_me() - mxid = pu.Puppet.get_mxid_from_id(bot_info.id) - displayname = bot_info.first_name + info, mxid = await evt.tgbot.get_me(use_cache=False) return await evt.reply("Telegram message relay bot is active: " - f"[{displayname}](https://matrix.to/#/{mxid}) (ID {bot_info.id})\n\n" + f"[{info.first_name}](https://matrix.to/#/{mxid}) (ID {info.id})\n\n" "To use the bot, simply invite it to a portal room.") diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index a86d6eee..aced8f22 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -1304,8 +1304,13 @@ class Portal: invites = await self._get_telegram_users_in_matrix_room() if len(invites) < 2: + if self.bot is not None: + info, mxid = await self.bot.get_me() + raise ValueError("Not enough Telegram users to create a chat. " + "Invite more Telegram ghost users to the room, such as the " + f"relaybot ([{info.first_name}](https://matrix.to/#/{mxid})).") raise ValueError("Not enough Telegram users to create a chat. " - "Invite more Telegram ghost users to the room, such as the relaybot.") + "Invite more Telegram ghost users to the room.") if self.peer_type == "chat": response = await source.client(CreateChatRequest(title=self.title, users=invites)) entity = response.chats[0]