Esempio n. 1
0
async def good_order(user_id):

    table = db_main.Table(tables.names_dict["order"])
    adress_id = table.select("adress_id", "user_id", user_id)[0][0]
    table.update("user_id", user_id, "sum", 0)
    table.close()

    table = db_main.Table(tables.names_dict["delivery_adress"])
    delivery_adress = table.select("delivery_adress", "id", adress_id)[0][0]
    table.close()

    await bot.send_message(
        user_id, "{delivery_adress}\nID пользователя: {user_id}\n\n".format(
            delivery_adress=delivery_adress, user_id=user_id) +
        generate_basket_text(user_id))
    await bot.send_message(user_id, "ЗАКАЗ ОПЛАЧЕН")
    await bot.send_message(
        int(texts["managers_chat"]),
        "{delivery_adress}\nID пользователя: {user_id}\n\n".format(
            delivery_adress=delivery_adress, user_id=user_id) +
        generate_basket_text(user_id))

    table = db_main.Table(tables.names_dict["basket"])
    table.delete("user_id", user_id)
    table.close()
Esempio n. 2
0
async def any_callback(callback: types.CallbackQuery):
    message = callback.message
    user_id = message.chat.id

    table = db_main.Table(tables.names_dict["user_orders"])
    orders = table.select(["order_code", "delivery_adress"],
                          ["user_id", "checked"], [user_id, 0])
    table.update("user_id", user_id, "checked", 1)
    table.close()

    for order, delivery_adress in orders:
        text = "Номер заказа: {order}\nАдрес доставки: {delivery_adress}\n\n".format(
            order=order, delivery_adress=delivery_adress)
        table = db_main.Table(tables.names_dict["orders"])
        order_items = table.select(
            ["item_code", "item_name", "item_price", "item_count"],
            "order_code", order)
        table.close()

        for item_code, item_name, item_price, item_count in order_items:
            table = db_main.Table(tables.names_dict["items"])
            description = table.select("description", "code", item_code)[0][0]
            table.close()
            text += "Товар #{item_code}\n{item_name}\nКоличество: {item_count}\nОписание: {description}".format(
                item_code=item_code,
                item_name=item_name,
                item_count=item_count,
                description=description)
        await bot.send_message(user_id, text)
        await bot.send_message(user_id, "ЗАКАЗ ОПЛАЧЕН")
        await bot.send_message(int(texts["managers_chat"]), text)
Esempio n. 3
0
async def basket_add(callback: types.CallbackQuery):
    category_id, position = int(callback.data.split()[2]), int(
        callback.data.split()[3])
    message = callback.message

    table = db_main.Table(tables.names_dict["items"])
    item = table.select(["code", "name", "price"], ["category_id", "position"],
                        [category_id, position])[0]
    item_code, item_name, item_price = item[0], item[1], item[2]
    table.close()

    table = db_main.Table(tables.names_dict["basket"])
    data = table.select("item_count", ["user_id", "item_code"],
                        [message.chat.id, item_code])
    if len(data) > 0:
        item_count = data[0][0]
        table.update(["user_id", "item_code"], [message.chat.id, item_code],
                     "item_count", item_count + 1)
        await callback.answer(
            "Количество данного товара в корзине: {item_count}".format(
                item_count=item_count + 1))
    else:
        table.insert(
            ["user_id", "item_code", "item_name", "item_count", "item_price"],
            [message.chat.id, item_code, item_name, 1, item_price])
        await callback.answer("Добавлено в корзину")
    table.close()
Esempio n. 4
0
def update_constants():
    # --- --- ---
    global categories
    global count_items
    table = db_main.Table("categories")
    data = table.select_all(["name", "count_items"])
    table.close()

    categories = [category[0] for category in data]
    count_items = [category[1] for category in data]

    # --- --- ---
    global texts
    table = db_main.Table("texts")
    data = table.select_all(["title", "text"])
    table.close()

    texts = dict(data)

    # --- --- ---
    global admins
    table = db_main.Table("admins")
    data = table.select_all("telegram_id")
    table.close()
    admins = [category[0] for category in data]
    print(admins)
Esempio n. 5
0
async def start_first_admin(message: types.Message):
    telegram_id = message.chat.id

    table = db_main.Table(tables.names_dict["admins"])
    table.insert("telegram_id", telegram_id)
    table.close()

    table = db_main.Table(tables.names_dict["texts"])
    table.insert(["title", "text"], ["managers_chat", telegram_id])
    table.close()

    update_constants()
    await start(message)
Esempio n. 6
0
def qiwi_update():
    global texts

    s = requests.Session()
    s.headers['authorization'] = 'Bearer ' + texts["qiwi_api_access_token"]
    parameters = {'rows': '10', 'operation': 'IN'}
    h = s.get(
        'https://edge.qiwi.com/payment-history/v1/persons/{qiwi_login}/payments'
        .format(qiwi_login=texts["number"]),
        params=parameters)
    # try:
    data = json.loads(h.text)
    lastTxnId = int(texts["lastTxnId"])

    if data["data"]:
        TxnId = data["data"][0]["txnId"]

        if TxnId > lastTxnId and lastTxnId != 0:
            print("QIWI new transaction")
            for transaction in data["data"]:
                if transaction["txnId"] > lastTxnId:
                    if transaction["status"] == "SUCCESS" and transaction[
                            "sum"]["currency"] == 398:  # !!!=====
                        if transaction["comment"].isdigit():
                            user_id = int(transaction["comment"])
                            sum = int(transaction["sum"]["amount"])
                            print(sum, user_id)
                            table = db_main.Table(
                                tables.names_dict["user_order"])
                            data = table.select("sum", "user_id", user_id)
                            table.close()
                            if len(data) > 0:
                                necessary_sum = data[0][0]
                                if sum >= necessary_sum:
                                    basket_to_orders(user_id)

                else:
                    break
        if TxnId > lastTxnId or lastTxnId == 0:
            table = db_main.Table(tables.names_dict["texts"])
            table.update("title", "lastTxnId", "text", TxnId)
            table.close()
            update_constants()

        # except:
        #     print ("Problems in qiwi auth")

    threading.Timer(5, qiwi_update).start()
Esempio n. 7
0
async def any_callback(callback: types.CallbackQuery):
    adress_id = int(callback.data.split()[1])

    message = callback.message
    user_id, message_id = message.chat.id, message.message_id
    sum = basket_sum(user_id)

    table = db_main.Table(tables.names_dict["user_order"])
    table.update("user_id", user_id, ["adress_id", "sum"], [adress_id, sum])
    table.close()

    await bot.edit_message_text(text="Адрес выбран",
                                chat_id=user_id,
                                message_id=message_id)

    text = "Совершите перевод на кошелек QIWI, при оплате обязательно укажите комментарий, приведенный ниже."
    text += "\nНомер: {number}\nСумма: {sum}\nКомментарий: {comment}".format(
        number=texts["number"], sum=sum, comment=user_id)
    link = "https://qiwi.com/payment/form/99?extra%5B%27account%27%5D={number}&amountInteger={sum}&amountFraction=0&currency=398&extra%5B%27comment%27%5D={comment}&currency=643&blocked[0]=sum&blocked[1]=account&blocked[2]=comment".format(
        number=texts["number"], sum=sum, comment=user_id)
    await bot.send_message(user_id,
                           text,
                           reply_markup=keyboard_main.url("Перейти в QIWI",
                                                          url=link))
    await bot.send_message(user_id,
                           "После оплаты нажмите кнопку проверки",
                           reply_markup=keyboard_main.inline(
                               "Проверка оплаты", "check_payment"))
Esempio n. 8
0
async def show_item(callback: types.CallbackQuery,
                    change_scroll=False,
                    scroll=True):

    message = callback.message
    category_id, position, photo_message_id = int(
        callback.data.split()[1]), int(callback.data.split()[2]), int(
            callback.data.split()[3])

    table = db_main.Table(tables.names_dict["items"])
    item = table.select(["name", "description", "price", "photo_id"],
                        ["category_id", "position"],
                        [category_id, position])[0]
    name, description, price, photo_id = item[0], item[1], item[2], item[3]
    table.close()

    if change_scroll == False:
        media = types.InputMediaPhoto(photo_id)
        await bot.edit_message_media(chat_id=message.chat.id,
                                     message_id=photo_message_id,
                                     media=media)

    keyboard, callback = generate_kb(category_id, position, photo_message_id,
                                     scroll)
    if change_scroll == False:
        await bot.edit_message_text(
            chat_id=message.chat.id,
            message_id=message.message_id,
            text=generate_item_text(name, description, price),
            reply_markup=keyboard_main.inline(keyboard, callback))
    else:
        await bot.edit_message_reply_markup(chat_id=message.chat.id,
                                            message_id=message.message_id,
                                            reply_markup=keyboard_main.inline(
                                                keyboard, callback))
Esempio n. 9
0
async def categories(message: types.Message):
    #
    # global categories
    # global count_items

    await bot.send_message(message.chat.id, message.text)

    #
    table = db_main.Table(tables.names_dict["items"])
    category_id = categories.index(message.text) + 1
    item = table.select(["name", "description", "price", "photo_id"],
                        ["category_id", "position"], [category_id, 1])[0]
    name, description, price, photo_id = item[0], item[1], item[2], item[3]
    table.close()

    media = types.MediaGroup()
    media.attach_photo(photo_id)
    photo_message = await bot.send_media_group(message.chat.id, media=media)
    photo_message_id = photo_message[0].message_id

    keyboard, callback = generate_kb(category_id, 1, photo_message_id)
    await bot.send_message(message.chat.id,
                           generate_item_text(name, description, price),
                           reply_markup=keyboard_main.inline(
                               keyboard, callback))
Esempio n. 10
0
async def new_adress(message: types.Message):
    table = db_main.Table(tables.names_dict["delivery_adress"])
    table.insert("delivery_adress", message.text)
    table.close()

    await bot.send_message(message.chat.id, "АДРЕС ДОБАВЛЕН")
    await menu(message)
Esempio n. 11
0
async def any_callback(callback: types.CallbackQuery):
    message = callback.message

    user_id, item_code, item_count = int(callback.data.split()[1]), int(
        callback.data.split()[2]), int(callback.data.split()[3])

    table = db_main.Table(tables.names_dict["basket"])

    if item_count > 0:
        table.update(["user_id", "item_code"], [user_id, item_code],
                     "item_count", item_count)
        table.close()
        text = generate_basket_text(user_id)
        keyboard, callback = genarate_basket_kb(user_id)
        await bot.edit_message_text(text=text,
                                    chat_id=user_id,
                                    message_id=message.message_id,
                                    reply_markup=keyboard_main.inline(
                                        keyboard, callback))
    else:
        table.delete(["user_id", "item_code"], [user_id, item_code])
        basket_items = table.select("item_code", "user_id", user_id)
        if len(basket_items) > 0:
            text = generate_basket_text(user_id)
            keyboard, callback = genarate_basket_kb(user_id)
            await bot.edit_message_text(text=text,
                                        chat_id=user_id,
                                        message_id=message.message_id,
                                        reply_markup=keyboard_main.inline(
                                            keyboard, callback))
        else:
            await bot.edit_message_text(text="Корзина пуста",
                                        chat_id=user_id,
                                        message_id=message.message_id)
        table.close()
Esempio n. 12
0
async def add_admin(message: types.Message):
    user_id = message.forward_from.id
    table = db_main.Table(tables.names_dict["admins"])
    table.insert("telegram_id", user_id)
    table.close()
    await bot.send_message(message.chat.id, "МЕНЕДЖЕР ДОБАВЛЕН")

    update_constants()
Esempio n. 13
0
async def new_item_name(message: types.Message):
    table = db_main.Table(tables.names_dict["items"])
    table.insert("name", message.text)
    table.close()

    await bot.send_message(message.chat.id,
                           texts["new_item_description"],
                           reply_markup=keyboard_main.force_reply())
Esempio n. 14
0
async def start_chat(message: types.Message):
    table = db_main.Table(tables.names_dict["texts"])
    table.update("title", "managers_chat", "text", message.chat.id)
    table.close()
    await bot.send_message(
        message.chat.id,
        "Приветствую, в данный чат будут приходить оплаченные заказы.")
    update_constants()
Esempio n. 15
0
def basket_sum(user_id):
    table = db_main.Table(tables.names_dict["basket"])
    data = table.select(["item_price", "item_count"], "user_id", user_id)
    table.close()
    sum = 0
    for price, count in data:
        sum += price * count
    return sum
Esempio n. 16
0
async def new_item_price(message: types.Message):
    category_name = categories[int(message.text) - 1]
    table = db_main.Table(tables.names_dict["categories"])
    data = table.select(["id", "count_items"], "name", category_name)[0]
    category_id, count_items = data[0], data[1]
    table.update("id", category_id, "count_items", count_items + 1)
    table.close()

    table = db_main.Table(tables.names_dict["items"])
    id = table.select_by_max("id", "id")[0][0]
    table.update("id", id, ["category_id", "position"],
                 [category_id, count_items + 1])
    table.close()

    update_constants()
    await bot.send_message(message.chat.id, "ТОВАР ДОБАВЛЕН")
    await menu(message)
Esempio n. 17
0
async def add_category(message: types.Message):
    table = db_main.Table(tables.names_dict["categories"])
    # !!!! count_items in categories
    table.insert("name", message.text)
    table.close()
    update_constants()
    await bot.send_message(message.chat.id, "КАТЕГОРИЯ ДОБАВЛЕНА")
    await menu(message)
Esempio n. 18
0
async def start(message: types.Message):
    table = db_main.Table(tables.names_dict["user_order"])
    data = table.select("user_id", "user_id", message.chat.id)
    if len(data) == 0:
        table.insert("user_id", message.chat.id)
    table.close()

    await bot.send_message(message.chat.id, "Рады приветствовать")
    await menu(message)
Esempio n. 19
0
async def cats(message: types.Message):
    print("ok")
    texts = db_main.Table(db_config.tables.texts)
    text = texts.insert("text", "title", "hello")
    await bot.send_message(message.chat.id,
                           text,
                           reply_markup=keyboard.reply([["Object #00001"],
                                                        ["Object #00002"],
                                                        ["HIDE KEYBOARD"]]))
Esempio n. 20
0
async def new_item_price(message: types.Message):
    table = db_main.Table(tables.names_dict["items"])
    data = table.select_by_max("id", "id")
    id = data[0][0]
    table.update("id", id, "price", message.text)
    table.close()

    await bot.send_message(message.chat.id,
                           texts["new_item_photo"],
                           reply_markup=keyboard_main.force_reply())
Esempio n. 21
0
async def number_qiwi(message: types.Message):

    table = db_main.Table(tables.names_dict["texts"])
    table.update("title", "qiwi_api_access_token", "text", message.text)
    table.close()

    update_constants()

    await bot.send_message(message.chat.id, "Номер QIWI ДОБАВЛЕН")
    await menu(message)
Esempio n. 22
0
async def basket_erase(callback: types.CallbackQuery):
    message = callback.message
    user_id, message_id = message.chat.id, message.message_id

    table = db_main.Table(tables.names_dict["basket"])
    table.delete("user_id", message.chat.id)
    table.close()

    await bot.edit_message_text(text="Корзина пуста",
                                chat_id=user_id,
                                message_id=message_id)
Esempio n. 23
0
async def number_qiwi(message: types.Message):

    table = db_main.Table(tables.names_dict["texts"])
    table.update("title", "number", "text", message.text)
    table.close()

    update_constants()

    user_id = message.chat.id
    keyboard = keyboard_main.force_reply()
    await bot.send_message(user_id, texts["token_qiwi"], reply_markup=keyboard)
Esempio n. 24
0
def basket_to_orders(user_id):

    table = db_main.Table(tables.names_dict["user_order"])
    adress_id = table.select("adress_id", "user_id", user_id)[0][0]
    table.update("user_id", user_id, "sum", 0)
    table.close()

    table = db_main.Table(tables.names_dict["delivery_adress"])
    delivery_adress = table.select("delivery_adress", "id", adress_id)[0][0]
    table.close()

    table = db_main.Table(tables.names_dict["basket"])
    basket = table.select(
        ["item_code", "item_name", "item_price", "item_count"], "user_id",
        user_id)
    table.close()

    order = randomword(10)

    table = db_main.Table(tables.names_dict["user_orders"])
    table.insert(["user_id", "order_code", "delivery_adress"],
                 [user_id, order, delivery_adress])
    table.close()

    table = db_main.Table(tables.names_dict["orders"])
    for item_code, item_name, item_price, item_count in basket:
        table.insert([
            "item_code", "item_name", "item_price", "item_count", "order_code"
        ], [item_code, item_name, item_price, item_count, order])
    table.close()

    table = db_main.Table(tables.names_dict["basket"])
    table.delete("user_id", user_id)
    table.close()
Esempio n. 25
0
 def __init__(self, id):
     table = db_main.Table(tables.names_dict["items"])
     data = table.select("*", "id", id)[0]
     table.close()
     self.name = data[1]
     description
     photo_id
     category_id
     ", "
     position
     ", "
     price
     ", "
     code
     "
Esempio n. 26
0
async def basket_open_message(message: types.Message):

    table = db_main.Table(tables.names_dict["basket"])
    data = table.select("item_count", "user_id", message.chat.id)
    table.close()

    if len(data) > 0:
        keyboard, callback = genarate_basket_kb(message.chat.id)
        text = generate_basket_text(message.chat.id)
        await bot.send_message(message.chat.id,
                               text,
                               reply_markup=keyboard_main.inline(
                                   keyboard, callback))
    else:
        await bot.send_message(message.chat.id, "Корзина пуста")
Esempio n. 27
0
async def any_callback(callback: types.CallbackQuery):
    item_code = int(callback.data.split()[1])
    message = callback.message
    user_id, message_id = message.chat.id, message.message_id

    table = db_main.Table(tables.names_dict["items"])
    name, description, price, photo_id = table.select(
        ["name", "description", "price", "photo_id"], "code", item_code)[0]
    table.close()

    media = types.MediaGroup()
    media.attach_photo(photo_id)
    await bot.send_media_group(user_id, media=media)

    text = generate_item_text(name, description, price)
    await bot.send_message(user_id, text)
Esempio n. 28
0
async def any_callback(callback: types.CallbackQuery):
    message = callback.message
    user_id, message_id = message.chat.id, message.message_id

    table = db_main.Table(tables.names_dict["delivery_adress"])
    adresses = table.select_all(["id", "delivery_adress"])
    table.close()

    keyboard, callback, text = [], [], ""

    for adress_id, adress in adresses:
        keyboard.append([adress])
        callback.append(["set_adress {adress_id}".format(adress_id=adress_id)])
        text += adress + "\n"
    keyboard.append(["Отмена"])
    callback.append(["cancel_adress"])

    await bot.send_message(user_id,
                           text,
                           reply_markup=keyboard_main.inline(
                               keyboard, callback))
Esempio n. 29
0
def genarate_basket_kb(user_id):
    keyboard = []
    callback = []

    table = db_main.Table(tables.names_dict["basket"])
    data = table.select(["item_code", "item_count"], "user_id", user_id)
    table.close()

    keyboard.append(["Перейти к оплате"])
    callback.append(["choose_adress"])

    for item in data:
        item_code, item_count = item[0], item[1]
        if item_count > 0:
            keyboard.append(["#{item_code}".format(item_code=item_code)])
            callback.append(
                ["show_naked_item {item_code}".format(item_code=item_code)])

            keyboard.append(["-1", "Удалить", "+1"])
            callback.append([
                "basket_set_item_count {user_id} {item_code} {item_count}".
                format(user_id=user_id,
                       item_code=item_code,
                       item_count=item_count - 1),
                "basket_set_item_count {user_id} {item_code} 0".format(
                    user_id=user_id, item_code=item_code),
                "basket_set_item_count {user_id} {item_code} {item_count}".
                format(user_id=user_id,
                       item_code=item_code,
                       item_count=item_count + 1)
            ])

    keyboard.append(["Очистить корзину"])
    callback.append(["basket_erase"])

    keyboard.append(["Перейти к оплате"])
    callback.append(["choose_adress"])

    return keyboard, callback
Esempio n. 30
0
async def new_item_category(message: types.Message):
    table = db_main.Table(tables.names_dict["items"])
    data = table.select_by_max("id", "id")
    id = data[0][0]
    table.update("id", id, "photo_id", message.photo[0].file_id)
    table.close()

    # print (message.photo[0].file_id)
    # print (message.reply_to_message)
    # print ("---\nphoooto\n---")
    #
    # media = types.MediaGroup()
    # for photo in message.photo:
    #     media.attach_photo(photo.file_id)
    # await bot.send_media_group(message.chat.id, media=media)
    text_categories = ""
    for i, category in enumerate(categories):
        text_categories += "\n{i}. {category}".format(i=i + 1,
                                                      category=category)
    await bot.send_message(message.chat.id,
                           texts["new_item_category"] + text_categories,
                           reply_markup=keyboard_main.force_reply())