Advertisement
Guest User

Untitled

a guest
Nov 11th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.22 KB | None | 0 0
  1. import psycopg2
  2. import constants
  3. import telebot
  4. import json
  5. import requests
  6. from time import sleep
  7.  
  8. try:
  9.     conn = psycopg2.connect(dbname=constants.dbname,
  10.                             user=constants.user,
  11.                             password=constants.password)
  12.     print("Database is connected")
  13. except:
  14.     print("Something wrong :(")
  15.  
  16. cur = conn.cursor()
  17.  
  18.  
  19. def check():
  20.     request = requests.get(constants.url + constants.bot_token + '/getUpdates')
  21.     cur.execute("SELECT update_id FROM {} ORDER BY update_id DESC;".format(constants.table_name))
  22.     results = cur.fetchall()
  23.     # print(results[0][0])
  24.     last_update_id = (results[0][0])
  25.     new_update_id = json.loads(request.text)
  26.     try:
  27.         new_update_id = new_update_id['result'][-1]['update_id']
  28.     except:
  29.         check()
  30.     if new_update_id != last_update_id:
  31.         work_with_request(request.text)
  32.         print(new_update_id)
  33.         print(last_update_id)
  34.     else:
  35.         sleep(0.1)
  36.         check()
  37.  
  38.  
  39.  
  40. def work_with_request(request):
  41.     parsed_json = json.loads(request)
  42.     update_id = parsed_json['result'][-1]['update_id']
  43.     chat_id = parsed_json['result'][-1]['message']['chat']['id']
  44.     user_id = parsed_json['result'][-1]['message']['from']['id']
  45.     #user_id = 165682730
  46.     username = parsed_json['result'][-1]['message']['from']['username']
  47.     date = parsed_json['result'][-1]['message']['date']
  48.     try:
  49.         photo_id = parsed_json['result'][-1]['message']['photo'][-1]['file_id']
  50.         photo_upload(update_id, chat_id, user_id, photo_id)
  51.     except:
  52.         text = parsed_json['result'][-1]['message']['text']
  53.         if text == '/cancel':
  54.             remove_from_database(user_id)
  55.         elif text == '/start':
  56.             user_check(update_id, chat_id, user_id, username, date, text)
  57.         else:
  58.             work_with_old_user(update_id, chat_id, user_id, text)
  59.  
  60.  
  61. def user_check(update_id, chat_id, user_id, username, date, text):
  62.     try:
  63.         cur.execute("SELECT step from {} WHERE user_id='{}'".format(constants.table_name, user_id))
  64.         step = cur.fetchall()
  65.         print(step)
  66.         step = step[0][0]
  67.         print(step)
  68.         print("Ебаный шаг = " + str(step) + '!')
  69.         if step == 1 or step > 1:
  70.             work_with_old_user(update_id, chat_id, user_id, text)
  71.         else:
  72.             start_message(update_id, chat_id, user_id, username, date)
  73.     except:
  74.         start_message(update_id, chat_id, user_id, username, date)
  75.  
  76.  
  77. def work_with_old_user(update_id, chat_id, user_id, text):
  78.     cur.execute("SELECT step from {} WHERE user_id='{}'".format(constants.table_name, user_id))
  79.     step = cur.fetchall()
  80.     step = step[0][0]
  81.     print(step)
  82.     if step == 1:
  83.         requests.get(
  84.             constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
  85.                 str(chat_id) + '&text=' + constants.steps[1]))
  86.         cur.execute("UPDATE {} SET name='{}', step='2', update_id='{}' where user_id='{}'".format(constants.table_name,
  87.                                                                                                   text, update_id,
  88.                                                                                                   user_id))
  89.         commit()
  90.         check()
  91.     elif step == 2:
  92.         requests.get(
  93.             constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
  94.                 str(chat_id) + '&text=' + constants.steps[2]))
  95.         cur.execute("UPDATE {} SET sex='{}', step='3', update_id='{}' where user_id='{}'".format(constants.table_name,
  96.                                                                                                  text, update_id,
  97.                                                                                                  user_id))
  98.         commit()
  99.         check()
  100.     elif step == 3:
  101.         if int(text) < 10 or int(text) > 100:
  102.             requests.get(
  103.                 constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
  104.                     str(chat_id) + '&text=' + 'Кажется, ты нас обманываешь, пидорашка'))
  105.             cur.execute(
  106.                 "UPDATE {} SET update_id='{}' where user_id='{}'".format(constants.table_name, update_id, user_id))
  107.         else:
  108.             requests.get(
  109.             constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
  110.                 str(chat_id) + '&text=' + constants.steps[3]))
  111.             cur.execute(
  112.             "UPDATE {} SET age='{}', step='4', update_id='{}' where user_id='{}'".format(constants.table_name, text,
  113.                                                                                          update_id, user_id))
  114.             commit()
  115.         check()
  116.     elif step == 4:
  117.         requests.get(
  118.             constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
  119.                 str(chat_id) + '&text=' + constants.steps[4]))
  120.         cur.execute("UPDATE {} SET city='{}', step='5', update_id='{}' where user_id='{}'".format(constants.table_name,
  121.                                                                                                   text, update_id,
  122.                                                                                                   user_id))
  123.         commit()
  124.         check()
  125.     elif step == 5:
  126.         requests.get(
  127.             constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
  128.                 str(chat_id) + '&text=' + constants.steps[5]))
  129.         cur.execute(
  130.             "UPDATE {} SET hobbies='{}', step='6', update_id='{}' where user_id='{}'".format(constants.table_name,
  131.                                                                                              text, update_id,
  132.                                                                                              user_id))
  133.         commit()
  134.         check()
  135.     elif step == 6:
  136.         requests.get(
  137.             constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
  138.                 str(chat_id) + '&text=' + constants.steps[6]))
  139.         cur.execute(
  140.             "UPDATE {} SET with_whom='{}', step='7', update_id='{}' where user_id='{}'".format(constants.table_name,
  141.                                                                                                text, update_id,
  142.                                                                                                user_id))
  143.         commit()
  144.         check()
  145.     elif step == 7:
  146.         print("zalupa")
  147.     else:
  148.         accept(user_id, update_id)
  149.  
  150.  
  151. def photo_upload(update_id, chat_id, user_id, photo_id):
  152.     requests.get(
  153.         constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
  154.             str(chat_id) + '&text=' + constants.end_filling_form))
  155.     cur.execute("UPDATE {} SET file_with_photo_id='{}', step='8', update_id='{}' where user_id='{}'".format(
  156.         constants.table_name,
  157.         photo_id, update_id,
  158.         user_id))
  159.     conn.commit()
  160.     accept(user_id, update_id)
  161.  
  162.  
  163. def accept(user_id, update_id):
  164.     cur.execute("UPDATE {} SET update_id='{}' where user_id='{}'".format(
  165.         constants.table_name, update_id, user_id))
  166.     conn.commit()
  167.     cur.execute("SELECT * from {} where user_id = '{}'".format(constants.table_name, user_id))
  168.     result = cur.fetchall()
  169.     requests.get(constants.url + constants.bot_token + '/sendPhoto?chat_id=' + str(
  170.         user_id) + '&photo=' + result[0][12] + '&caption=' +
  171.                  '1.Имя.\n' + result[0][10] + '\n'
  172.                  '2.Пол.\n' + result[0][11] + '\n'
  173.                  '3.Возраст.\n' + str(result[0][9]) + '\n'
  174.                  '4.Город.\n' + result[0][6] + '\n'
  175.                  '5.Увлечения.\n' + result[0][7] + '\n'
  176.                  '6.С кем хочу познакомиться\n' + result[0][8] + '\n')
  177.     requests.get(
  178.         constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
  179.             str(user_id) + '&text=' + constants.end_text))
  180.     commit()
  181.  
  182.  
  183. def start_message(update_id, chat_id, user_id, username, date):
  184.     welcome = requests.get(
  185.         constants.url + constants.bot_token + '/sendMessage?chat_id=' + str(
  186.             chat_id) + '&text=' + constants.welcome_text)
  187.     example = requests.get(
  188.         constants.url + constants.bot_token + '/sendPhoto?chat_id=' + str(
  189.             chat_id) + '&photo=' + constants.welcome_photo_id + '&caption=Пример заполнение анкеты')
  190.     name = requests.get(
  191.         constants.url + constants.bot_token + '/sendMessage?chat_id=' + str(
  192.             chat_id) + '&text=' + constants.steps[0])
  193.     if welcome.status_code == 200 and example.status_code == 200 and name.status_code == 200:
  194.         print('good request')
  195.         try:
  196.             cur.execute(
  197.                 "INSERT into {} (update_id, chat_id, user_id, username, date, step) VALUES ('{}', '{}', '{}', '{}', "
  198.                 "'{}', 1)".format(constants.table_name, update_id, chat_id, user_id, username, date))
  199.         except:
  200.             print('Error')
  201.     else:
  202.         print('Bad request codes')
  203.     commit()
  204.  
  205.  
  206. def remove_from_database(user_id):
  207.     cur.execute("DELETE from {} where user_id={}".format(constants.table_name, user_id))
  208.     commit()
  209.  
  210.  
  211. def commit():
  212.     conn.commit()
  213.     check()
  214.  
  215.  
  216. check()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement