Python Forum
Telegram Bot for forwarding messages
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Telegram Bot for forwarding messages
#1
First time poster so I hope Im in the right place of the forum. Excuse me in advance if I use wrong terms or names I am an absolute beginner with this.

Im using:

Python 3.9 for windows
PyCharm Community Edition 2020.2.2
I also loaded in the Telethon info prior to starting

Hey so to be completley clear from the offest, I have never done any coding of any type for about 20 years. I find myself in a position that I need to auto forward messages from one main telegram channel (which I own) to two seperate others (again both mine) dependant on a defining key word/phrase. I'd also like to remove several lines as they provide no purpose to the information I require, the lines to be removed would always be the same for all messages regardless of their destination. Lastly I wished to add a header and footer to each adjusted message, again the header and footer would always be the same for all messages.

So at the moment all messages in the main channel appears as:

Subject: Notice 1
Time: XXX
Date: XXX
Detail: XXX
4th line: XXX
5th line: XXX
6th line: XXX

So as an example I wish for them to appear something like this:

RECIEVED FROM MAIN GROUP

Subject: Notice 1
Time: XXX
Detail: XXX
4th line: XXX
6th line: XXX

THANK YOU

The only difference between messages is the Subject it'll either say Notice 1 or Notice 2. My intention is to send Notice 1 marked messages to Notice 1 group and Notice 2 to Notice 2 group anything that doesnt have the whitelist word stays where it is and is not forwarded.

So I started looking around the internet as you do and I can across this tutorial on youtube https://youtu.be/nnERUAHO2PY

Great, I followed it to the letter and I can set up a single forward, I havent added whitelist coding part yet. But again I wish to have 2 destination groups and use whitelist to send dependant on source message text and to remove certain lines from it.

I have tried my very best to read up on the subject but I am just completely lost and although I may understand some of the commands I am at a loss in to how they should actually be used within the code and all I find myself doing is screwing up the code and breaking it.

The tutorial has a settings.py file and Ive included below what it contains, I've taken out the real API and Hash info:

TELEGRAM_API_ID = XXXXXXX
TELEGRAM_API_HASH = "asdfghjh76ftyd54ttcXXXX"
COMBINATIONS = {
    -1001292557818: [-1001329301443]
And a main.py which include the rest of the code:

from telethon import TelegramClient, events
from settings import TELEGRAM_API_ID, TELEGRAM_API_HASH, COMBINATIONS


client = TelegramClient('session', api_id=TELEGRAM_API_ID, api_hash=TELEGRAM_API_HASH)


@client.on(events.NewMessage)
async def handle_new_message(event):
    sender_chat_id = event.sender_id
    if sender_chat_id in list(COMBINATIONS.keys()):
        destination_chat_ids = COMBINATIONS.get(sender_chat_id, [])
        for chat_id in destination_chat_ids:
             await client.send_message(chat_id, event.raw_text)

client.start()
client.run_until_disconnected()
I'm sure there are probably 100 ways to write code like this but this was clearest instruction I found for close to what I'm trying to acheive.

So just to recap I'm looking to:

Forward messages from one destination Telegram Channel to one of two source channels based on keywords
Remove lines from source message
Add standardised header and footer to all forwarded messages

I really hope I'm not asking too much but if anyone can help me in the right direction with a few pointers or examples I'd greatly appreciate it

Cheers
Reply
#2
(Oct-02-2020, 11:46 AM)johnotherise Wrote: First time poster so I hope Im in the right place of the forum. Excuse me in advance if I use wrong terms or names I am an absolute beginner with this.

Im using:

Python 3.9 for windows
PyCharm Community Edition 2020.2.2
I also loaded in the Telethon info prior to starting

Hey so to be completley clear from the offest, I have never done any coding of any type for about 20 years. I find myself in a position that I need to auto forward messages from one main telegram channel (which I own) to two seperate others (again both mine) dependant on a defining key word/phrase. I'd also like to remove several lines as they provide no purpose to the information I require, the lines to be removed would always be the same for all messages regardless of their destination. Lastly I wished to add a header and footer to each adjusted message, again the header and footer would always be the same for all messages.

So at the moment all messages in the main channel appears as:

Subject: Notice 1
Time: XXX
Date: XXX
Detail: XXX
4th line: XXX
5th line: XXX
6th line: XXX

So as an example I wish for them to appear something like this:

RECIEVED FROM MAIN GROUP

Subject: Notice 1
Time: XXX
Detail: XXX
4th line: XXX
6th line: XXX

THANK YOU

The only difference between messages is the Subject it'll either say Notice 1 or Notice 2. My intention is to send Notice 1 marked messages to Notice 1 group and Notice 2 to Notice 2 group anything that doesnt have the whitelist word stays where it is and is not forwarded.

So I started looking around the internet as you do and I can across this tutorial on youtube https://youtu.be/nnERUAHO2PY

Great, I followed it to the letter and I can set up a single forward, I havent added whitelist coding part yet. But again I wish to have 2 destination groups and use whitelist to send dependant on source message text and to remove certain lines from it.

I have tried my very best to read up on the subject but I am just completely lost and although I may understand some of the commands I am at a loss in to how they should actually be used within the code and all I find myself doing is screwing up the code and breaking it.

The tutorial has a settings.py file and Ive included below what it contains, I've taken out the real API and Hash info:

TELEGRAM_API_ID = XXXXXXX
TELEGRAM_API_HASH = "asdfghjh76ftyd54ttcXXXX"
COMBINATIONS = {
    -1001292557818: [-1001329301443]
And a main.py which include the rest of the code:

from telethon import TelegramClient, events
from settings import TELEGRAM_API_ID, TELEGRAM_API_HASH, COMBINATIONS


client = TelegramClient('session', api_id=TELEGRAM_API_ID, api_hash=TELEGRAM_API_HASH)


@client.on(events.NewMessage)
async def handle_new_message(event):
    sender_chat_id = event.sender_id
    if sender_chat_id in list(COMBINATIONS.keys()):
        destination_chat_ids = COMBINATIONS.get(sender_chat_id, [])
        for chat_id in destination_chat_ids:
             await client.send_message(chat_id, event.raw_text)

client.start()
client.run_until_disconnected()
I'm sure there are probably 100 ways to write code like this but this was clearest instruction I found for close to what I'm trying to acheive.

So just to recap I'm looking to:

Forward messages from one destination Telegram Channel to one of two source channels based on keywords
Remove lines from source message
Add standardised header and footer to all forwarded messages

I really hope I'm not asking too much but if anyone can help me in the right direction with a few pointers or examples I'd greatly appreciate it

Cheers

Hey I am not a developer, but I am trying to do a little code on Telegram messages forward from Multiple Groups to One Group, Can you help me with the same
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020