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
Beispiel #2
0
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 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 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 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 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)])