aheskandani commited on
Commit
f15ce6e
1 Parent(s): 463b556

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -18
app.py CHANGED
@@ -10,6 +10,7 @@ from telegram.ext import (Updater,
10
  from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ForceReply, ReplyKeyboardMarkup
11
  import requests
12
  import hashlib
 
13
  import re
14
  from bs4 import BeautifulSoup
15
 
@@ -22,6 +23,12 @@ EXPECT_STATUS, EXPECT_USERNAME, EXPECT_PASSWORD, EXPECT_SUBMIT, EXPECT_BACK, EXP
22
 
23
  def start(update: Update, context: CallbackContext):
24
  chat_id = update.effective_chat.id
 
 
 
 
 
 
25
  text = ('به ربات پرتال دانشجویی خوش آمدید 🌹')
26
  update.message.reply_text(text, reply_markup=main_keyboard)
27
 
@@ -69,9 +76,9 @@ def button_click_handler(update: Update, context: CallbackContext):
69
 
70
 
71
  def hash_password(text):
72
- password = hashlib.md5()
73
- password.update(text.encode('utf-8'))
74
- return password.hexdigest()
75
 
76
 
77
  def login_portal(update: Update, context: CallbackContext):
@@ -115,29 +122,61 @@ def set_username(update: Update, context: CallbackContext):
115
 
116
 
117
  def cancel(update: Update, context: CallbackContext):
118
- update.message.reply_text('منوی اصلی', reply_markup=main_keyboard)
119
- return ConversationHandler.END
120
-
121
 
122
  #############################################
123
 
124
- def captcha_url():
125
- web_url = 'https://pooya.um.ac.ir/gateway/PuyaAuthenticate.php'
126
- req_url = 'https://pooya.um.ac.ir/gateway/UserInterim.php'
127
- 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'}
128
- session = requests.session()
129
- resp = session.get(web_url, headers=headers)
130
- soup = BeautifulSoup(resp.content, 'html.parser')
131
- img_src = soup.find('img', id='secimg')['src']
132
- capcha_src = 'https://pooya.um.ac.ir' + img_src[2:]
133
- return capcha_src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  if __name__ == '__main__':
 
 
 
 
 
136
  session = requests.session()
137
  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'}
138
- pers = PicklePersistence(filename='mybot')
139
- updater = Updater(token=bot_token, persistence=pers)
140
  updater.dispatcher.add_handler(CommandHandler('start', start))
 
 
141
  updater.dispatcher.add_handler(ConversationHandler(
142
  entry_points=[MessageHandler(Filters.regex('ورود به پرتال دانشجویی'), set_info_handler)],
143
  states={
 
10
  from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ForceReply, ReplyKeyboardMarkup
11
  import requests
12
  import hashlib
13
+ import sqlite3
14
  import re
15
  from bs4 import BeautifulSoup
16
 
 
23
 
24
  def start(update: Update, context: CallbackContext):
25
  chat_id = update.effective_chat.id
26
+ user_data = check_existance(chatID=chat_id)
27
+ if not user_data:
28
+ create_user(chatID=chat_id, username='', password='')
29
+ user_data = {'id': 0, 'chatID': chat_id, 'username': None, 'password': None}
30
+ else:
31
+ user_data = dict(zip(['id', 'chatID', 'username', 'password']))
32
  text = ('به ربات پرتال دانشجویی خوش آمدید 🌹')
33
  update.message.reply_text(text, reply_markup=main_keyboard)
34
 
 
76
 
77
 
78
  def hash_password(text):
79
+ password = hashlib.md5()
80
+ password.update(text.encode('utf-8'))
81
+ return password.hexdigest()
82
 
83
 
84
  def login_portal(update: Update, context: CallbackContext):
 
122
 
123
 
124
  def cancel(update: Update, context: CallbackContext):
125
+ update.message.reply_text('منوی اصلی', reply_markup=main_keyboard)
126
+ return ConversationHandler.END
 
127
 
128
  #############################################
129
 
130
+ def create_database(update: Update, context: CallbackContext):
131
+ chat_id = update.effective_chat.id
132
+ if chat_id == admin:
133
+ try:
134
+ con.execute('CREATE TABLE USER (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, chatID TEXT, username TEXT, password TEXT)')
135
+ update.message.reply_text('دیتابیس با موفقیت ساخته شد', reply_markup=main_keyboard)
136
+ except:
137
+ update.message.reply_text('دیتابیس قبلا ایجاد شده است', reply_markup=main_keyboard)
138
+
139
+
140
+ def show_table(update: Update, context: CallbackContext):
141
+ chat_id = update.effective_chat.id
142
+ if chat_id == admin:
143
+ records = con.execute('SELECT * FROM USER')
144
+ table = [', '.join(map(str,user)) for user in records]
145
+ text = '\n'.join(table)
146
+ update.message.reply_text(text, reply_markup=main_keyboard)
147
+
148
+
149
+ def create_user(chatID, username, password):
150
+ con.execute(f'INSERT INTO USER (chatID, username, password) values({chatID}, {username}, {password})')
151
+
152
+
153
+ def check_existance(chatID, verbose=True):
154
+ records = con.execute('SELECT * FROM USER')
155
+ if verbose:
156
+ for user in records:
157
+ if user[1] == chatID:
158
+ return user
159
+ return None
160
+
161
+
162
+ def update_user(chatID, username, password):
163
+ con.execute(f'UPDATE USER (username, password) values({username}, {password}) WHERE chatID = {chatID}')
164
+
165
+ #############################################
166
 
167
  if __name__ == '__main__':
168
+ # Database
169
+ db_url = 'my2-test.db'
170
+ db_con = sqlite3.connect(db_url)
171
+ con = db_con.cursor()
172
+ # Session
173
  session = requests.session()
174
  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'}
175
+ # Bot details
176
+ updater = Updater(token=bot_token)
177
  updater.dispatcher.add_handler(CommandHandler('start', start))
178
+ updater.dispatcher.add_handler(CommandHandler('db', create_database))
179
+ updater.dispatcher.add_handler(CommandHandler('all', show_table))
180
  updater.dispatcher.add_handler(ConversationHandler(
181
  entry_points=[MessageHandler(Filters.regex('ورود به پرتال دانشجویی'), set_info_handler)],
182
  states={