forked from rtaserver/RTA-WRT
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtelebot.py
224 lines (198 loc) · 8.45 KB
/
telebot.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import sys
import subprocess
import os
import glob
import math
def check_and_install_dependencies():
required_packages = ['telethon', 'colorama']
installed_packages = []
failed_packages = []
for package in required_packages:
try:
__import__(package)
installed_packages.append(package)
except ImportError:
print(f"\nMenginstall {package}...")
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
installed_packages.append(package)
print(f"{package} berhasil diinstall!")
except subprocess.CalledProcessError:
failed_packages.append(package)
print(f"Gagal menginstall {package}")
if failed_packages:
print("\nBeberapa package gagal diinstall:", failed_packages)
sys.exit(1)
elif installed_packages != required_packages:
print("\nSemua dependensi telah diinstall. Menjalankan ulang script...")
os.execv(sys.executable, ['python3'] + sys.argv)
else:
print("\nSemua dependensi sudah terinstall!")
# Jalankan pengecekan dependensi
check_and_install_dependencies()
from telethon import TelegramClient, events
import asyncio
from colorama import Fore, Back, Style, init
# Inisialisasi colorama
init(autoreset=True)
def check_required_args():
if len(sys.argv) < 6:
print('')
print(f"{Fore.RED}Error: Tidak cukup argumen!")
print('')
print(f"{Fore.YELLOW}Penggunaan: python3 script.py <api_id> <api_hash> <bot_token> <message> <chat_id> [opsional] <topik_id> <files_path>")
print('')
print(f'{Fore.YELLOW}Kirim pesan ke chat/channel (tanpa topic):')
print(f'{Fore.GREEN} > python3 main.py "API_ID" "API_HASH" "BOT_TOKEN" "Pesan Anda" "CHAT_ID"')
print('')
print(f'{Fore.YELLOW}Kirim pesan dan file ke chat/channel (tanpa topic):')
print(f'{Fore.GREEN} > python3 main.py "API_ID" "API_HASH" "BOT_TOKEN" "Pesan Anda" "CHAT_ID" "/path/to/file.txt"')
print('')
print(f'{Fore.YELLOW}Kirim pesan ke grup dengan topic:')
print(f'{Fore.GREEN} > python3 main.py "API_ID" "API_HASH" "BOT_TOKEN" "Pesan Anda" "GROUP_ID" "TOPIC_ID"')
print('')
print(f'{Fore.YELLOW}Kirim pesan dan file ke grup dengan topic:')
print(f'{Fore.GREEN} > python3 main.py "API_ID" "API_HASH" "BOT_TOKEN" "Pesan Anda" "GROUP_ID" "TOPIC_ID" "/path/to/file.txt')
print('')
print('')
sys.exit(1)
# Fungsi untuk mengirim pesan
async def send_message_to_chat(client, chat_id, message, topic_id=None):
try:
if topic_id:
await client.send_message(
entity=chat_id,
parse_mode='HTML',
message=message,
reply_to=topic_id
)
else:
await client.send_message(
entity=chat_id,
parse_mode='HTML',
message=message
)
print(f"{Fore.GREEN}✓ Pesan berhasil dikirim ke chat ID: {chat_id}")
except Exception as e:
print(f"{Fore.RED}✗ Error saat mengirim pesan: {e}")
# Fungsi untuk mengirim file
async def send_file_to_chat(client, chat_id, file_path, message, topic_id=None):
try:
if os.path.exists(file_path):
if topic_id:
await client.send_file(
chat_id,
file_path,
parse_mode='HTML',
caption=f"{message}",
reply_to=topic_id
)
else:
await client.send_file(
chat_id,
file_path,
parse_mode='HTML',
caption=f"{message}"
)
print(f"{Fore.GREEN}✓ File berhasil dikirim: {file_path}")
else:
print(f"{Fore.RED}✗ File tidak ditemukan: {file_path}")
except Exception as e:
print(f"{Fore.RED}✗ Error saat mengirim file: {e}")
# Fungsi untuk mengirim file Group
async def send_file_group_to_chat(client, chat_id, file_paths, message, topic_id=None, max_files_per_group=10):
try:
# Bagi file menjadi kelompok
file_groups = [file_paths[i:i + max_files_per_group] for i in range(0, len(file_paths), max_files_per_group)]
for group_index, file_group in enumerate(file_groups, 1):
# Tambahkan informasi kelompok ke pesan
group_message = f"{message}\n\n(ID {group_index}/{len(file_groups)})"
try:
if topic_id:
await client.send_file(
chat_id,
file_group,
parse_mode='HTML',
caption=group_message,
reply_to=topic_id
)
else:
await client.send_file(
chat_id,
file_group,
parse_mode='HTML',
caption=group_message
)
print(f"{Fore.GREEN}✓ Kelompok file {group_index} berhasil dikirim")
except Exception as e:
print(f"{Fore.RED}✗ Error saat mengirim kelompok file {group_index}: {e}")
except Exception as e:
print(f"{Fore.RED}✗ Error saat mengatur kelompok file: {e}")
async def main():
# Cek argumen wajib
check_required_args()
# Mengambil argumen wajib
api_id = sys.argv[1]
api_hash = sys.argv[2]
bot_token = sys.argv[3]
message = sys.argv[4]
chat_id = int(sys.argv[5])
# Print konfigurasi
print(f"\n{Fore.CYAN}Konfigurasi yang digunakan:")
print(f"{Fore.WHITE}API ID: {api_id}")
print(f"{Fore.WHITE}API Hash: {api_hash}")
print(f"{Fore.WHITE}Bot Token: {bot_token}")
print(f"{Fore.WHITE}Pesan: {message}")
print(f"{Fore.WHITE}Chat ID: {chat_id}")
# Inisialisasi client
client = TelegramClient('bot_session', api_id, api_hash)
await client.start(bot_token=bot_token)
print(f"\n{Fore.GREEN}✓ Bot telah aktif!")
try:
# Cek apakah ada Topic ID (argumen ke-6)
if len(sys.argv) > 6:
topic_id = int(sys.argv[6])
print(f"{Fore.WHITE}Topic ID: {topic_id}")
# Cek apakah ada file path (argumen ke-7)
if len(sys.argv) > 7:
file_path_pattern = sys.argv[7]
print(f"{Fore.WHITE}File Path: {file_path_pattern}")
# Dapatkan daftar file
file_paths = glob.glob(file_path_pattern)
if len(file_paths) > 1:
# Jika lebih dari satu file, gunakan fungsi grouping
await send_file_group_to_chat(client, chat_id, file_paths, message, topic_id)
elif len(file_paths) == 1:
# Jika hanya satu file, kirim seperti biasa
await send_file_to_chat(client, chat_id, file_paths[0], message, topic_id)
else:
# Kirim pesan ke grup dengan topic
await send_message_to_chat(client, chat_id, message, topic_id)
else:
# Kirim pesan normal
await send_message_to_chat(client, chat_id, message)
except Exception as e:
# Jika tidak ada Topic ID, kirim langsung ke chat/channel
try:
# Cek apakah ada file path (argumen ke-6)
if len(sys.argv) > 6:
file_path_pattern = sys.argv[6]
print(f"{Fore.WHITE}File Path: {file_path_pattern}")
# Dapatkan daftar file
file_paths = glob.glob(file_path_pattern)
if len(file_paths) > 1:
# Jika lebih dari satu file, gunakan fungsi grouping
await send_file_group_to_chat(client, chat_id, file_paths, message)
elif len(file_paths) == 1:
# Jika hanya satu file, kirim seperti biasa
await send_file_to_chat(client, chat_id, file_paths[0], message)
else:
# Kirim pesan normal
await send_message_to_chat(client, chat_id, message)
except Exception as e:
print(f"{Fore.RED}✗ Error: {e}")
finally:
await client.disconnect()
print(f"\n{Fore.YELLOW}Bot telah dinonaktifkan")
if __name__ == '__main__':
asyncio.run(main())