This bot uses Telethon to relay messages between users and an owner. Users can send messages to the bot, and these are forwarded to the owner's chat. The owner can reply directly through the bot to the original sender. Additionally, users can be blocked or unblocked by the owner using commands.
- Message Relaying: Forwards messages between users and the owner.
- Command Handling: Supports
/block
,/unblock
, and/help
commands. (Reply to a message) - User Blocking System: Uses SQLite to track blocked users.
- Encoded Chat IDs: Uses a unique encoding mechanism for privacy.
- TOML Configuration: Configurable via
config.toml
file.
- Python 3.8+
- Telegram Bot Token
- API ID and API Hash from my.telegram.org
-
Clone the repository or save the script to a directory.
-
Install the dependencies by running:
pip install -r requirements.txt
-
Create a config.toml file with the following structure:
[Config] token = "your_bot_token" api_id = 123456 api_hash = "your_api_hash" destinationID = 987654321 # Owner's Telegram ID commandPrefix = "/" [Strings] name = "Relay Bot"
-
Ensure the bot token, API ID, and API hash are correctly configured in
config.toml
. -
Start the bot by running:
python relay_bot.py
-
The bot will display:
Bot is running...
/help
– Displays the list of available commands./block
– Blocks the user from communicating with the bot./unblock
– Unblocks a previously blocked user.
The bot loads the API ID, API Hash, and Bot Token from config.toml
:
loadedConfig = toml.load("config.toml")["Config"]
strings = toml.load("config.toml")["Strings"]
The bot is initialized using Telethon. It authenticates with the bot token using:
client = TelegramClient('relay_bot', api_id, api_hash).start(bot_token=loadedConfig["token"])
The SQLite database stores blocked users. If the table doesn’t exist, it’s created on startup:
def create_db():
connection, cursor = getDBC()
cursor.execute('''
CREATE TABLE IF NOT EXISTS BlockedUsers (
userId INTEGER
)''')
connection.commit()
connection.close()
The bot uses Telethon's @client.on(events.NewMessage)
to handle new messages.
@client.on(events.NewMessage)
async def handle_update(event):
sender = await event.get_sender()
if sender.bot:
return # Ignore messages from other bots
If the user is blocked, the bot sends a warning message:
if user_is_blocked(sender.id):
await event.reply(f"You are currently blocked from contacting {strings['name']}.")
return
To maintain privacy, chat IDs are encoded before forwarding them to the owner:
def encode_chat_id(id: int):
chars = {"zeroWidthSpace": "\u200B", "nationalDigitShapes": "\u206F"}
encoded_parts = [
chars['zeroWidthSpace'] * int(number) + chars['nationalDigitShapes']
for number in str(id)
]
return ''.join(encoded_parts)
The original chat ID can be decoded with:
def decode_chat_id(id: str):
chars = {"zeroWidthSpace": "\u200B", "nationalDigitShapes": "\u206F"}
split_result = id.split(chars['nationalDigitShapes'])
return int(''.join([str(len(number)) for number in split_result[:-1]]))
The bot responds to owner commands (e.g., /block
and /unblock
):
async def handle_command(message_text, response_id):
command = message_text[1:].strip().lower()
if command == "help":
await client.send_message(destination_Id, f"""Commands:
{commandPrefix}help - Shows this message
{commandPrefix}block - Blocks the user from contacting the bot
{commandPrefix}unblock - Unblocks the user""")
elif command == "block":
connection, cursor = getDBC()
cursor.execute("INSERT INTO BlockedUsers VALUES (?)", (response_id,))
connection.commit()
connection.close()
await client.send_message(destination_Id, "User blocked.")
elif command == "unblock":
connection, cursor = getDBC()
cursor.execute("DELETE FROM BlockedUsers WHERE userId = ?", (response_id,))
connection.commit()
connection.close()
await client.send_message(destination_Id, "User unblocked.")
-
getDBC()
: Opens a connection to the SQLite database.def getDBC(): connection = sqlite.connect('blockedUsers.db') cursor = connection.cursor() return connection, cursor
-
user_is_blocked(user_id)
: Checks if a user is blocked.def user_is_blocked(user_id): _, cursor = getDBC() cursor.execute("SELECT userId FROM BlockedUsers WHERE userId = ?", (user_id,)) return cursor.fetchone() is not None
-
User Message:
User sends a message:Hello!
-
Bot Relays Message to Owner: [John#senderid] - Hello![EncodedChatID]
-
Owner Reply:
Owner replies:Hi, how can I help?
-
Bot Relays Reply to User: Hi, how can I help?
-
Blocking a User:
Owner sends/block
. The user is now blocked.
- Bot Token Invalid: Ensure the correct bot token is provided in
config.toml
. - Database Errors: Make sure
blockedUsers.db
is writable. - API ID/Hash Errors: Ensure the correct API credentials are used from my.telegram.org.
This Relay Bot provides a robust way to manage communication between users and an owner using Telethon. With message forwarding, user blocking, and configurable commands, the bot is suitable for personal or business use cases where intermediary communication is required.