-
Notifications
You must be signed in to change notification settings - Fork 518
/
bot.py
81 lines (66 loc) · 3.05 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright (c) 2021 Ayush
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# License can be found in < https://github.com/Ayush7445/telegram-auto_forwarder/blob/main/License > .
# Import necessary modules
from telethon import TelegramClient, events
from decouple import config
import logging
from telethon.sessions import StringSession
import os
# Configure logging
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.WARNING)
# Print starting message
print("Starting...")
# Read configuration from environment variables
APP_ID = config("APP_ID", default=0, cast=int)
API_HASH = config("API_HASH", default=None, cast=str)
SESSION = config("SESSION", default="", cast=str)
FROM_ = config("FROM_CHANNEL", default="", cast=str)
TO_ = config("TO_CHANNEL", default="", cast=str)
BLOCKED_TEXTS = config("BLOCKED_TEXTS", default="", cast=lambda x: [i.strip().lower() for i in x.split(',')])
MEDIA_FORWARD_RESPONSE = config("MEDIA_FORWARD_RESPONSE", default="yes").lower()
FROM = [int(i) for i in FROM_.split()]
TO = [int(i) for i in TO_.split()]
YOUR_ADMIN_USER_ID = config("YOUR_ADMIN_USER_ID", default=0, cast=int)
BOT_API_KEY = config("BOT_API_KEY", default="", cast=str)
# Initialize Telethon client
try:
steallootdealUser = TelegramClient(StringSession(SESSION), APP_ID, API_HASH)
steallootdealUser.start()
except Exception as ap:
print(f"ERROR - {ap}")
exit(1)
# Event handler for incoming messages
@steallootdealUser.on(events.NewMessage(incoming=True, chats=FROM))
async def sender_bH(event):
for i in TO:
try:
message_text = event.raw_text.lower()
if any(blocked_text in message_text for blocked_text in BLOCKED_TEXTS):
print(f"Blocked message containing one of the specified texts: {event.raw_text}")
logging.warning(f"Blocked message containing one of the specified texts: {event.raw_text}")
continue
if event.media:
user_response = MEDIA_FORWARD_RESPONSE
if user_response != 'yes':
print(f"Media forwarding skipped by user for message: {event.raw_text}")
continue
await steallootdealUser.send_message(i, message_text, file=event.media)
print(f"Forwarded media message to channel {i}")
else:
await steallootdealUser.send_message(i, message_text)
print(f"Forwarded text message to channel {i}")
except Exception as e:
print(f"Error forwarding message to channel {i}: {e}")
# Run the bot
print("Bot has started.")
steallootdealUser.run_until_disconnected()