-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
310 lines (238 loc) · 10.5 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
from pyrogram.types import Message
from pyrogram import Client, filters
from github import Github
from os import environ
import os
api_id = int(environ["API_ID"])
api_hash = environ["API_HASH"]
bot_token = environ["BOT_TOKEN"]
github_access_token = environ["GITHUB_ACCESS_TOKEN"]
github_repository_link = environ["GITHUB_REPOSITORY_LINK"]
instagram_file_from_same_repository = environ["INSTGRAM_FILE_FROM_SAME_REPOSITORY"]
other_file_from_same_repository = environ["OTHER_FILE_FROM_SAME_REPOSITORY"]
youtube_file_from_same_repository = environ["YOUTUBE_FILE_FROM_SAME_REPOSITORY"]
notes_file_from_same_repository = environ["NOTES_FILE_FROM_SAME_REPOSITORY"]
app = Client(":memory:", api_id, api_hash, bot_token=bot_token)
@app.on_message(filters.command(["start"]))
def start(client: Client, message: Message):
msg = app.send_message(message.from_user.id,"Welcome",reply_to_message_id=message.message_id)
@app.on_message(filters.regex(r'#read'))
def read_files(client: Client, message: Message):
working_msg = app.send_message(message.from_user.id,"Working on it.",reply_to_message_id=message.message_id)
#login
g = Github(github_access_token)
#getting text
msg_textedd = (message.text)
msg_texted = msg_textedd.lstrip("#read ")
print(msg_texted)
#getting repo and files
repo = g.get_repo(github_repository_link)
contents = repo.get_contents(msg_texted, ref="main")
reading_file = contents.decoded_content.decode()
#sending message
msg = app.send_message(chat_id=message.from_user.id, reply_to_message_id=message.message_id, text="The content of " + "<b>" + msg_texted + "</b>" + " file:" + "\n\n" + reading_file,parse_mode="html")
#delete message
working_msg.delete()
@app.on_message(filters.regex(r'#ls'))
def list_files(client: Client, message: Message):
working_msg = app.send_message(message.from_user.id,"Working on it.",reply_to_message_id=message.message_id)
#login
g = Github(github_access_token)
#getting repo and files
repo = g.get_repo(github_repository_link)
all_files = []
contents = repo.get_contents("")
while contents:
file_content = contents.pop(0)
if file_content.type == "dir":
contents.extend(repo.get_contents(file_content.path))
else:
file = file_content
all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))
all_filess = str(all_files).lstrip("['").rstrip("']").replace("'","")
#sending message
msg = app.send_message(chat_id=message.from_user.id, reply_to_message_id=message.message_id, text="These files are in your current repository:" + "\n\n"+ "<b>" + all_filess + "</b>",parse_mode="html")
#delete message
working_msg.delete()
@app.on_message(filters.regex(r'#get'))
def get(client: Client, message: Message):
working_msg = app.send_message(message.from_user.id,"Working on it.",reply_to_message_id=message.message_id)
#login
g = Github(github_access_token)
#getting text
msg_textedd = (message.text)
msg_texted = msg_textedd.lstrip("#get ")
#getting repo and file
repo = g.get_repo(github_repository_link)
contents = repo.get_contents(msg_texted, ref="main")
aaa = contents.decoded_content.decode()
#copying old with new
f = open(msg_texted,"w")
f.write(aaa)
f.close()
document = (open(msg_texted,'rb'))
#sending message
app.send_document(chat_id=message.from_user.id,reply_to_message_id=message.message_id,document=document)
msg = app.send_message(message.from_user.id,"Done.",reply_to_message_id=message.message_id)
#removing files
os.remove(msg_texted)
#delete message
working_msg.delete()
@app.on_message(filters.regex(r'#other'))
def other(client: Client, message: Message):
working_msg = app.send_message(message.from_user.id,"Working on it.",reply_to_message_id=message.message_id)
#login
g = Github(github_access_token)
#getting text
msg_textedd = (message.text)
msg_texted = msg_textedd.lstrip("#other ")
#getting repo and file
repo = g.get_repo(github_repository_link)
contents = repo.get_contents(other_file_from_same_repository, ref="main")
aaa = contents.decoded_content.decode()
#copying old with new
aa = msg_texted + "\n" + aaa
f = open("file.txt","w")
f.write(aa)
f.close()
#removing duplicate
lines_seen = set()
outfile = open("filee.txt", "w")
for line in open("file.txt", "r"):
if line not in lines_seen:
outfile.write(line)
lines_seen.add(line)
outfile.close()
ff = open("filee.txt","r")
content = ff.read()
#updating file
repo.update_file(contents.path, "updated", content, contents.sha, branch="main")
#removing files
os.remove("file.txt")
os.remove("filee.txt")
#sending message
msg = app.send_message(message.from_user.id,"Done.",reply_to_message_id=message.message_id)
#delete message
working_msg.delete()
@app.on_message(filters.regex(r'#youtube|youtube.com|youtu.be'))
def youtube(client: Client, message: Message):
working_msg = app.send_message(message.from_user.id,"Working on it.",reply_to_message_id=message.message_id)
#login
g = Github(github_access_token)
#getting text
msg_textedd = (message.text)
if '#youtube' in msg_textedd:
msg_texted = msg_textedd.lstrip("#youtube ")
else:
msg_texted = (message.text)
#getting repo and file
repo = g.get_repo(github_repository_link)
contents = repo.get_contents(youtube_file_from_same_repository, ref="main")
aaa = contents.decoded_content.decode()
#copying old with new
aa = msg_texted + "\n" + aaa
f = open("file.txt","w")
f.write(aa)
f.close()
#removing duplicate
lines_seen = set()
outfile = open("filee.txt", "w")
for line in open("file.txt", "r"):
if line not in lines_seen:
outfile.write(line)
lines_seen.add(line)
outfile.close()
ff = open("filee.txt","r")
content = ff.read()
#updating file
repo.update_file(contents.path, "updated", content, contents.sha, branch="main")
#removing files
os.remove("file.txt")
os.remove("filee.txt")
#sending message
msg = app.send_message(message.from_user.id,"Done.",reply_to_message_id=message.message_id)
#delete message
working_msg.delete()
@app.on_message(filters.regex(r'#insta|instagram.com'))
def instagram(client: Client, message: Message):
working_msg = app.send_message(message.from_user.id,"Working on it.",reply_to_message_id=message.message_id)
#login
g = Github(github_access_token)
#getting text
msg_textedd = (message.text)
if '#insta' in msg_textedd:
msg_texted = msg_textedd.lstrip("#insta ")
else:
msg_texted = (message.text)
#getting repo and file
repo = g.get_repo(github_repository_link)
contents = repo.get_contents(instagram_file_from_same_repository, ref="main")
aaa = contents.decoded_content.decode()
#copying old with new
aa = msg_texted + "\n" + aaa
f = open("file.txt","w")
f.write(aa)
f.close()
#removing duplicate
lines_seen = set()
outfile = open("filee.txt", "w")
for line in open("file.txt", "r"):
if line not in lines_seen:
outfile.write(line)
lines_seen.add(line)
outfile.close()
ff = open("filee.txt","r")
content = ff.read()
#updating file
repo.update_file(contents.path, "updated", content, contents.sha, branch="main")
#removing files
os.remove("file.txt")
os.remove("filee.txt")
#sending message
msg = app.send_message(message.from_user.id,"Done.",reply_to_message_id=message.message_id)
#delete message
working_msg.delete()
@app.on_message(filters.document)
def notes_file(client: Client, message: Message):
working_msg = app.send_message(message.from_user.id,"Working on it.",reply_to_message_id=message.message_id)
#login
g = Github(github_access_token)
#getting file id
file_id = None
if message.media:
if message.document:
file_id = message.document.file_id
#downloading
download_file = app.download_media(file_id)
#read file
with open(download_file, 'r') as file:
contentss = file.read()
#getting repo and file
repo = g.get_repo(github_repository_link)
contents = repo.get_contents(notes_file_from_same_repository, ref="main")
aaa = contents.decoded_content.decode()
#copying old with new
aa = contentss + "\n" + aaa
f = open("file.txt","w")
f.write(aa)
f.close()
#removing duplicate
lines_seen = set()
outfile = open("filee.txt", "w")
for line in open("file.txt", "r"):
if line not in lines_seen:
outfile.write(line)
lines_seen.add(line)
outfile.close()
ff = open("filee.txt","r")
content = ff.read()
#updating file
repo.update_file(contents.path, "updated", content, contents.sha, branch="main")
#removing files
os.remove("file.txt")
os.remove("filee.txt")
#sending message
msg = app.send_message(message.from_user.id,"Done.",reply_to_message_id=message.message_id)
#delete message
working_msg.delete()
app.run()