S
S
SashaN692020-12-09 19:31:30
Python
SashaN69, 2020-12-09 19:31:30

Telegram bot information output?

Good day! I am writing a telegram bot in python for automatic sales, using the aiogram library. When you press the / start command, the bot sends a list of cities, in this format


Cities: City1
City2
Etc.

Using the state machine, the user sends the name of the city to the bot, and in response it sends all the goods in this city.
Problem:
You need to do something so that instead of the name of the city, the user can send the number of the city, let's say 1 is city1, 2 is city2, and so on, and in response the bot sent the goods in this city.
@dp.message_handler(state=Form.choose_district)
async def process_name(message: types.Message, state: FSMContext):
    """
    Process user name
    """
    async with state.proxy() as data:
        data['choose_district'] = message.text
        try:
            f = open('product.txt', 'w')
            g = open('product.txt', 'r')
            for element in db.check_product(data['choose_district']):
                f.writelines(element)
                f.write('\n')
            f.close()
            for line in g.readlines():
                s = re.split(r'I', line)
                await bot.send_message(message.from_user.id, "Товары - цена:\n"
                                                             "{0} - {1} \n".format(s[0], s[1], bot.get_me()),
                                       reply_markup=inline_kb1)
        except:
            await bot.send_message(message.from_user.id, "Такого района не существует")

    await state.finish()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2020-12-09
@SashaN69

Somewhere there should be a database, and in it a table with the names of cities. It is necessary to add a column with city numbers (unique) to this table, or create a new city-to-number correspondence table, and use this column of numbers to find the city by number, and then pass the city itself to the db.check_product (city) query, if I'm correct understood the meaning of this code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question