skip to Main Content

I am using a Python code to forward new messages between two bot/channel telegram.
I am trying to edit messages before forwarding them (I need to delete some parts of text), but I didn’t find the right solution

This is my code to forward new messages:

from telethon import TelegramClient, 
events
import asyncio

api_id = 99999999
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'

my_channel_id = -10000000000000
channels = [-1000000000000]

client = TelegramClient('myGrab', api_id, api_hash)
print("GRAB - Started")

@client.on(events.NewMessage(chats=channels))
async def my_event_handler(event):
    if event.message:
        await client.send_message(my_channel_id, event.message)


client.start()
client.run_until_disconnected()

This is the source message:

The Button LTDSH 11 has matched with 
a live Match

Adelaide United ||(4) V NE MetroStars(1)
🇦🇺 Australia :- Npl South Australian
⏱ Elapsed Time: 55'
⚽️ Score: 1 - 1

Home Shots: on:off Target: 3:1
Away Shots: on:off Target: 2:7

Home Possession: 37%
Away Possession: 63%

Home Dangerous Attacks: 30
Away Dangerous Attacks: 39

📈AP1 (H:A): 27:33
Form (H:A): LLDLD:WWDWW

Home Corners: 2
Away Corners: 5

Last 10mins Shots on Target
Home:Away 1 : 0

💰Odds (H:D:A)
Starting 5.50 : 4.75 : 1.40
Live     8.50 : 4.33 : 1.33

And I’d like to delete some parts to get just this:

The Button LTDSH 11 has matched with a live Match

Adelaide United ||(4) V NE MetroStars(1)
🇦🇺 Australia :- Npl South Australian
⏱ Elapsed Time: 55'
⚽️ Score: 1 - 1

💰Odds (H:D:A)
Live     8.50 : 4.33 : 1.33

Any suggestion, please?

EDIT: the source text changes everytime, but there are some specific words in all messages. Is it possible to delete words/characters before or after a specific word?

2

Answers


  1. You can edit the event text and patch the edited text to it, the modifications will be saved and used when passed to send_message:

    @client.on(events.NewMessage(chats=channels))
    async def my_event_handler(event):
        if event.message:
            old_message = event.text
            new_message = ...
            # do your edits however necessary.
            event.text = new_message
    
            await client.send_message(my_channel_id, event.message)
    
    Login or Signup to reply.
  2. You can use string manipulation to edit the message. This is the updated script:

    from telethon import TelegramClient, events
    import asyncio
    
    api_id = 99999999
    api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
    
    my_channel_id = -10000000000000
    channels = [-1000000000000]
    
    client = TelegramClient('myGrab', api_id, api_hash)
    print("GRAB - Started")
    
    @client.on(events.NewMessage(chats=channels))
    async def my_event_handler(event):
        if event.message:
            original_message = event.message.message
    
            lines = original_message.split('n')  # Split the source message into seperate lines
            new_message = 'n'.join(lines[:7]) + 'nn' + lines[-2] # Keep the required parts and delete the rest
    
            await client.send_message(my_channel_id, new_message)
    
    client.start()
    client.run_until_disconnected()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search