patch 1a0add76192510876c18329da2e85acd0a16d368 Author: Peter Bui Date: Thu Jul 18 19:06:07 CEST 2019 * abstract resolving telegram_id to irc_nick The get_irc_nick_from_telegram_id will check if the telegram_id is in the dictionary. If it is not, it will look it up and add the appropriate entries. diff -rN -u old-irgramd/irtelegramd.py new-irgramd/irtelegramd.py --- old-irgramd/irtelegramd.py 2024-11-23 03:47:46.583501930 +0100 +++ new-irgramd/irtelegramd.py 2024-11-23 03:47:46.587501924 +0100 @@ -280,15 +280,18 @@ def get_telegram_channel(self, chat): return '#' + chat.title.lower().replace(' ', '-') + async def get_irc_nick_from_telegram_id(self, tid): + if tid not in self.tid_to_iid: + user = await self.telegram_client.get_input_entity(tid) + nick = self.get_telegram_nick(user) + self.tid_to_iid[tid] = nick + self.iid_to_tid[nick] = tid + + return self.tid_to_iid[tid] + async def handle_telegram_message(self, event): self.logger.debug('Handling Telegram Message: %s', event) - if event.from_id not in self.tid_to_iid: - user = await self.telegram_client.get_input_entity(event.from_id) - nick = self.get_telegram_nick(user) - self.tid_to_iid[user.id] = nick - self.iid_to_tid[nick] = user.id - if event.message.is_private: await self.handle_telegram_private_message(event) else: @@ -297,7 +300,7 @@ async def handle_telegram_private_message(self, event): self.logger.debug('Handling Telegram Private Message: %s', event) - nick = self.tid_to_iid[event.from_id] + nick = await self.get_irc_nick_from_telegram_id(event.from_id) for message in event.message.message.splitlines(): await self.send_irc_command(':{} PRIVMSG {} :{}'.format( self.get_irc_user_mask(nick), self.irc_nick, message @@ -319,7 +322,7 @@ if channel not in self.irc_channels: await self.join_irc_channel(self.irc_nick, channel) - nick = self.tid_to_iid[event.from_id] + nick = await self.get_irc_nick_from_telegram_id(event.from_id) if nick not in self.irc_channels[channel]: await self.join_irc_channel(nick, channel) @@ -349,7 +352,7 @@ self.logger.info('Handling Telegram Chat Action: %s', event) irc_channel = self.tid_to_iid[event.action_message.to_id.channel_id] - irc_nick = self.tid_to_iid[event.action_message.from_id] + irc_nick = await self.get_irc_nick_from_telegram_id(event.action_message.from_id) if event.user_added or event.user_joined: await self.join_irc_channel(irc_nick, irc_channel)