V
V
volodik5872021-09-06 16:15:39
Python
volodik587, 2021-09-06 16:15:39

Error when sending to users, what could be?

The aiogram code throws an error

first_user = call_sp[0][0]
IndexError: list index out of range

Mailing code:

@dp.message_handler(lambda message: message.text == 'Розсилка на random user')
async def rozsulka(message):
    user_id = str(message.from_user.id)
    if user_id in config.admin:
        await message.reply('Очікую повідомлення...')
        await Mailing.Text.set()
    else:
        mention = "[" + str(message.from_user.first_name) + \
            "](tg://user?id=" + str(message.from_user.id) + ")"
        await bot.send_message(config.error, f"{mention} хоче зробити розсилку", parse_mode="Markdown")

@dp.message_handler(state=Mailing.Text, content_types=types.ContentType.ANY)
async def rozsulka_f(message: types.Message, state: FSMContext):
    async with state.proxy() as data:
        data['Text'] = message.text
    await state.update_data(Text=(message.text))
    await message.reply('Введіть кількість користувачів:')
    await Mailing.next()

@dp.message_handler(lambda message: message.text.isdigit(), state=Mailing.Count_User, content_types=types.ContentType.ANY)
async def rozsulka_full(message: types.Message, state: FSMContext):
    result = ""
    with sq.connect("clients.db") as con:
        cur = con.cursor()
        result = cur.execute('SELECT `id` FROM `users`').fetchall()
    call_sp = cur.fetchall()
    error = 0
    good = 0
    async with state.proxy() as data:
        data['Count_User'] = message.text
    
    msg = data['Text']
    count = int(data['Count_User'])
    for user in result:
        first_user = call_sp[0][0]
        del [call_sp[0]]
        user = user[0]
        if(count > good):
            if(random.choice([True, False])):
                try:
                    await bot.send_message(user, str(msg))
                    good += 1
                except:
                    error += 1

    await bot.send_message(config.error, f"Розсилка: \nУспішно: {good} \nНе пройшло: {error}")

    await state.finish()


IDs are in the database

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-09-06
@Vindicar

result = cur.execute('SELECT `id` FROM `users`') .fetchall()
call_sp = cur .fetchall()
The second .fetchall() won't return anything - the first one fetched all the data given to the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question