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 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 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 ask_me(update, context): btn = [[buttons['back'][language(update)]]] context.bot.send_message(chat_id=get_chat(update), text=texts['ask_me'][language(update)], reply_markup=ReplyKeyboardMarkup( btn, resize_keyboard=True)) return ASKING
def choose_language(update, context): message = update.message btn = buttons['language'] markup = [[ InlineKeyboardButton(btn['uz'], callback_data='uz'), InlineKeyboardButton(btn['ru'], callback_data='ru'), InlineKeyboardButton(btn['en'], callback_data='en') ]] try: context.bot.delete_message( chat_id=get_chat(update), message_id=context.chat_data['language_message_id']) msg = context.bot.send_message( chat_id=message.chat_id, text=texts['choose_language'], reply_markup=InlineKeyboardMarkup(markup), parse_mode=ParseMode.MARKDOWN_V2) context.chat_data.update({'language_message_id': msg.message_id}) except KeyError: msg = context.bot.send_message( chat_id=message.chat_id, text=texts['choose_language'], reply_markup=InlineKeyboardMarkup(markup), parse_mode=ParseMode.MARKDOWN_V2) context.chat_data.update({'language_message_id': msg.message_id})
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 request_comments(update, context): button = [[KeyboardButton(buttons['skip'][language(update)])], [KeyboardButton(buttons['back'][language(update)])]] context.bot.send_message(chat_id=get_chat(update), text=texts['any_comments?'][language(update)], reply_markup=ReplyKeyboardMarkup( button, resize_keyboard=True))
def preview(update, context: CallbackContext): context.bot.send_photo(chat_id=get_chat(update), photo='AgACAgIAAxkBAAMxYKN2sDqj6YNOus2_' 'uQQT1EBIAAGjAAJksTEbAAHAGUkpn3z1K' 'cYAAZIibAABny4AAwEAAwIAA3gAAwNBBAABHwQ', caption=texts['description'][language(update)], parse_mode='HTML') quantity(update, context) return SELECTING_QUANTITY
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 request_address(update, context: CallbackContext): button = [[ KeyboardButton(text=buttons['send_location'][language(update)], request_location=True) ], [KeyboardButton(buttons['back'][language(update)])]] context.bot.send_message(chat_id=get_chat(update), text=texts['send_address'][language(update)], reply_markup=ReplyKeyboardMarkup( button, resize_keyboard=True)) return REQUESTING_ADDRESS
def main_menu(update, context: CallbackContext): markup = [ [KeyboardButton(buttons['order'][language(update)])], [KeyboardButton(buttons['watch_tutorial'][language(update)]), KeyboardButton(buttons['ask_question'][language(update)])], [KeyboardButton(buttons['change_language'][language(update)])] ] context.bot.send_message(chat_id=get_chat(update), text=texts['main_menu'][language(update)], reply_markup=ReplyKeyboardMarkup(markup, resize_keyboard=True))
def quantity(update, context: CallbackContext): button_list = [KeyboardButton(str(s)) for s in range(1, 10)] context.bot.send_message( chat_id=get_chat(update), text=texts['choose_quantity'][language(update)], reply_markup=ReplyKeyboardMarkup(build_menu( button_list, n_cols=3, footer_buttons=KeyboardButton(buttons['back'][language(update)])), resize_keyboard=True), parse_mode='HTML')
def request_phone(update, context): button = [[ KeyboardButton(buttons['send_phone_button'][language(update)], request_contact=True) ], [KeyboardButton(buttons['back'][language(update)])]] context.bot.send_message(chat_id=get_chat(update), text=texts['send_number'][language(update)], reply_markup=ReplyKeyboardMarkup( button, resize_keyboard=True), parse_mode='HTML') return REQUESTING_PHONE
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
def export(update, context: CallbackContext): db_df = pd.read_sql_query("SELECT * FROM users", connect) db_df.to_csv(f'exports/export_{datetime.date.today()}.csv') context.bot.send_document( chat_id=get_chat(update), document=open(f'exports/export_{datetime.date.today()}.csv'))