File size: 6,161 Bytes
352eac8
 
 
 
 
 
 
 
 
 
8e31d0d
 
f15ce6e
6f78cec
352eac8
 
94a70ff
352eac8
 
6f78cec
 
 
 
352eac8
6f78cec
23990d0
6f78cec
 
 
 
 
 
 
 
 
23990d0
 
6f78cec
 
 
 
 
 
 
 
23990d0
6f78cec
 
 
 
 
 
 
 
 
 
 
c4893e3
6f78cec
 
23990d0
6f78cec
 
23990d0
6f78cec
 
 
 
 
 
 
 
f15ce6e
 
 
6f78cec
352eac8
6f78cec
352eac8
6f78cec
 
 
 
 
 
bf60dda
6f78cec
 
bf60dda
6f78cec
 
 
 
 
 
 
 
 
 
 
 
 
70a1657
6f78cec
 
 
 
 
 
 
 
 
 
 
 
 
3700629
 
f15ce6e
 
3700629
6f78cec
352eac8
 
f15ce6e
6f78cec
 
f15ce6e
 
352eac8
6f78cec
 
 
 
 
 
 
 
 
 
 
 
 
463b556
352eac8
1935825
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
from telegram.ext.filters import Filters
from telegram.ext.messagehandler import MessageHandler
from telegram import Update, ParseMode
from telegram.ext import (Updater,
						  PicklePersistence,
						  CommandHandler,
						  CallbackQueryHandler,
						  CallbackContext,
						  ConversationHandler)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ForceReply, ReplyKeyboardMarkup
import requests
import hashlib
import sqlite3
import re
from bs4 import BeautifulSoup

bot_token = '5415345086:AAFsaAWlJcgMd0Wii1Z8UgzpgSrfSZM1ZWw'
admin = 37087739

#############################################

EXPECT_STATUS, EXPECT_USERNAME, EXPECT_PASSWORD, EXPECT_SUBMIT, EXPECT_BACK, EXPECT_BUTTON_CLICK  = range(6)

def start(update: Update, context: CallbackContext):
	chat_id = update.effective_chat.id
	text = ('به ربات پرتال دانشجویی خوش آمدید 🌹')
	update.message.reply_text(text, reply_markup=main_keyboard)


def set_info_handler(update: Update, context: CallbackContext):
	username = context.user_data.get('username', 'تنظیم نشده است')
	password = context.user_data.get('password', 'تنظیم نشده است')
	text = (f'نام کاربری: {username}\n'
			f'رمز عبور: {password}\n\n'
			'لطفا یک گزینه را انتخاب نمایید')
	button = [  [InlineKeyboardButton('رمز عبور', callback_data='pass'),
				 InlineKeyboardButton('نام کاربری', callback_data='user')],
				[InlineKeyboardButton('ورود و دریافت نمرات', callback_data='login_portal')],
				[InlineKeyboardButton('بازگشت', callback_data='cancel')],
			]
	inline_keyboard = InlineKeyboardMarkup(button)
	update.message.reply_text(text, reply_markup=inline_keyboard)
	return EXPECT_BUTTON_CLICK

def button_click_handler(update: Update, context: CallbackContext):
	chat_id = update.effective_chat.id
	query = update.callback_query
	query.answer()
	query.delete_message()
	if query.data == 'login_portal':
		web_url = 'https://pooya.um.ac.ir/gateway/PuyaAuthenticate.php'
		resp = session.get(web_url, headers=headers)
		soup = BeautifulSoup(resp.content, 'html.parser')
		img_src = soup.find('img', id='secimg')['src']
		filename = 'C:/Users/Home/OneDrive/Desktop/captcha.png'
		capcha_src = 'https://pooya.um.ac.ir' + img_src[2:]
		context.user_data['captcha'] = capcha_src
		context.bot.send_photo(chat_id, photo=capcha_src)
		context.bot.send_message(chat_id, text='کد کپچا را وارد کنید', reply_markup=ForceReply())
		return EXPECT_SUBMIT
	elif query.data == 'user':
		context.bot.send_message(chat_id, text='نام کاربری خود را وارد کنید', reply_markup=ForceReply())
		return EXPECT_USERNAME
	elif query.data == 'pass':
		context.bot.send_message(chat_id, text='رمز عبور خود را وارد کنید', reply_markup=ForceReply())
		return EXPECT_PASSWORD
	elif query.data == 'cancel':
		query.answer('Opeartion canceled!')
		return ConversationHandler.END


def hash_password(text):
	password = hashlib.md5()
	password.update(text.encode('utf-8'))
	return password.hexdigest()


def login_portal(update: Update, context: CallbackContext):
	chat_id = update.effective_chat.id
	captcha = update.message.text
	USER = context.user_data['username']
	PASS = context.user_data['password']
	capcha_src = context.user_data['captcha']
	login_data = {  'UserPassword': hash_password(PASS),
					'pswdStatus': '',
					'UserID': USER,
					'DummyVar': '',
					'rand2': capcha_src[capcha_src.find('rand2=')+6:],
					'mysecpngco': captcha
				}
	req_url = 'https://pooya.um.ac.ir/gateway/UserInterim.php'
	resp = session.post(req_url, data=login_data) 
	if resp.status_code == 200:
		grade_list = session.get('https://pooya.um.ac.ir/educ/educfac/stuShowEducationalLogFromGradeList.php', headers=headers)
		soup = BeautifulSoup(grade_list.content, 'html.parser')
		data = [item.text.splitlines() for item in soup.find_all('tr', bgcolor="#E8E8FF")]
		mean = [item.text.splitlines() for item in soup.find_all('tr', bgcolor="#C4C4FF")][1][3]
		mark = '\n'.join([f'{i+1}) {row[2]}, {row[5]} ({row[8]})' for i, row in enumerate(data)])
		text = f'{mark}\nمعدل: {mean}'
		context.bot.send_message(chat_id, text)
	else:
		print('Login faild!')
	return ConversationHandler.END 


def set_password(update: Update, context: CallbackContext):
	context.user_data['password'] = update.message.text
	update.message.reply_text('رمز عبور با موفقیت ثبت شد', reply_markup=main_keyboard)
	return ConversationHandler.END


def set_username(update: Update, context: CallbackContext):
	context.user_data['username'] = update.message.text
	update.message.reply_text('نام کاربری با موفقیت ثبت شد', reply_markup=main_keyboard)
	return ConversationHandler.END


def cancel(update: Update, context: CallbackContext):
	update.message.reply_text('منوی اصلی', reply_markup=main_keyboard)
	return ConversationHandler.END

#############################################

if __name__ == '__main__':
	# Session
	session = requests.session()
	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'}
	# Bot details
	updater = Updater(token=bot_token)
	updater.dispatcher.add_handler(CommandHandler('start', start))
	updater.dispatcher.add_handler(ConversationHandler(
		entry_points=[MessageHandler(Filters.regex('ورود به پرتال دانشجویی'), set_info_handler)],
		states={
			EXPECT_USERNAME: [MessageHandler(Filters.text, set_username)],
			EXPECT_PASSWORD: [MessageHandler(Filters.text, set_password)],
			EXPECT_SUBMIT: [MessageHandler(Filters.text, login_portal)],
			EXPECT_BUTTON_CLICK: [CallbackQueryHandler(button_click_handler)],
			EXPECT_BACK : [CallbackQueryHandler('cancel', start)]
		},
		fallbacks=[MessageHandler(Filters.regex('cancel'), cancel)],
	))
	buttons = [['ورود به پرتال دانشجویی']]
	main_buttons = [row[::-1] for row in buttons]
	main_keyboard = ReplyKeyboardMarkup(main_buttons, one_time_keyboard=True, resize_keyboard=True) 
	updater.start_polling()
	print('Bot started ..........')