-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
132 lines (107 loc) · 4.66 KB
/
main.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
from dependency import *
from datetime import datetime
async def start_command(event):
user_id = event.sender_id
json_data = get_user_data_from_json(user_id)
if json_data:
reply = "Welcome back! Your user data has been loaded from the JSON file. Use /help command to view the list of commands."
else:
reply = "Hello! I am your message forwarding bot. Please use the /help command to view the list of commands."
# Initialize user data if not present in memory
if str(user_id) not in user_data:
user_data[(user_id)] = {}
await event.reply(reply)
# Command to input source channel link
async def source_channel_command(event):
user_id = event.sender_id
try:
if str(user_id) not in user_data:
user_data[str(user_id)] = {}
user_data[str(user_id)]["source_channel_link"] = event.raw_text.split(
maxsplit=1
)[1]
update_github_file(PAT, REPO_OWNER, REPO_NAME, FILE_PATH, user_data)
reply = "Source channel link saved. Please use the /destination."
except IndexError:
reply = "Please send /source followed-by-the-source-url"
await event.reply(reply)
# Command to input destination channel link
async def destination_channel_command(event):
if event.sender_id == 6522874768:
try:
user_id = event.sender_id
if str(user_id) not in user_data:
user_data[str(user_id)] = {}
user_data[str(user_id)]["destination_channel_link"] = event.raw_text.split(
maxsplit=1
)[1]
update_github_file(PAT, REPO_OWNER, REPO_NAME, FILE_PATH, user_data)
reply = "Destination channel link saved. Please use the /datestart."
except IndexError:
reply = "Please send /destination followed-by-the-source-url"
await event.reply(reply)
# Command to input start date
async def start_date_command(event):
user_id = event.sender_id
try:
date_str = event.raw_text.split(maxsplit=1)[1]
parsed_date = datetime.strptime(date_str, "%Y-%m-%d %H:%M")
if str(user_id) not in user_data:
user_data[str(user_id)] = {}
user_data[str(user_id)]["start_date_input"] = parsed_date.strftime(
"%Y-%m-%d %H:%M"
) # Convert to string
update_github_file(PAT, REPO_OWNER, REPO_NAME, FILE_PATH, user_data)
reply = "Start date saved. Please use the /enddate command next."
except ValueError:
reply = "Invalid date format. Please use the format 'YYYY-MM-DD HH:MM'."
except IndexError:
reply = "Please send /datestart followed-by-the-begining-date in 'YYYY-MM-DD HH:MM' format"
await event.reply(reply)
async def end_date_command(event):
user_id = event.sender_id
# Get the date string
try:
date_str = event.raw_text.split(maxsplit=1)[1]
parsed_date = datetime.strptime(date_str, "%Y-%m-%d %H:%M")
if str(user_id) not in user_data:
user_data[str(user_id)] = {}
user_data[str(user_id)]["end_date_input"] = parsed_date.strftime(
"%Y-%m-%d %H:%M"
) # Convert to string
update_github_file(PAT, REPO_OWNER, REPO_NAME, FILE_PATH, user_data)
reply = "End date saved. You can now use the /settings command to view your saved data."
except ValueError:
reply = "Invalid date format. Please use the format 'YYYY-MM-DD HH:MM'."
except IndexError:
reply = "Please send /enddate followed-by-the-end-date in 'YYYY-MM-DD HH:MM' format"
await event.reply(reply)
# Command to view saved data
async def settings_command(event):
user_id = event.sender_id
data = get_user_data_from_json(user_id)
if data:
response = (
f"Your saved data:\nSource Channel: {data.get('source_channel_link')}\n"
f"Destination Channel: {data.get('destination_channel_link')}\n"
f"Start Date: {data.get('start_date_input')}\n"
f"End Date: {data.get('end_date_input')}"
)
await event.reply(response)
else:
await event.reply(
"No saved data found. Use the relevant commands to save your data."
)
async def help_command(event):
help_message = (
"Available commands:\n"
"/source the-source-channel-link\n"
"/destination the-destination-channel-link\n"
"/datestart the-start-date\n"
"/enddate the-end-date\n"
"/settings - View your saved data\n"
"/help - Show this help message\n"
"/forward - Start the message forwarding process"
)
await event.reply(help_message)
update_github_file(PAT, REPO_OWNER, REPO_NAME, FILE_PATH, user_data)