telegram: Add compact ids to messages
patch b77c17d9bb8b484231cb1d28ca9c9fdc865d4612
Author: E. Bosch <presidev@AT@gmail.com>
Date: Wed Jan 26 22:30:58 CET 2022
* telegram: Add compact ids to messages
hunk ./telegram.py 38
+ self.mid = mesg_id('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%+./_~')
hunk ./telegram.py 237
+
+ if self.mid.mesg_base is None:
+ self.mid.mesg_base = event.message.id
+
+ user = self.get_irc_user_from_telegram(event.sender_id)
+ mid = self.mid.num_to_id(event.message.id - self.mid.mesg_base)
+ message = '[{}] {}'.format(mid, event.message.message)
hunk ./telegram.py 246
- await self.handle_telegram_private_message(event)
+ await self.handle_telegram_private_message(user, message)
hunk ./telegram.py 248
- await self.handle_telegram_channel_message(event)
-
- async def handle_telegram_private_message(self, event):
- self.logger.debug('Handling Telegram Private Message: %s', event)
-
- user = self.get_irc_user_from_telegram(event.sender_id)
- message = event.message.message if event.message.message else ''
+ await self.handle_telegram_channel_message(event, user, message)
+
+ async def handle_telegram_private_message(self, user, message):
+ self.logger.debug('Handling Telegram Private Message: %s, %s', user, message)
hunk ./telegram.py 255
- async def handle_telegram_channel_message(self, event):
+ async def handle_telegram_channel_message(self, event, user, message):
hunk ./telegram.py 261
- user = self.get_irc_user_from_telegram(event.sender_id)
-
hunk ./telegram.py 262
- message = event.message.message if event.message.message else ''
hunk ./telegram.py 319
+
+class mesg_id:
+ def __init__(self, alpha):
+ self.alpha = alpha
+ self.base = len(alpha)
+ self.alphaval = { i:v for v, i in enumerate(alpha) }
+ self.mesg_base = None
+
+ def num_to_id(self, num, neg=''):
+ if num < 0: return self.num_to_id(-num, '-')
+ (high, low) = divmod(num, self.base)
+ if high >= self.base:
+ aux = self.num_to_id(high)
+ return neg + aux + self.alpha[low]
+ else:
+ return neg + self.alpha[high] + self.alpha[low]
+
+ def id_to_num(self, id, n=1):
+ if id:
+ if id[0] == '-': return self.id_to_num(id[1:], -1)
+ aux = self.alphaval[id[-1:]] * n
+ sum = self.id_to_num(id[:-1], n * self.base)
+ return sum + aux
+ else:
+ return 0