Spaces:
Runtime error
Runtime error
aheskandani
commited on
Commit
•
6f78cec
1
Parent(s):
fcee93f
Update app.py
Browse files
app.py
CHANGED
@@ -10,28 +10,111 @@ from telegram.ext import (Updater,
|
|
10 |
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ForceReply, ReplyKeyboardMarkup
|
11 |
import requests
|
12 |
import hashlib
|
13 |
-
|
14 |
from bs4 import BeautifulSoup
|
15 |
-
import urllib.request
|
16 |
|
17 |
bot_token = '5415345086:AAEpiZypEMxgEs5zMcyOBAx_CpT9iLcnE8s'
|
18 |
admin = 37087739
|
19 |
|
|
|
|
|
|
|
|
|
20 |
def start(update: Update, context: CallbackContext):
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
def
|
25 |
-
''' Entry point of conversation this gives buttons to user'''
|
26 |
chat_id = update.effective_chat.id
|
27 |
-
|
28 |
-
|
29 |
-
context.
|
30 |
-
context.
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
def captcha_url():
|
37 |
web_url = 'https://pooya.um.ac.ir/gateway/PuyaAuthenticate.php'
|
@@ -45,8 +128,24 @@ def captcha_url():
|
|
45 |
return capcha_src
|
46 |
|
47 |
if __name__ == '__main__':
|
48 |
-
|
|
|
|
|
|
|
49 |
updater.dispatcher.add_handler(CommandHandler('start', start))
|
50 |
-
updater.dispatcher.add_handler(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
updater.start_polling()
|
52 |
print('Bot started ..........')
|
|
|
10 |
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ForceReply, ReplyKeyboardMarkup
|
11 |
import requests
|
12 |
import hashlib
|
13 |
+
import re
|
14 |
from bs4 import BeautifulSoup
|
|
|
15 |
|
16 |
bot_token = '5415345086:AAEpiZypEMxgEs5zMcyOBAx_CpT9iLcnE8s'
|
17 |
admin = 37087739
|
18 |
|
19 |
+
#############################################
|
20 |
+
|
21 |
+
EXPECT_STATUS, EXPECT_USERNAME, EXPECT_PASSWORD, EXPECT_SUBMIT, EXPECT_BACK, EXPECT_BUTTON_CLICK = range(6)
|
22 |
+
|
23 |
def start(update: Update, context: CallbackContext):
|
24 |
+
chat_id = update.effective_chat.id
|
25 |
+
text = ('Hello dear user! 🌹\n'
|
26 |
+
'Please enter the username or instagram url\n\n'
|
27 |
+
'Powered by @eskandani')
|
28 |
+
update.message.reply_text(text, reply_markup=main_keyboard)
|
29 |
+
|
30 |
+
|
31 |
+
def set_info_handler(update: Update, context: CallbackContext):
|
32 |
+
username = context.user_data.get('username', 'تنظیم نشده است')
|
33 |
+
password = context.user_data.get('password', 'تنظیم نشده است')
|
34 |
+
text = (f'نام کاربری: {username}\n'
|
35 |
+
f'رمز عبور: {password}\n\n'
|
36 |
+
'لطفا یک گزینه را انتخاب نمایید')
|
37 |
+
button = [ [InlineKeyboardButton('نام کاربری', callback_data='username'),
|
38 |
+
InlineKeyboardButton('رمز عبور', callback_data='password')],
|
39 |
+
[InlineKeyboardButton('ورود و دریافت نمرات', callback_data='login_portal')],
|
40 |
+
[InlineKeyboardButton('بازگشت', callback_data='cancel')],
|
41 |
+
]
|
42 |
+
inline_keyboard = InlineKeyboardMarkup(button)
|
43 |
+
update.message.reply_text(text, reply_markup=inline_keyboard)
|
44 |
+
return EXPECT_BUTTON_CLICK
|
45 |
+
|
46 |
+
def button_click_handler(update: Update, context: CallbackContext):
|
47 |
+
query = update.callback_query
|
48 |
+
query.answer()
|
49 |
+
query.delete_message()
|
50 |
+
if query.data == 'login_portal':
|
51 |
+
web_url = 'https://pooya.um.ac.ir/gateway/PuyaAuthenticate.php'
|
52 |
+
resp = session.get(web_url, headers=headers)
|
53 |
+
soup = BeautifulSoup(resp.content, 'html.parser')
|
54 |
+
img_src = soup.find('img', id='secimg')['src']
|
55 |
+
filename = 'C:/Users/Home/OneDrive/Desktop/captcha.png'
|
56 |
+
capcha_src = 'https://pooya.um.ac.ir' + img_src[2:]
|
57 |
+
context.user_data['captcha'] = capcha_src
|
58 |
+
context.bot.send_message(chat_id, text=capcha_src)
|
59 |
+
context.bot.send_message(chat_id, text='کد کپچا را وارد کنید', reply_markup=ForceReply())
|
60 |
+
return EXPECT_SUBMIT
|
61 |
+
elif query.data == 'username':
|
62 |
+
context.bot.send_message(chat_id, text='نام کاربری خود را وارد کنید', reply_markup=ForceReply())
|
63 |
+
return EXPECT_USERNAME
|
64 |
+
elif query.data == 'password':
|
65 |
+
context.bot.send_message(chat_id, text='رمز عبور خود را وارد کنید', reply_markup=ForceReply())
|
66 |
+
return EXPECT_PASSWORD
|
67 |
+
elif query.data == 'cancel':
|
68 |
+
query.answer('Opeartion canceled!')
|
69 |
+
return ConversationHandler.END
|
70 |
+
|
71 |
+
|
72 |
+
def hash_password(text):
|
73 |
+
password = hashlib.md5()
|
74 |
+
password.update(text.encode('utf-8'))
|
75 |
+
return password.hexdigest()
|
76 |
+
|
77 |
|
78 |
+
def login_portal(update: Update, context: CallbackContext):
|
|
|
79 |
chat_id = update.effective_chat.id
|
80 |
+
captcha = update.message.text
|
81 |
+
USER = context.user_data['username']
|
82 |
+
PASS = context.user_data['password']
|
83 |
+
capcha_src = context.user_data['captcha']
|
84 |
+
login_data = { 'UserPassword': hash_password(PASS),
|
85 |
+
'pswdStatus': '',
|
86 |
+
'UserID': str(USER),
|
87 |
+
'DummyVar': '',
|
88 |
+
'rand2': capcha_src[capcha_src.find('rand2=')+6:],
|
89 |
+
'mysecpngco': captcha_val
|
90 |
+
}
|
91 |
+
req_url = 'https://pooya.um.ac.ir/gateway/UserInterim.php'
|
92 |
+
resp = session.post(req_url, data=login_data)
|
93 |
+
if resp.status_code == 200:
|
94 |
+
grade_list = session.get('https://pooya.um.ac.ir/educ/educfac/stuShowEducationalLogFromGradeList.php', headers=headers)
|
95 |
+
soup = BeautifulSoup(grade_list.content, 'html.parser')
|
96 |
+
data = [item.text.splitlines() for item in soup.find_all('tr', bgcolor="#E8E8FF")]
|
97 |
+
mean = [item.text.splitlines() for item in soup.find_all('tr', bgcolor="#C4C4FF")][1][3]
|
98 |
+
mark = '\n'.join([f'{i+1}) {row[2]}, {row[5]} ({row[8]})' for i, row in enumerate(data)])
|
99 |
+
text = f'{mark}\nمعدل: {mean}'
|
100 |
+
context.bot.send_message(chat_id, text)
|
101 |
+
else:
|
102 |
+
print('Login faild!')
|
103 |
+
return ConversationHandler.END
|
104 |
+
|
105 |
+
|
106 |
+
def set_password(update: Update, context: CallbackContext):
|
107 |
+
context.user_data['password'] = update.message.text
|
108 |
+
update.message.reply_text('رمز عبور با موفقیت ثبت شد', reply_markup=main_keyboard)
|
109 |
+
return ConversationHandler.END
|
110 |
+
|
111 |
+
|
112 |
+
def set_username(update: Update, context: CallbackContext):
|
113 |
+
context.user_data['username'] = update.message.text
|
114 |
+
update.message.reply_text('نام کاربری با موفقیت ثبت شد', reply_markup=main_keyboard)
|
115 |
+
return ConversationHandler.END
|
116 |
+
|
117 |
+
#############################################
|
118 |
|
119 |
def captcha_url():
|
120 |
web_url = 'https://pooya.um.ac.ir/gateway/PuyaAuthenticate.php'
|
|
|
128 |
return capcha_src
|
129 |
|
130 |
if __name__ == '__main__':
|
131 |
+
session = requests.session()
|
132 |
+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'}
|
133 |
+
pers = PicklePersistence(filename='mybot')
|
134 |
+
updater = Updater(token=bot_token, persistence=pers)
|
135 |
updater.dispatcher.add_handler(CommandHandler('start', start))
|
136 |
+
updater.dispatcher.add_handler(ConversationHandler(
|
137 |
+
entry_points=[MessageHandler(Filters.regex('ورود به پرتال دانشجویی'), set_info_handler)],
|
138 |
+
states={
|
139 |
+
EXPECT_USERNAME: [MessageHandler(Filters.text, set_username)],
|
140 |
+
EXPECT_PASSWORD: [MessageHandler(Filters.text, set_password)],
|
141 |
+
EXPECT_SUBMIT: [MessageHandler(Filters.text, login_portal)],
|
142 |
+
EXPECT_BUTTON_CLICK: [CallbackQueryHandler(button_click_handler)],
|
143 |
+
EXPECT_BACK : [CallbackQueryHandler('cancel', start)]
|
144 |
+
},
|
145 |
+
fallbacks=[MessageHandler(Filters.regex('cancel'), cancel)],
|
146 |
+
))
|
147 |
+
buttons = [['ورود به پرتال دانشجویی']]
|
148 |
+
main_buttons = [row[::-1] for row in buttons]
|
149 |
+
main_keyboard = ReplyKeyboardMarkup(main_buttons, one_time_keyboard=True, resize_keyboard=True)
|
150 |
updater.start_polling()
|
151 |
print('Bot started ..........')
|