telegram: Add handler for next reactions (2nd, 3rd, etc.) that don't come from --> to head
patch 0cfc7e59b24fb1a1b279fc593f8d04d0648e3880
Author: E. Bosch <presidev@AT@gmail.com>
Date: Mon Oct 21 00:54:16 CEST 2024
* README update
patch abf1d31ddcf3cddd55844900065a3c3dd6bf9c67
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sun Oct 20 02:32:44 CEST 2024
* exclam: Add "-" parameter to "!react" to remove a reaction
patch 5c1687a16a1e49af7b5f70e9449c447bbf7c9df9
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sat Oct 19 22:56:31 CEST 2024
* telegram: On emoticon->emoji conversions for reactions,
when several emoticons can be mapped to an emoji,
keep first elements that are more probable allowed for reactions
patch 7db9d9a07af099b00a32a6ee0197c400a52240e5
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sun Oct 13 23:00:24 CEST 2024
* telegram: Limit text length for reactions to twice as replies
patch 42fe2f72d41ed36616e6a19b9388eec356679e38
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sat Oct 12 23:17:45 CEST 2024
* README update
patch 873004141f83b60da113e8967a2148d9d33008be
Author: E. Bosch <presidev@AT@gmail.com>
Date: Thu Oct 10 23:11:40 CEST 2024
* exclam: Add reaction (!react) command to send emoticon->emoji as Telegram reaction
patch 69a63d9f5f49ece35c5d552ba8b3081c8277490d
Author: E. Bosch <presidev@AT@gmail.com>
Date: Mon Oct 7 00:07:54 CEST 2024
* telegram, service: Move initial help to service module
add a line for "equivalent command"
patch c1ffe716a42ea01ca345d7a756b685d7174f99c5
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sun Oct 6 23:59:23 CEST 2024
* telegram: Fix regression in delete reaction event
patch beacde93a685dd954f9823dc0a6fea4594b2c1e4
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sat Sep 28 02:53:06 CEST 2024
* telegram: Avoid duplicated reactions events in some cases
patch b2f8fe9251a26c43e16ba1aadff8d71e64a5a7e9
Author: E. Bosch <presidev@AT@gmail.com>
Date: Fri Sep 27 11:05:53 CEST 2024
* telegram: Add handler for next reactions (2nd, 3rd, etc.) that don't come from
the same events as 1st (why?!)
diff -rN -u old-irgramd/README.md new-irgramd/README.md
--- old-irgramd/README.md 2024-10-23 00:18:00.700714058 +0200
+++ new-irgramd/README.md 2024-10-23 00:18:00.704714051 +0200
@@ -50,7 +50,7 @@
- Forwards (receive, send)
- Deletions (receive, do)
- Editions (receive, do)
-- Reactions (receive)
+- Reactions (receive, send, remove)
- Polls (receive, show)
- Actions [pin message, channel photo] (receive)
- Dialogs management
diff -rN -u old-irgramd/emoji2emoticon.py new-irgramd/emoji2emoticon.py
--- old-irgramd/emoji2emoticon.py 2024-10-23 00:18:00.700714058 +0200
+++ new-irgramd/emoji2emoticon.py 2024-10-23 00:18:00.704714051 +0200
@@ -86,9 +86,13 @@
'\U0001f644': '"o o,"',
'\U0001f914': '":-L"',
'\U0001f92b': '":-o-m"',
- '\U0001f970': '":)e>"'
+ '\U0001f970': '":)e>"',
}
+emo_inv = { '-': None }
+for k in reversed(emo):
+ emo_inv[emo[k][1:-1]] = k
+
def replace_mult(line, emo):
for utf_emo in emo:
if utf_emo in line:
diff -rN -u old-irgramd/exclam.py new-irgramd/exclam.py
--- old-irgramd/exclam.py 2024-10-23 00:18:00.700714058 +0200
+++ new-irgramd/exclam.py 2024-10-23 00:18:00.704714051 +0200
@@ -7,9 +7,12 @@
# can be found in the LICENSE file included in this project.
import os
-from telethon.errors.rpcerrorlist import MessageNotModifiedError, MessageAuthorRequiredError
+from telethon.tl.functions.messages import SendReactionRequest
+from telethon import types as tgty
+from telethon.errors.rpcerrorlist import MessageNotModifiedError, MessageAuthorRequiredError, ReactionInvalidError
from utils import command, HELP
+from emoji2emoticon import emo_inv
class exclam(command):
def __init__(self, telegram):
@@ -21,6 +24,7 @@
'!fwd': (self.handle_command_fwd, 2, 2, -1),
'!upl': (self.handle_command_upl, 1, 2, 2),
'!reupl': (self.handle_command_reupl, 2, 3, 3),
+ '!react': (self.handle_command_react, 2, 2, -1),
}
self.tg = telegram
self.irc = telegram.irc
@@ -188,3 +192,33 @@
'HTTP/HTTPS URL.',
)
return reply
+
+ async def handle_command_react(self, cid=None, act=None, help=None):
+ if not help:
+ id, chk_msg = await self.check_msg(cid)
+ if chk_msg is not None:
+ if act in emo_inv:
+ utf8_emo = emo_inv[act]
+ reaction = [ tgty.ReactionEmoji(emoticon=utf8_emo) ] if utf8_emo else None
+ try:
+ update = await self.tg.telegram_client(SendReactionRequest(self.tmp_telegram_id, id, reaction=reaction))
+ except ReactionInvalidError:
+ reply = ('!react: Reaction not allowed',)
+ else:
+ self.tmp_tg_msg = update.updates[0].message
+ reply = True
+ else:
+ reply = ('!react: Unknown reaction',)
+ else:
+ reply = ('!react: Unknown message to react',)
+ else: # HELP.brief or HELP.desc (first line)
+ reply = (' !react React to a message',)
+ if help == HELP.desc: # rest of HELP.desc
+ reply += \
+ (
+ ' !react <compact_id> <emoticon reaction>|-',
+ 'React with <emoticon reaction> to a message with <compact_id>,',
+ 'irgramd will translate emoticon to closest emoji.',
+ 'Use - to remove a previous reaction.',
+ )
+ return reply
diff -rN -u old-irgramd/irc.py new-irgramd/irc.py
--- old-irgramd/irc.py 2024-10-23 00:18:00.700714058 +0200
+++ new-irgramd/irc.py 2024-10-23 00:18:00.704714051 +0200
@@ -532,11 +532,7 @@
await self.reply_code(user, 'RPL_ISUPPORT', (CHAN_MAX_LENGTH, NICK_MAX_LENGTH))
async def send_help(self, user):
- for line in (
- 'Welcome to irgramd service',
- 'use /msg {} help'.format(self.service_user.irc_nick),
- 'to get help',
- ):
+ for line in self.service.initial_help():
await self.send_msg(self.service_user, None, line, user)
async def check_telegram_auth(self, user):
diff -rN -u old-irgramd/service.py new-irgramd/service.py
--- old-irgramd/service.py 2024-10-23 00:18:00.700714058 +0200
+++ new-irgramd/service.py 2024-10-23 00:18:00.708714045 +0200
@@ -25,6 +25,14 @@
self.irc = telegram.irc
self.tmp_ircnick = None
+ def initial_help(self):
+ return (
+ 'Welcome to irgramd service',
+ 'use /msg {} help'.format(self.irc.service_user.irc_nick),
+ 'or equivalent in your IRC client',
+ 'to get help',
+ )
+
async def handle_command_code(self, code=None, help=None):
if not help:
if self.ask_code:
diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py
--- old-irgramd/telegram.py 2024-10-23 00:18:00.704714051 +0200
+++ new-irgramd/telegram.py 2024-10-23 00:18:00.708714045 +0200
@@ -71,6 +71,7 @@
self.volatile_cache = collections.OrderedDict()
self.prev_id = {}
self.sorted_len_usernames = []
+ self.last_reaction = None
# Set event to be waited by irc.check_telegram_auth()
self.auth_checked = asyncio.Event()
@@ -503,6 +504,25 @@
self.sorted_len_usernames.append(username)
self.sorted_len_usernames.sort(key=lambda k: len(k), reverse=True)
+ def format_reaction(self, msg, message_rendered, edition_case, reaction):
+ react_quote_len = self.quote_len * 2
+ if len(message_rendered) > react_quote_len:
+ text_old = '{}...'.format(message_rendered[:react_quote_len])
+ text_old = fix_braces(text_old)
+ else:
+ text_old = message_rendered
+
+ if edition_case == 'react-add':
+ user = self.get_irc_user_from_telegram(reaction.peer_id.user_id)
+ emoji = reaction.reaction.emoticon
+ react_action = '+'
+ react_icon = e.emo[emoji] if emoji in e.emo else emoji
+ elif edition_case == 'react-del':
+ user = self.get_irc_user_from_telegram(msg.sender_id)
+ react_action = '-'
+ react_icon = ''
+ return text_old, '{}{}'.format(react_action, react_icon), user
+
async def handle_telegram_edited(self, event):
self.logger.debug('Handling Telegram Message Edited: %s', pretty(event))
@@ -536,23 +556,12 @@
# Reactions
else:
+ if reaction:
+ if self.last_reaction == reaction.date:
+ return
+ self.last_reaction = reaction.date
action = 'React'
- if len(message_rendered) > self.quote_len and not event.message.is_reply:
- text_old = '{}...'.format(message_rendered[:self.quote_len])
- text_old = fix_braces(text_old)
- else:
- text_old = message_rendered
-
- if edition_case == 'react-add':
- user = self.get_irc_user_from_telegram(reaction.peer_id.user_id)
- emoji = reaction.reaction.emoticon
- react_action = '+'
- react_icon = e.emo[emoji] if emoji in e.emo else emoji
- elif edition_case == 'react-del':
- user = self.get_irc_user_from_telegram(event.sender_id)
- react_action = '-'
- react_icon = ''
- edition_react = '{}{}'.format(react_action, react_icon)
+ text_old, edition_react, user = self.format_reaction(event.message, message_rendered, edition_case, reaction)
text = '|{} {}| {}'.format(action, text_old, edition_react)
@@ -561,6 +570,29 @@
self.to_cache(id, mid, message, message_rendered, user, chan, event.message.media)
self.to_volatile_cache(self.prev_id, id, text, user, chan, current_date())
+ async def handle_next_reaction(self, event):
+ self.logger.debug('Handling Telegram Next Reaction (2nd, 3rd, ...): %s', pretty(event))
+
+ reactions = event.reactions.recent_reactions
+ react = max(reactions, key=lambda y: y.date)
+
+ if self.last_reaction != react.date:
+ self.last_reaction = react.date
+ id = event.msg_id
+ msg = await self.telegram_client.get_messages(entity=event.peer, ids=id)
+ mid = self.mid.num_to_id_offset(msg.peer_id, id)
+ message = self.filters(msg.message)
+ message_rendered = await self.render_text(msg, mid, upd_to_webpend=None)
+
+ text_old, edition_react, user = self.format_reaction(msg, message_rendered, edition_case='react-add', reaction=react)
+
+ text = '|React {}| {}'.format(text_old, edition_react)
+
+ chan = await self.relay_telegram_message(msg, user, text)
+
+ self.to_cache(id, mid, message, message_rendered, user, chan, msg.media)
+ self.to_volatile_cache(self.prev_id, id, text, user, chan, current_date())
+
async def handle_telegram_deleted(self, event):
self.logger.debug('Handling Telegram Message Deleted: %s', pretty(event))
@@ -584,6 +616,9 @@
if message:
await self.handle_telegram_message(event=None, message=message, upd_to_webpend=update.webpage)
+ elif isinstance(update, tgty.UpdateMessageReactions):
+ await self.handle_next_reaction(update)
+
async def handle_telegram_message(self, event, message=None, upd_to_webpend=None, history=False):
self.logger.debug('Handling Telegram Message: %s', pretty(event or message))