def check_phone(update, context): message = update.effective_message if message.contact: if message.contact.phone_number[: 3] == '998' or message.contact.phone_number[ 1:4] == '998': phone = message.contact.phone_number cursor.execute( "UPDATE orders SET phone_number = '{}' WHERE order_id = '{}'". format(phone, context.chat_data['order_id'])) connect.commit() cursor.execute( "UPDATE users SET phone_number = '{}' WHERE telegram_id = '{}'" .format(phone, get_chat(update))) connect.commit() request_address(update, context) return REQUESTING_ADDRESS else: update.effective_message.reply_text( texts['country_error'][language(update)]) else: phone = message.text[1:] if phone[:3] == '998' and len(phone) == 12 and int( phone): # chat not found context.bot.send_message( chat_id=get_chat(update), text=texts['good_number'][language(update)]) request_address(update, context) return REQUESTING_ADDRESS else: update.effective_message.reply_text( texts['format_error'][language(update)]) # language
def cancel_order(update, context): cursor.execute( "UPDATE orders SET comments ='CANCELED' WHERE order_id = '{}'".format( context.chat_data['order_id'])) connect.commit() update.effective_message.reply_text(texts['canceled'][language(update)]) back_to_main(update, context) return MAIN_PAGE
def get_comments(update, context): txt = update.effective_message.text cursor.execute( "UPDATE orders SET comments = '{}' WHERE order_id = '{}'".format( txt, context.chat_data['order_id'])) connect.commit() checkout(update, context) return CONFIRMING_ORDER
def greet_user(update, context): query = update.callback_query query.answer() cursor.execute( "UPDATE users SET language = '{}', status = '{}' WHERE telegram_id = '{}'" .format(query.data, LanguageGot, get_chat(update))) connect.commit() query.delete_message() context.bot.send_message(chat_id=get_chat(update), text=texts['greeting'][language(update)], parse_mode='HTML') return NAME
def name_accept(update, context: CallbackContext): message = update.effective_message a = message.text.split() if len(a) == 1 and a[0][0].isupper(): cursor.execute( "UPDATE users SET name = '{}', status = '{}' WHERE telegram_id = '{}'" .format(message.text, ActiveUser, get_chat(update))) connect.commit() context.bot.send_message( chat_id=get_chat(update), text=texts['name_accepted'][language(update)].format(message.text)) main_menu(update, context) return MAIN_PAGE else: update.effective_message.reply_text( texts['name_error'][language(update)], parse_mode='HTML')
def address(update, context): chat_id = update.effective_chat.id lang = language(update) location = update.message.location current_position = (location.longitude, location.latitude) coords = f"{current_position[0]},{current_position[1]}" if lang == "en": address_str = get_address_from_coords(coords, update, is_english=True) else: address_str = get_address_from_coords(coords, update) cursor.execute( "UPDATE orders SET location = '{}' WHERE order_id = '{}'".format( address_str, context.chat_data['order_id'])) connect.commit() request_comments(update, context) return REQUESTING_COMMENTS
def wrapped(update, context, *args, **kwargs): user_id = update.effective_user.id a = cursor.execute( "SELECT telegram_id FROM admins".format(user_id)).fetchall() IS_ADMIN = [] for i in a: IS_ADMIN.append(i[0]) if user_id not in IS_ADMIN: print("Unauthorized access denied for {}.".format(user_id)) return return func(update, context, *args, **kwargs)
def start(update, context: CallbackContext): chat = get_chat(update) row = cursor.execute( "SELECT id FROM users WHERE telegram_id = '{}'".format( chat)).fetchall() user = update.message if update.message.chat.id < 0: pass # In Group else: if len(row) == 0: cursor.execute("""INSERT INTO users( id, telegram_id, name, username, phone_number, language, status) VALUES ( NOT NULL, '{}', '{}', '{}', NULL, NULL, '{}')""".format( chat, user.from_user.full_name, user.from_user.username, NewUser)) connect.commit() choose_language(update, context) return LANGUAGE else: status = cursor.execute( "SELECT status FROM users where telegram_id = '{}'".format( chat)).fetchone()[0] if status != ActiveUser: cursor.execute( "DELETE FROM users WHERE telegram_id = '{}'".format(chat)) connect.commit() start(update, context) return LANGUAGE else: main_menu(update, context) return MAIN_PAGE
def checkout(update, context): order_id = context.chat_data['order_id'] q = cursor.execute( "SELECT quantity FROM orders WHERE order_id = '{}'".format( order_id)).fetchone()[0] comment = cursor.execute( "SELECT comments FROM orders WHERE order_id = '{}'".format( order_id)).fetchone()[0] deliver_to = cursor.execute( "SELECT location FROM orders WHERE order_id = '{}'".format( order_id)).fetchone()[0] user = cursor.execute( "SELECT name, phone_number FROM users WHERE telegram_id = '{}'".format( get_chat(update))).fetchmany()[0] if q >= 5: total = q * UNIT_PRICE cursor.execute( "UPDATE orders SET total = '{}' WHERE order_id = '{}'".format( total, order_id)) connect.commit() else: total = q * UNIT_PRICE + DELIVERY_PRICE cursor.execute( "UPDATE orders SET total = '{}' WHERE order_id = '{}'".format( total, order_id)) connect.commit() markup = [[KeyboardButton(buttons['confirm'][language(update)])], [KeyboardButton(buttons['cancel'][language(update)])]] txt = texts['checkout'][language(update)] context.bot.send_message( chat_id=get_chat(update), text=txt.format( user[0], user[1] if user[1][0] == '+' else '+' + user[1], deliver_to, texts['no_comments'][language(update)] if comment is None else comment, q, format_price(UNIT_PRICE), format_price(q * UNIT_PRICE) + ' ' + texts['currency'][language(update)], format_price(total - q * UNIT_PRICE), texts['currency'][language(update)], format_price(total), texts['currency'][language(update)]), reply_markup=ReplyKeyboardMarkup(markup, resize_keyboard=True), parse_mode='HTML') return CONFIRMING_ORDER
def reply_to_message(update, context): try: if update.message.reply_to_message: response = update.message.text reply_id = update.message.reply_to_message.message_id user_id = context.bot_data[reply_id] lang = cursor.execute( "SELECT language FROM Users WHERE telegram_id = '{}'".format( user_id)).fetchone()[0] reply = texts['replying'][lang].format(response) context.bot.send_message(chat_id=user_id, text=reply, parse_mode='HTML') else: pass except KeyError: context.bot.send_message( chat_id=update.effective_chat.id, text='На это ответить я не могу 😢\n' 'Выберите реальный запрос который я вам отправил')
def get_quantity(update, context: CallbackContext): q = update.message.text current_time = datetime.datetime.utcnow() + datetime.timedelta(hours=5) time = current_time.strftime("%Y-%m-%d %H:%M:%S") try: if int(q) < 20: if int(q) >= 5: cursor.execute( """INSERT INTO orders(timestamp, customer_id, quantity, unit_price, delivery_cost) VALUES ('{}', '{}', '{}', '{}', '{}')""" .format(time, get_chat(update), q, UNIT_PRICE, 0)) context.chat_data.update({ 'order_id': cursor.execute( "SELECT last_insert_rowid() FROM orders").fetchone()[0] }) connect.commit() else: cursor.execute( """INSERT INTO orders(timestamp, customer_id, quantity, unit_price, delivery_cost) VALUES ('{}', '{}', '{}', '{}', '{}')""" .format(time, get_chat(update), q, UNIT_PRICE, DELIVERY_PRICE)) context.chat_data.update({ 'order_id': cursor.execute( "SELECT last_insert_rowid() FROM orders").fetchone()[0] }) connect.commit() request_phone(update, context) return REQUESTING_PHONE else: context.bot.send_message(chat_id=get_chat(update), text=texts['too_much'][language(update)]) except ValueError: context.bot.send_message(chat_id=get_chat(update), text=texts['not_quantity'][language(update)])
def confirm_order(update, context): new_order = texts['new_order_for_admin'] order_id = context.chat_data['order_id'] timestamp = cursor.execute( "SELECT timestamp FROM orders WHERE order_id = '{}'".format( order_id)).fetchone()[0] q = cursor.execute( "SELECT quantity FROM orders WHERE order_id = '{}'".format( order_id)).fetchone()[0] comment = cursor.execute( "SELECT comments FROM orders WHERE order_id = '{}'".format( order_id)).fetchone()[0] deliver_to = cursor.execute( "SELECT location FROM orders WHERE order_id = '{}'".format( order_id)).fetchone()[0] user = cursor.execute( "SELECT name, phone_number, username, language FROM users WHERE telegram_id = '{}'" .format(get_chat(update))).fetchmany()[0] delivery_cost = cursor.execute( "SELECT delivery_cost FROM orders WHERE order_id = '{}'".format( order_id)).fetchone()[0] context.bot.send_message( chat_id=ORDERS_CHANNEL_ID, text=new_order.format( timestamp, user[0], user[1] if user[1][0] == '+' else '+' + user[1], 'тут пусто 🙃' if user[2] is None else '@' + user[2], user[3], q, format_price(UNIT_PRICE), format_price(q * UNIT_PRICE), format_price(int(delivery_cost)), deliver_to, 'без коммента' if comment is None else comment, format_price(q * UNIT_PRICE + int(delivery_cost))), parse_mode='HTML') context.bot.send_message(chat_id=get_chat(update), text=texts['order_accepted'][language(update)]) back_to_main(update, context) return MAIN_PAGE