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 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 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 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 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 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 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 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 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 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 forward_message(update, context): message_id = update.effective_message.message_id chat_id = update.effective_message.chat_id msg = context.bot.forward_message(chat_id=RESPONSES_GROUP_ID, from_chat_id=chat_id, message_id=message_id) payload = {msg.message_id: chat_id} context.bot_data.update(payload) update.effective_message.reply_text(texts['got_you'][language(update)]) back_to_main(update, context) return MAIN_PAGE
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 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 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 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 instructions(update, context): chat_id = update.effective_chat.id context.bot.send_video(chat_id, video="BAACAgIAAxkBAAIFBWDW_4KPRWFPBfdBvZvYN3tjaTnuAAK9DAACGVC4SlxU_LmBlQv8IAQ", caption=texts['video_caption'][language(update)], parse_mode="HTML")