S
S
Studentka19962020-02-03 15:56:00
Python
Studentka1996, 2020-02-03 15:56:00

Why doesn't the program define call_1?

Hello. I ask for help, because "I walk in a circle"
There are inline buttons:

#создаём инлайн меню выбора метода доставки
adress_dostavki = telebot.types.InlineKeyboardMarkup()  
adress_list = [('ул. Студенческая, д.33, кор.8'),
    ('ул. Дербенёвская, д.67, стр. 11'),
    ('ул. Чершская, д.122, стр.5'),
    ('ул. Красные холмы, д.88, кор. 12')
     ]
for row in adress_list:
     adress_dostavki.add( 
        telebot.types.InlineKeyboardButton(
             f'{row}', callback_data = f'АдресAdd_{row}')
             ) 
#Создаём инлайн меню
time = telebot.types.InlineKeyboardMarkup()
time_list = [
    ('10:15'), ('12:00'), ('13:15') ]
for items in time_list:
     time.add( 
        telebot.types.InlineKeyboardButton(
             f'{items}', callback_data = f'ВремяAdd_{items}')
             )


How to correctly write values ​​to a tuple?
Why doesn't the program define call_1? Reading from top to bottom is not worth mentioning)))
#Добавление инфы о доставке в таблицу Доставка
    insert_query_Dostavka = '''INSERT OR IGNORE INTO Доставка(Адрес_доставки,
            Дата_доставки) VALUES (?, ?)''' 
    
    call_1 = text.split('АдресAdd_')[-1] 
    call_2 = text.split('ВремяAdd_')[-1] 
    if 'АдресAdd' in text:
        bot.send_message(message.chat.id, 'Доставка на сегодня. Выберите время:', reply_markup=interface.time)
    if 'ВремяAdd' in text:
            tuple_Dostavka = tuple([call_1 + call_2])
            cur.execute(insert_query_Dostavka, tuple_Dostavka)
            con.commit()                       
            print(tuple_Dostavka) #результат [время, время]


You need to write down the values ​​​​in this variant: [address, time]. The question is similar to the previous one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Taus, 2020-02-03
@Taus

In the second piece of code call_1, and call_2will never be defined together, because only one button click will be processed. You need to store this data "somewhere", for example in a global object:

class Order:
    def __init__(self, address):
        self.address = address
        self.date = None
        
orders = {}
...
@bot.callback_query_handler(lambda query: 'АдресAdd_' in query.data or 'ВремяAdd_' in query.data)
def process_callback(query):
    chat_id = query.message.chat.id
    if 'АдресAdd' in query.data:
        address = query.data.lstrip('АдресAdd_')
        orders[chat_id] = Order(address)
        bot.send_message(chat_id, 'Доставка на сегодня. Выберите время:', reply_markup=interface.time)
    elif 'ВремяAdd' in query.data:
        date = query.data.lstrip('ВремяAdd_')
        order = orders[chat_id]
        order.date = date               
        print((order.address, order.date))

PS: You callback_datacan use strings of 1-64 bytes in length .
PSS: tuple([call_1 + call_2])will create a tuple object with one element, which is equal to a string with an address and a time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question