obsidian-telegram-sync icon indicating copy to clipboard operation
obsidian-telegram-sync copied to clipboard

Process Old Messages not working.

Open RonYanDaik opened this issue 8 months ago • 3 comments

I'm adding chats to 'Process Old Messages' and it does not fetch old messages from channel. I have my user connected.

Maybe there can be an option to procces old messages by executing a command or something?

RonYanDaik avatar Apr 22 '25 11:04 RonYanDaik

I wrote this hacky script that uses telethon to resend all the old message the bot didn't process. I have a command in my terminal I can run to "wakeup" the bot, have all the old message re-sent, and then pulled to my obsidian. The code is such that, even if the message is duplicated in the Telegram chat, only one version gets to the obsidian note.

This might be helpful for you!


chat = "@..."
api_id = ... #an int
api_hash = "..."
SEND_MESSAGE = True #turn to False for debug
username = "..." #your name as it appears on Telegram
botname = "..." #the name of the bot as it appears on Telegram

from telethon.sync import TelegramClient
import telethon.sync
from datetime import datetime, timedelta, timezone
import warnings

client = TelegramClient('session_id', api_id, api_hash)

user_msg = {}
id_to_og_key = {} #message id to the original message date (if the message is forward)
id_to_msg = {}
bot_msg = []

def is_forwarded(msg):
    return isinstance(msg.fwd_from, telethon.tl.types.MessageFwdHeader)

def get_og_msg_key(msg):
    if is_forwarded(msg):
        return str(msg.fwd_from.date) + "-" + str(msg.text)
    else:
        return str(msg.date) + "-" + str(msg.text)

def is_recent(msg):
    return msg.date > (datetime.now() - timedelta(weeks=1)).replace(tzinfo=timezone.utc)

with client:
    msg_counter = 0
    for msg in reversed(client.iter_messages(chat, None)):
        msg_counter +=1
        if msg_counter > 10: #we ignore the first messages
            #print(msg.sender.first_name, ':', msg.text)
            if botname in msg.sender.first_name:
                bot_msg.append(msg)
            elif username in msg.sender.first_name and not is_forwarded(msg):
                reactions = msg.reactions
                if get_og_msg_key(msg) in user_msg and is_recent(msg):
                    warnings.warn(f"""The message "{msg.text}" has the same OG key as "{user_msg[get_og_msg_key(msg)].text}" """)
                user_msg[get_og_msg_key(msg)] = msg
                id_to_og_key[msg.id] = get_og_msg_key(msg)
                id_to_msg[msg.id] = msg
            elif username in msg.sender.first_name and is_forwarded(msg): # and msg.text != "...✅..."
                id_to_og_key[msg.id] = get_og_msg_key(msg)
                id_to_msg[msg.id] = msg
            else:
                raise ValueError("This code is not used in the narrow setting it's supposed to be used!")
    #print(f"Len user_msg before {len(user_msg)}")
    for msg in bot_msg:
        if msg.is_reply:
            if ("...✅..." in msg.text) or ("...🆗..." in msg.text):
                try:
                    del user_msg[id_to_og_key[msg.reply_to.reply_to_msg_id]] #we remove the OG messages that have been answered. We ignore the forwarded messages.
                except KeyError:
                    if is_recent(msg):
                        warnings.warn("A bot message is replying to nothing. Maybe the original message has been deleted.")
            elif is_recent(msg):
                warnings.warn(f"""The bot bot answered: "{msg.text}". It is different than '...✅...' or '...🆗...'. It might not be well-functionning.""")
        else:
            raise ValueError("The bot sent a message that is not a reply!")
    
    key_to_del = [] #the bot can also react with 👍 to signal processed message
    for msg_id,msg in id_to_msg.items():
        if msg.reactions is not None and len(msg.reactions.results) > 0:
            if msg.reactions.results[0].reaction.emoticon in ['👍', '👌']:
                key_to_del.append(id_to_og_key[msg_id])
            else:
                warnings.warn(f"""The message has a reaction different from 👍: {msg.reactions.results[0].reaction.emoticon}""")
    for k in key_to_del:
        try:
            del user_msg[k]
        except KeyError:
            pass # in the case where the message has a react AND has been answered

    if len(user_msg) > 0:
        print(f"Sending {len(user_msg)} messages ...")
    else:
        print(f"{botname} is up to date! No message to send.")

    for msg in user_msg.values():
        # print(msg.sender.first_name, ':', msg.text)
        if SEND_MESSAGE:
            client.forward_messages(chat, msg)

aVariengien avatar Jul 16 '25 19:07 aVariengien

chat = "@..."

thank you! what data is supposed to be in this field?

RonYanDaik avatar Aug 01 '25 11:08 RonYanDaik

It is the exact username of your bot. You can find it in the Telegram app!

aVariengien avatar Aug 05 '25 22:08 aVariengien