-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
194 lines (162 loc) · 7.75 KB
/
main.py
File metadata and controls
194 lines (162 loc) · 7.75 KB
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
import os
from dotenv import load_dotenv
import logging
from datetime import datetime, timedelta
from pyrogram import Client, filters
from pyrogram.enums.parse_mode import ParseMode
# Enable logging
logging.basicConfig(level=logging.INFO)
# Initialize Pyrogram client
load_dotenv()
API_ID = os.getenv("API_ID")
API_HASH = os.getenv("API_HASH")
SESSION_STRING = os.getenv("SESSION_STRING")
# Get admin IDs from environment variables
ADMIN_IDS_STR = os.getenv("ADMIN_IDS", "")
ADMIN_IDS = [int(admin_id.strip()) for admin_id in ADMIN_IDS_STR.split(",") if admin_id.strip()]
app = Client(
name="my_session",
api_id=API_ID,
api_hash=API_HASH,
# session_string=SESSION_STRING
)
@app.on_message(filters.command("start"))
async def start_command(client, message):
response_text = (
"Hello! I'm the Content Caster bot.\n\n"
"I can help you schedule messages from one Telegram chat to another.\n\n"
"Type `/help` to learn about available commands and how to use them.\n\n"
"Enjoy scheduling messages with Content Caster!"
)
await message.reply_text(response_text, parse_mode=ParseMode.MARKDOWN)
@app.on_message(filters.command("help"))
async def help_command(client, message):
response_text = (
"Here are the commands you can use with the Content Caster bot:\n\n"
"`/start` - Start the bot and get a brief introduction.\n\n"
"`/schedule` <src_chat_id> <dest_chat_id> <start_message_id> <end_message_id> <start_time (YYYY-MM-DD-HH:MM:SS)> <interval in hrs> [custom_message] - Schedule messages from one chat to another.\n\n"
"Example:\n"
"/schedule 123456789 987654321 1 100 2024-06-30-10:00:00 1 \"Join our channel @YourChannel\"\n\n"
"This command schedules messages from message ID 1 to 100 from chat ID 123456789 to chat ID 987654321, starting on June 30, 2024, at 10:00 AM, with a 1-hour interval between messages. If custom_message is provided, it will replace the caption for media messages.\n\n"
"Use `/info` in reply to a message to get the message ID and chat ID.\n\n"
"Enjoy scheduling messages with Content Caster!"
)
await message.reply_text(response_text, parse_mode=ParseMode.MARKDOWN)
@app.on_message(filters.command("info"))
def info_command(client, message):
mess = f"Message ID : `{message.reply_to_message_id}`\nChat ID : `{message.chat.id}`"
message.reply_text(text = mess, parse_mode =ParseMode.MARKDOWN)
async def fetch_and_schedule_messages(src_chat_id, dest_chat_id, start_message_id, end_message_id, start_time, interval, chat_id, custom_message=None):
current_time = start_time
message_id = start_message_id
scheduler_counter = 0
while message_id <= end_message_id:
try:
if scheduler_counter >= 95:
current_time_str = current_time.strftime("%Y-%m-%d-%H:%M:%S")
interval_hours = int(interval.total_seconds()/3600)
command = f"/schedule {src_chat_id} {dest_chat_id} {message_id} {end_message_id} {current_time_str} {interval_hours}"
if custom_message:
command += f" {custom_message}"
await app.send_message(chat_id = chat_id, text=f"Scheduler limit has been reached. To continue from here, send the following command at {current_time}: \n`{command}`",parse_mode=ParseMode.MARKDOWN)
await app.send_message(chat_id = chat_id, text=command, schedule_date=current_time, parse_mode=ParseMode.MARKDOWN)
return
message = await app.get_messages(src_chat_id, message_id)
if message:
if await schedule_message(message, dest_chat_id, current_time, custom_message):
logging.info(f"Scheduled message {message_id} for {current_time}")
current_time += interval
scheduler_counter += 1
except Exception as e:
logging.warning(f"Error: {e}")
message_id += 1
async def schedule_message(message, dest_chat_id, schedule_time, custom_message=None):
if message.text:
await app.send_message(
chat_id=dest_chat_id,
text=message.text,
parse_mode=ParseMode.MARKDOWN,
schedule_date=schedule_time
)
return True
elif message.photo:
await app.send_photo(
chat_id=dest_chat_id,
photo=message.photo.file_id,
caption=custom_message if custom_message else message.caption,
parse_mode=ParseMode.MARKDOWN,
schedule_date=schedule_time
)
return True
elif message.video:
await app.send_video(
chat_id=dest_chat_id,
video=message.video.file_id,
caption=custom_message if custom_message else message.caption,
parse_mode=ParseMode.MARKDOWN,
schedule_date=schedule_time
)
return True
elif message.document:
await app.send_document(
chat_id=dest_chat_id,
document=message.document.file_id,
caption=custom_message if custom_message else message.caption,
parse_mode=ParseMode.MARKDOWN,
schedule_date=schedule_time
)
return True
else:
logging.warning(f"Unable to Schedule message : {message.id}")
return False
@app.on_message(filters.command("schedule"))
async def schedule_handler(client, message):
try:
# Check if user is an admin
user_id = message.from_user.id
if user_id not in ADMIN_IDS:
await message.reply_text("You are not authorized to use this command.")
return
chat_id = message.chat.id
# Split by space but preserve quoted strings
text_parts = message.text.split()
args = []
i = 0
while i < len(text_parts):
if text_parts[i].startswith('"') and not text_parts[i].endswith('"'):
# Start of a quoted string
quoted_string = text_parts[i]
i += 1
while i < len(text_parts) and not text_parts[i].endswith('"'):
quoted_string += ' ' + text_parts[i]
i += 1
if i < len(text_parts):
quoted_string += ' ' + text_parts[i]
args.append(quoted_string.strip('"'))
else:
args.append(text_parts[i].strip('"'))
i += 1
if len(args) < 7:
await message.reply_text("Usage: /schedule <src_chat_id> <dest_chat_id> <start_message_id> <end_message_id> <start_time> <interval in hrs> [custom_message]")
return
src_chat_id = int(args[1])
dest_chat_id = int(args[2])
start_message_id = int(args[3])
end_message_id = int(args[4])
start_time_str = args[5]
interval_hours = int(args[6])
# Get custom message if provided
custom_message = None
if len(args) > 7:
custom_message = args[7]
start_time = datetime.strptime(start_time_str, "%Y-%m-%d-%H:%M:%S")
interval = timedelta(hours=interval_hours)
message_info = f"Scheduling messages from {start_message_id} to {end_message_id} from chat {src_chat_id} to {dest_chat_id} starting at {start_time} every {interval_hours} hours."
if custom_message:
message_info += f"\nCustom message: {custom_message}"
await message.reply_text(message_info)
await fetch_and_schedule_messages(src_chat_id, dest_chat_id, start_message_id, end_message_id, start_time, interval, chat_id, custom_message)
except Exception as e:
logging.error(f"Error in scheduling: {e}")
await message.reply_text(f"An error occurred: {e}")
app.run()