Creating or using a telegram bot that forward messages received from a bot to another account

Viewed 2273

I'm new to telegram bot (especially programming one).

Is there a bot that can forward a message from another bot to a second account? Where the second account is not the admin of the bot writing.

Alternatevely, is there a way to create one? I'm pretty skilled in Python but I get a little bit lost on using a Python script to make a bot work.

I'm working on a Windows 11 environment

1 Answers

Install Telethon

python3 -m pip install --upgrade pip
python3 -m pip install --upgrade telethon

Obtain your API_ID and API_TOKEN from Telegram

And then replace your values and run the code:

from telethon import TelegramClient, events
import asyncio
import logging
logging.basicConfig(level=logging.WARNING)

api_id = # don't forget to fill this 
api_hash = '' # don't forget to fill this 

client = TelegramClient("forwarder", api_id, api_hash)
client.start()


from_chat_id = # don't forget to fill this
to_chat_id = "me"


async def get_chat_id(title):
    async for dialog in client.iter_dialogs():        
        if dialog.title == title:        
            return dialog.id


@client.on(events.NewMessage)
async def my_event_handler(event):
    chat = await event.get_chat()
    if chat.id == from_chat_id:
        await client.forward_messages(to_chat_id, event.message)

# asyncio.get_event_loop().run_until_complete(get_chat_id("YOUR CHAT TITLE"))
asyncio.get_event_loop().run_forever()

from_chat_id will be the chat id of the bot you want to forward message from.

to_chat_id will be the destination chat id. using 'me' would forward the message to your saved messages.

Another thing to note is this code:

asyncio.get_event_loop().run_until_complete(get_chat_id("YOUR CHAT TITLE"))

if you want to get the id of a chat you can uncomment this line and pass your chat title.

Related