telegram-forwarder

A telegram bot that forwards messages to a telegram channel

🥅 Clients Goal

The clients goal is to be able to get any message recieved from a bot in telegram forwarded to them by another bot so they can run some analysis on the message. The client would also like this to be made using python3

🧪 Solution

We start by using Telethon a asynchronous python package which lets us login in by:

  • User Authentication
  • Bot Authentcation
  • Listen to events - Filter events
  • Send messages

This is great as we need to create 2 clients one for the user to detect when they recieve a messsage and another client (the bot) to then forward the message.

Once we have the two clients is as simple as using the events.NewMessage handler and filtering by the channel & sender. If they match our edge case then Voilà we can do what ever we wish!

In this case we just want to use telethon's functionaltity to then use the bot client to forward the raw_text we got from the event.

this adds room to do what ever we want with the message we have recieved

💻 PoC Code

Here is a snippet of how the event handling will look

@user_client.on(
    events.NewMessage(
        incoming=True,
        from_users=Config.USER_TO_LISTEN_TO,
    )
)
async def handler(event: events.NewMessage.Event):
    logger.info(f"Received message: {event.raw_text}")
    await bot_client.send_message(Config.USER_TO_FORWARD_TO, event.raw_text)

    logger.info(f"Forwarded message: {event.raw_text}")

if you're interested about more of the code checkout the Git Repo Here

There are 2 branches:

  • main : simple code
  • verbose : Has exceptional error handling and proper asynchronous event loop