Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- import constants
- import telebot
- import json
- import requests
- from time import sleep
- try:
- conn = psycopg2.connect(dbname=constants.dbname,
- user=constants.user,
- password=constants.password)
- print("Database is connected")
- except:
- print("Something wrong :(")
- cur = conn.cursor()
- def check():
- request = requests.get(constants.url + constants.bot_token + '/getUpdates')
- cur.execute("SELECT update_id FROM {} ORDER BY update_id DESC;".format(constants.table_name))
- results = cur.fetchall()
- # print(results[0][0])
- last_update_id = (results[0][0])
- new_update_id = json.loads(request.text)
- try:
- new_update_id = new_update_id['result'][-1]['update_id']
- except:
- check()
- if new_update_id != last_update_id:
- work_with_request(request.text)
- print(new_update_id)
- print(last_update_id)
- else:
- sleep(0.1)
- check()
- def work_with_request(request):
- parsed_json = json.loads(request)
- update_id = parsed_json['result'][-1]['update_id']
- chat_id = parsed_json['result'][-1]['message']['chat']['id']
- user_id = parsed_json['result'][-1]['message']['from']['id']
- #user_id = 165682730
- username = parsed_json['result'][-1]['message']['from']['username']
- date = parsed_json['result'][-1]['message']['date']
- try:
- photo_id = parsed_json['result'][-1]['message']['photo'][-1]['file_id']
- photo_upload(update_id, chat_id, user_id, photo_id)
- except:
- text = parsed_json['result'][-1]['message']['text']
- if text == '/cancel':
- remove_from_database(user_id)
- elif text == '/start':
- user_check(update_id, chat_id, user_id, username, date, text)
- else:
- work_with_old_user(update_id, chat_id, user_id, text)
- def user_check(update_id, chat_id, user_id, username, date, text):
- try:
- cur.execute("SELECT step from {} WHERE user_id='{}'".format(constants.table_name, user_id))
- step = cur.fetchall()
- print(step)
- step = step[0][0]
- print(step)
- print("Ебаный шаг = " + str(step) + '!')
- if step == 1 or step > 1:
- work_with_old_user(update_id, chat_id, user_id, text)
- else:
- start_message(update_id, chat_id, user_id, username, date)
- except:
- start_message(update_id, chat_id, user_id, username, date)
- def work_with_old_user(update_id, chat_id, user_id, text):
- cur.execute("SELECT step from {} WHERE user_id='{}'".format(constants.table_name, user_id))
- step = cur.fetchall()
- step = step[0][0]
- print(step)
- if step == 1:
- requests.get(
- constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
- str(chat_id) + '&text=' + constants.steps[1]))
- cur.execute("UPDATE {} SET name='{}', step='2', update_id='{}' where user_id='{}'".format(constants.table_name,
- text, update_id,
- user_id))
- commit()
- check()
- elif step == 2:
- requests.get(
- constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
- str(chat_id) + '&text=' + constants.steps[2]))
- cur.execute("UPDATE {} SET sex='{}', step='3', update_id='{}' where user_id='{}'".format(constants.table_name,
- text, update_id,
- user_id))
- commit()
- check()
- elif step == 3:
- if int(text) < 10 or int(text) > 100:
- requests.get(
- constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
- str(chat_id) + '&text=' + 'Кажется, ты нас обманываешь, пидорашка'))
- cur.execute(
- "UPDATE {} SET update_id='{}' where user_id='{}'".format(constants.table_name, update_id, user_id))
- else:
- requests.get(
- constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
- str(chat_id) + '&text=' + constants.steps[3]))
- cur.execute(
- "UPDATE {} SET age='{}', step='4', update_id='{}' where user_id='{}'".format(constants.table_name, text,
- update_id, user_id))
- commit()
- check()
- elif step == 4:
- requests.get(
- constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
- str(chat_id) + '&text=' + constants.steps[4]))
- cur.execute("UPDATE {} SET city='{}', step='5', update_id='{}' where user_id='{}'".format(constants.table_name,
- text, update_id,
- user_id))
- commit()
- check()
- elif step == 5:
- requests.get(
- constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
- str(chat_id) + '&text=' + constants.steps[5]))
- cur.execute(
- "UPDATE {} SET hobbies='{}', step='6', update_id='{}' where user_id='{}'".format(constants.table_name,
- text, update_id,
- user_id))
- commit()
- check()
- elif step == 6:
- requests.get(
- constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
- str(chat_id) + '&text=' + constants.steps[6]))
- cur.execute(
- "UPDATE {} SET with_whom='{}', step='7', update_id='{}' where user_id='{}'".format(constants.table_name,
- text, update_id,
- user_id))
- commit()
- check()
- elif step == 7:
- print("zalupa")
- else:
- accept(user_id, update_id)
- def photo_upload(update_id, chat_id, user_id, photo_id):
- requests.get(
- constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
- str(chat_id) + '&text=' + constants.end_filling_form))
- cur.execute("UPDATE {} SET file_with_photo_id='{}', step='8', update_id='{}' where user_id='{}'".format(
- constants.table_name,
- photo_id, update_id,
- user_id))
- conn.commit()
- accept(user_id, update_id)
- def accept(user_id, update_id):
- cur.execute("UPDATE {} SET update_id='{}' where user_id='{}'".format(
- constants.table_name, update_id, user_id))
- conn.commit()
- cur.execute("SELECT * from {} where user_id = '{}'".format(constants.table_name, user_id))
- result = cur.fetchall()
- requests.get(constants.url + constants.bot_token + '/sendPhoto?chat_id=' + str(
- user_id) + '&photo=' + result[0][12] + '&caption=' +
- '1.Имя.\n' + result[0][10] + '\n'
- '2.Пол.\n' + result[0][11] + '\n'
- '3.Возраст.\n' + str(result[0][9]) + '\n'
- '4.Город.\n' + result[0][6] + '\n'
- '5.Увлечения.\n' + result[0][7] + '\n'
- '6.С кем хочу познакомиться\n' + result[0][8] + '\n')
- requests.get(
- constants.url + constants.bot_token + '/sendMessage?chat_id=' + (
- str(user_id) + '&text=' + constants.end_text))
- commit()
- def start_message(update_id, chat_id, user_id, username, date):
- welcome = requests.get(
- constants.url + constants.bot_token + '/sendMessage?chat_id=' + str(
- chat_id) + '&text=' + constants.welcome_text)
- example = requests.get(
- constants.url + constants.bot_token + '/sendPhoto?chat_id=' + str(
- chat_id) + '&photo=' + constants.welcome_photo_id + '&caption=Пример заполнение анкеты')
- name = requests.get(
- constants.url + constants.bot_token + '/sendMessage?chat_id=' + str(
- chat_id) + '&text=' + constants.steps[0])
- if welcome.status_code == 200 and example.status_code == 200 and name.status_code == 200:
- print('good request')
- try:
- cur.execute(
- "INSERT into {} (update_id, chat_id, user_id, username, date, step) VALUES ('{}', '{}', '{}', '{}', "
- "'{}', 1)".format(constants.table_name, update_id, chat_id, user_id, username, date))
- except:
- print('Error')
- else:
- print('Bad request codes')
- commit()
- def remove_from_database(user_id):
- cur.execute("DELETE from {} where user_id={}".format(constants.table_name, user_id))
- commit()
- def commit():
- conn.commit()
- check()
- check()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement