telegram, irc: Set topic in IRC with Telegram channel/chat description --> to head
patch 10725c4fff915a1c95e95b5f998f26c97b146d5e
Author: E. Bosch <presidev@AT@gmail.com>
Date: Wed Jan 22 00:35:42 CET 2025
* telegram: Try to fix ChatAdminRequiredError when getting participants from a channel
reported in https://github.com/prsai/irgramd/issues/1
patch 0c96892ec68150c96ef7ba50ec4e2db3db2296e4
Author: E. Bosch <presidev@AT@gmail.com>
Date: Thu Jan 16 22:00:14 CET 2025
* exclam: Fix send a reaction when there was already a previous reaction from other user on the same message,
for some reason the 'message' attribute is not received
patch 89bc0957a4fb31bf8066e338bc4548cd2d52c821
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sat Nov 2 20:20:45 CET 2024
* telegram, irc: Set topic in IRC with Telegram channel/chat description
diff -rN -u old-irgramd/exclam.py new-irgramd/exclam.py
--- old-irgramd/exclam.py 2025-04-19 03:02:19.405359206 +0200
+++ new-irgramd/exclam.py 2025-04-19 03:02:19.409359199 +0200
@@ -205,8 +205,8 @@
except ReactionInvalidError:
reply = ('!react: Reaction not allowed',)
else:
- self.tmp_tg_msg = update.updates[0].message
- reply = True
+ self.tmp_tg_msg = getattr(update.updates[0], 'message', None)
+ reply = bool(self.tmp_tg_msg)
else:
reply = ('!react: Unknown reaction',)
else:
diff -rN -u old-irgramd/include.py new-irgramd/include.py
--- old-irgramd/include.py 2025-04-19 03:02:19.405359206 +0200
+++ new-irgramd/include.py 2025-04-19 03:02:19.409359199 +0200
@@ -11,3 +11,4 @@
VERSION = '0.2'
NICK_MAX_LENGTH = 20
CHAN_MAX_LENGTH = 50
+MAX_LINE = 400
diff -rN -u old-irgramd/irc.py new-irgramd/irc.py
--- old-irgramd/irc.py 2025-04-19 03:02:19.405359206 +0200
+++ new-irgramd/irc.py 2025-04-19 03:02:19.409359199 +0200
@@ -18,7 +18,7 @@
# Local modules
-from include import VERSION, CHAN_MAX_LENGTH, NICK_MAX_LENGTH
+from include import VERSION, CHAN_MAX_LENGTH, NICK_MAX_LENGTH, MAX_LINE
from irc_replies import irc_codes
from utils import chunks, set_replace, split_lines
from service import service
@@ -259,7 +259,7 @@
real_chan = self.get_realcaps_name(chan)
users_count = len(self.irc_channels[chan])
topic = await self.tg.get_channel_topic(chan, [None])
- await self.reply_code(user, 'RPL_LIST', (real_chan, users_count, topic))
+ await self.reply_code(user, 'RPL_LIST', (real_chan, users_count, topic[:MAX_LINE]))
await self.reply_code(user, 'RPL_LISTEND')
async def handle_irc_names(self, user, channels):
@@ -612,7 +612,7 @@
founder = list(self.irc_channels_founder[chan])[0]
else:
founder = self.service_user.irc_nick
- await self.reply_code(user, 'RPL_TOPIC', (channel, topic))
+ await self.reply_code(user, 'RPL_TOPIC', (channel, topic[:MAX_LINE]))
await self.reply_code(user, 'RPL_TOPICWHOTIME', (channel, founder, timestamp))
async def irc_namelist(self, user, channel):
diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py
--- old-irgramd/telegram.py 2025-04-19 03:02:19.409359199 +0200
+++ new-irgramd/telegram.py 2025-04-19 03:02:19.409359199 +0200
@@ -15,7 +15,8 @@
import collections
import telethon
from telethon import types as tgty, utils as tgutils
-from telethon.tl.functions.messages import GetMessagesReactionsRequest
+from telethon.tl.functions.messages import GetMessagesReactionsRequest, GetFullChatRequest
+from telethon.tl.functions.channels import GetFullChannelRequest
# Local modules
@@ -176,18 +177,21 @@
self.irc.iid_to_tid[chan] = chat.id
self.irc.irc_channels[chan] = set()
# Add users from the channel
- async for user in self.telegram_client.iter_participants(chat.id):
- user_nick = self.set_ircuser_from_telegram(user)
- if not user.is_self:
- self.irc.irc_channels[chan].add(user_nick)
- # Add admin users as ops in irc
- if isinstance(user.participant, tgty.ChatParticipantAdmin) or \
- isinstance(user.participant, tgty.ChannelParticipantAdmin):
- self.irc.irc_channels_ops[chan].add(user_nick)
- # Add creator users as founders in irc
- elif isinstance(user.participant, tgty.ChatParticipantCreator) or \
- isinstance(user.participant, tgty.ChannelParticipantCreator):
- self.irc.irc_channels_founder[chan].add(user_nick)
+ try:
+ async for user in self.telegram_client.iter_participants(chat.id):
+ user_nick = self.set_ircuser_from_telegram(user)
+ if not user.is_self:
+ self.irc.irc_channels[chan].add(user_nick)
+ # Add admin users as ops in irc
+ if isinstance(user.participant, tgty.ChatParticipantAdmin) or \
+ isinstance(user.participant, tgty.ChannelParticipantAdmin):
+ self.irc.irc_channels_ops[chan].add(user_nick)
+ # Add creator users as founders in irc
+ elif isinstance(user.participant, tgty.ChatParticipantCreator) or \
+ isinstance(user.participant, tgty.ChannelParticipantCreator):
+ self.irc.irc_channels_founder[chan].add(user_nick)
+ except:
+ self.logger.warning('Not possible to get participants of channel %s', channel)
def get_telegram_nick(self, user):
nick = (user.username
@@ -309,8 +313,16 @@
else:
entity = await self.telegram_client.get_entity(tid)
entity_cache[0] = entity
+ if isinstance(entity, tgty.Channel):
+ full = await self.telegram_client(GetFullChannelRequest(channel=entity))
+ elif isinstance(entity, tgty.Chat):
+ full = await self.telegram_client(GetFullChatRequest(chat_id=tid))
+ else:
+ return ''
entity_type = self.get_entity_type(entity, format='long')
- return 'Telegram ' + entity_type + ' ' + str(tid) + ': ' + entity.title
+ topic = full.full_chat.about
+ sep = ': ' if topic else ''
+ return entity_type + sep + topic
async def get_channel_creation(self, channel, entity_cache):
tid = self.get_tid(channel)
diff -rN -u old-irgramd/utils.py new-irgramd/utils.py
--- old-irgramd/utils.py 2025-04-19 03:02:19.409359199 +0200
+++ new-irgramd/utils.py 2025-04-19 03:02:19.409359199 +0200
@@ -20,6 +20,8 @@
FILENAME_INVALID_CHARS = re.compile('[/{}<>()"\'\\|&#%?]')
SIMPLE_URL = re.compile('http(|s)://[^ ]+')
+from include import MAX_LINE
+
# Utilities
class command:
@@ -61,9 +63,8 @@
return (x + mark if n != length else x for n, x in enumerate(items, start=1))
def split_lines(message):
- MAX = 400
messages_limited = []
- wr = textwrap.TextWrapper(width=MAX)
+ wr = textwrap.TextWrapper(width=MAX_LINE)
# Split when Telegram original message has breaks
messages = message.splitlines()