-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbot.py
316 lines (273 loc) · 9.87 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
311
312
313
314
315
"""
[emoji symbols]
orange box : 🔸
man with laptop : 👨💻
"""
import os
import telebot
from telebot import types
import random
import json
import threading ,schedule,time #extras
from utils.github_tools import *
TOKEN = open('test-key.txt').readline()
# TOKEN = os.getenv('TG_TOKEN')
cpp_data = json.load(open('utils/resource/cpp_resource.json'))
bot = telebot.TeleBot(TOKEN,parse_mode='HTML')
print("Bot is online")
hey_msg = ['Hi ','Hello ','Hey ']
bot_name = ['Developer','Coder','Mastermind','Cool','Resource']
user_name = ['Developer','Coder','Genius','Mastermind','Buddy','Programmer']
knownUsers = []
userStep = {}
#----------------Keyboard Layouts--------------------
#command_cpp keyboard [value=cpp]
cpp_select = types.ReplyKeyboardMarkup(one_time_keyboard=True)
cpp_select.add('Youtube videos','PDFs','Courses','Websites(Learning)','Websites(Practice)')
#command_github keyboard [value=github]
github_select = types.ReplyKeyboardMarkup(one_time_keyboard=True)
github_select.add('Search','Search(by user)')
github_select.add('Clone Repository')
hideBoard = types.ReplyKeyboardRemove() # hide the keyboard
commands = {
'start':'Restart bot',
'cpp' : 'C++ resources',
'github' : 'Search and clone Repository',
'codeforces' : 'still in development...',
'stackoverflow' : 'still in development...',
'help': 'Help',
'all' : 'List all commands',
}
#--------------------others functions----------------------
#get resource data
def cpp_resource(NAME):
for cpp in cpp_data:
if cpp['name'] == NAME:
text = ''
for i in cpp:
if i!='name':
text += '🔸 '
text += i
text += "\n"
text += cpp[i]
text += "\n\n"
return text
def get_user_step(uid):
if uid in userStep:
return userStep[uid]
else:
knownUsers.append(uid)
userStep[uid] = 0
print("New user detected")
return 0
#console output
def listener(messages):
for m in messages:
if m.content_type == 'text':
print(m.from_user.first_name +'['+ str(m.chat.id) + "] : " + m.text)
bot.set_update_listener(listener)
#------------------------All main commands--------------------------
@bot.message_handler(commands=['all'])
def command_all(m):
text = " All available commands 👨💻 : \n\n"
for key in commands:
text += "🔸 /" + key + " : "
text += commands[key] + "\n\n"
bot.send_message(m.chat.id, text)
@bot.message_handler(commands=['cpp'])
def command_cpp(m):
cid = m.chat.id
bot.send_message(cid, "What do you want ?", reply_markup=cpp_select)
userStep[cid] = 'cpp'
@bot.message_handler(commands=['github'])
def command_github(m):
cid = m.chat.id
bot.send_message(cid, "What do you want ?", reply_markup=github_select)
userStep[cid] = 'github'
@bot.message_handler(commands=['codeforces'])
def command_codeforces(m):
text = "Still in development..."
bot.reply_to(m, text)
@bot.message_handler(commands=['stackoverflow'])
def command_stackoverflow(m):
text = "Comming soon....."
bot.reply_to(m, text)
@bot.message_handler(commands=['help'])
def command_help(m):
bot.send_chat_action(m.chat.id, 'typing')
text = random.choice(hey_msg)
if m.chat.type == "private":
text += m.chat.first_name
else:
text += random.choice(user_name)
text += ' , I am a '+ random.choice(bot_name) + " Bot"
text += '\n\nI can do following things :'
text += '\n 🔸 Provide C++ Resources'
text += '\n 🔸 Github search , Clone Repository'
text += '\n 🔸 Codeforces Visualizer, Random Problems(comming soon)'
text += '\n 🔸 Stackoverflow Search (comming soon)'
text += "\n\nSee all commands at /all :)"
text += "\n\n\nContact Developer 👨💻: @vishal2376"
bot.reply_to(m, text)
@bot.message_handler(commands=['start'])
def command_start(m):
cid = m.chat.id
if cid not in knownUsers:
knownUsers.append(cid)
userStep[cid] = 0
command_help(m)
#-------------------Custom keyboard functions-------------------
#---------------------------cpp-----------------------------
@bot.message_handler(func=lambda message: get_user_step(message.chat.id) == 'cpp')
def msg_cpp_select(m):
cid = m.chat.id
# userStep[cid] = 0
bot.send_chat_action(cid, 'typing')
if m.text == 'Youtube videos':
text = cpp_resource('yt')
bot.send_message(cid,text,disable_web_page_preview=True,reply_markup=hideBoard)
elif m.text =='PDFs':
text = cpp_resource('pdf')
bot.send_message(cid,text,disable_web_page_preview=True,reply_markup=hideBoard)
elif m.text =='Websites(Practice)':
text = cpp_resource('practice_websites')
bot.send_message(cid,text,disable_web_page_preview=True,reply_markup=hideBoard)
elif m.text =='Websites(Learning)':
text = cpp_resource('learning_websites')
bot.send_message(cid,text,disable_web_page_preview=True,reply_markup=hideBoard)
elif m.text =='Courses':
text = cpp_resource('courses')
bot.send_message(cid,text,disable_web_page_preview=True)
else:
bot.send_message(cid, "Invalid Commands")
#-------------------github all functions-------------------------
#[value=github]
@bot.message_handler(func=lambda message: get_user_step(message.chat.id) == 'github')
def msg_github_select(m):
cid = m.chat.id
userStep[cid] = 0
bot.send_chat_action(cid, 'typing')
if m.text == 'Search':
# text = 'Not available for some days'
text = 'Enter your query '
bot.send_message(m.chat.id,text,reply_markup=hideBoard)
userStep[cid] = 'github_search'
elif m.text == 'Search(by user)':
text = 'Enter username \nExample : vishal2376'
bot.send_message(m.chat.id,text,reply_markup=hideBoard)
userStep[cid] = 'github_search_user'
elif m.text == 'Clone Repository':
text = 'Enter username \nExample : vishal2376'
bot.send_message(m.chat.id,text,reply_markup=hideBoard)
userStep[cid] = 'github_clone_view'
else:
bot.send_message(cid, "Invalid Commands")
#[value=github_clone]
@bot.message_handler(func=lambda message: get_user_step(message.chat.id) == 'github_clone')
def msg_github_clone(m):
cid = m.chat.id
userStep[cid] = 0
user_name = ''
with open('./user_name.txt','r') as f:
user_name = f.readline()
repo_list = get_repo_list(user_name)
try:
for repo_name in repo_list:
if m.text == '/stop':
bot.send_message(cid,"Successfully stopped",reply_markup=hideBoard)
break
elif m.text == repo_name:
full_repo = user_name +'/'+repo_name
bot.send_chat_action(cid, 'typing')
text = 'https://github.com/'+full_repo+'/archive/refs/heads/master.zip'
msg = bot.send_message(cid,text,reply_markup=hideBoard)
except Exception as e:
bot.send_message(cid,'Something went Wrong',reply_markup=hideBoard)
print('Error : Failed to download or send files')
#[value=github_clone_view]
@bot.message_handler(func=lambda message: get_user_step(message.chat.id) == 'github_clone_view')
def view_clone_repo(m):
try:
repo_list = get_repo_list(m.text)
button = types.ReplyKeyboardMarkup(one_time_keyboard=True)
for repo_name in repo_list:
button.add(repo_name)
bot.send_message(m.chat.id,'Click button to clone Repository \n\nUse /stop to exit',reply_markup=button)
userStep[m.chat.id] = 'github_clone'
except Exception as e:
bot.send_message(m.chat.id,'Something went wrong , Try again later',reply_markup=hideBoard)
print("Error : Keyboard not created")
#[value=github_search_user]
@bot.message_handler(func=lambda message: get_user_step(message.chat.id) == 'github_search_user')
def view_user_repo(m):
try:
userStep[m.chat.id]=0
repo_list = get_repo_list(m.text,1)
text = 'Github Repository List\n\n'
for repo_name in repo_list:
name = repo_name.split('/')[1]
stars = get_repo_stars(repo_name)
issues = get_repo_issues(repo_name)
text += '🔸 <b>'+ name + '</b>\n'
text += 'Stars : '+str(stars)+' | Issues : '+ str(issues)
text += '\n<a href = "https://github.com/'+ repo_name + '">Click here to visit</a>\n\n'
bot.send_message(m.chat.id,text,disable_web_page_preview=True)
except Exception as e:
bot.send_message(m.chat.id,'Github API search Limit exceed',reply_markup=hideBoard)
print("Error : Github search Limit exceed")
#[value=github_search]
@bot.message_handler(func=lambda message: get_user_step(message.chat.id) == 'github_search')
def view_repo(m):
try:
userStep[m.chat.id]=0
repo_list = search_repo(m.text)
text = 'Top Search Results \n\n'
for repo_name in repo_list:
#name = repo_name.split('/')[1]
stars = get_repo_stars(repo_name)
issues = get_repo_issues(repo_name)
text += '🔸 '+ repo_name + '\n'
text += 'Stars : '+str(stars)+' | Issues : '+ str(issues)
text += '\n<a href = "https://github.com/'+ repo_name + '">Click here to visit</a>\n\n'
bot.send_message(m.chat.id,text,disable_web_page_preview=True)
except Exception as e:
bot.send_message(m.chat.id,'Github API search Limit exceed',reply_markup=hideBoard)
print("Error : Search limit exceed")
#-------------------------------------------------
#-----------------EXTRA-------------------------
run = True
rem_time = '20:15'
def remainder():
msg = "⏰ [Remainder]\n\n"
msg += "👨💻 Codeforces Contest will start in 15min.\n"
msg += "\nLink : "
msg += rem_link
bot.send_message(grp_id,msg,disable_web_page_preview=True)
global run
run = False
return schedule.CancelJob
#-------------------------------------------------
#-------------------------------------------------
# filter message
@bot.message_handler(func=lambda message: True, content_types=['text'])
def command_default(m):
lower_text = m.text.lower()
if lower_text == 'hello' or lower_text == 'hi':
text = random.choice(hey_msg)
text += m.from_user.first_name
bot.reply_to(m, text)
if 'https://codeforces.com/contestInvitation' in m.text:
global grp_id
global rem_link
grp_id = m.chat.id
rem_link = m.text
schedule.every().day.at(rem_time).do(remainder)
text = "⏰ Codeforces remainder scheduled\n"
bot.send_message(m.chat.id,text)
def forever():
while run:
schedule.run_pending()
time.sleep(1)
t1 = threading.Thread(target=forever,name='remainder')
t1.start()
bot.polling()