При рассылке только текста пользователям нет ошибок.
@router.callback_query(F.data == "next", bot_mailingg.text)
async def start(call: types.CallbackQuery, state: FSMContext, bot: Bot):
data = await state.get_data()
text = data.get('text')
await state.clear()
with sqlite3.connect('tg2.db') as con:
cur = con.cursor()
rows = cur.execute('SELECT user_id FROM users').fetchall()
for row in rows:
await bot.send_message(row[0], text)
await bot.send_message(call.from_user.id, "Успешная рассылка")
При попытке разослать и фото и текст выходит следующая ошибка
@router.callback_query(F.data == "next", bot_mailingg.photo)
async def start(call: types.CallbackQuery, state: FSMContext, bot: Bot):
data = await state.get_data()
text = data.get('text')
photo = data.get('photo')
await state.clear()
with sqlite3.connect('tg2.db') as con:
cur = con.cursor()
rows = cur.execute('SELECT user_id FROM users').fetchall()
for row in rows:
await bot.send_message(row[0], photo, text)
await bot.send_message(call.from_user.id, "Успешная рассылка")
Ошибка:
pydantic.error_wrappers.ValidationError: 1 validation error for SendMessage
message_thread_id
value is not a valid integer (type=type_error.integer)
Где и как нужно исправить код чтобы все работало?
Весь код:
import sqlite3
from aiogram.fsm.context import FSMContext
from aiogram import F, types, Bot, Router
import config
from kb import kb_state
from handlers import Message
from states import bot_mailingg
router = Router()
@router.message(F.text == "Создать рассылку")
async def start_mailing(msg: Message, state: FSMContext):
if msg.from_user.id == config.ADMIN_ID:
await msg.answer(f"Введите текст рассылки")
await state.set_state(bot_mailingg.text)
else:
await msg.answer("Я вас не понимаю. Если возникли трудности, перезапустите меня командой /start")
@router.message(bot_mailingg.text)
async def text(msg: Message, state: FSMContext):
answer = msg.text
await state.update_data(text=answer)
await msg.answer(text=answer, reply_markup=kb_state.ForP)
await state.set_state(bot_mailingg.text)
@router.callback_query(F.data == "next", bot_mailingg.text)
async def start(call: types.CallbackQuery, state: FSMContext, bot: Bot):
data = await state.get_data()
text = data.get('text')
await state.clear()
with sqlite3.connect('tg2.db') as con:
cur = con.cursor()
rows = cur.execute('SELECT user_id FROM users').fetchall()
for row in rows:
await bot.send_message(row[0], text)
await bot.send_message(call.from_user.id, "Успешная рассылка")
@router.callback_query(F.data == "add_photo", bot_mailingg.text)
async def add_photo(call: types.CallbackQuery, state: FSMContext):
await call.message.answer("Пришлите фото")
await state.set_state(bot_mailingg.photo)
@router.message(bot_mailingg.text)
async def no_photo(msg: Message):
await msg.answer("Пришлите мне фотографию", reply_markup=kb_state.otm)
@router.message(F.photo, bot_mailingg.photo)
async def mailing_txt(msg: Message, state: FSMContext):
photo_file_id = msg.photo[-1].file_id
await state.update_data(photo=photo_file_id)
data = await state.get_data()
text = data.get('text')
photo = data.get('photo')
await msg.answer_photo(photo=photo, caption=text, reply_markup=kb_state.next)
@router.callback_query(F.data == "next", bot_mailingg.photo)
async def start(call: types.CallbackQuery, state: FSMContext, bot: Bot):
data = await state.get_data()
text = data.get('text')
photo = data.get('photo')
await state.clear()
with sqlite3.connect('tg2.db') as con:
cur = con.cursor()
rows = cur.execute('SELECT user_id FROM users').fetchall()
for row in rows:
await bot.send_message(row[0], photo, text)
await bot.send_message(call.from_user.id, "Успешная рассылка")
@router.callback_query(F.data == 'quit', bot_mailingg.text, bot_mailingg.photo, bot_mailingg.video, bot_mailingg.state)
async def quite(call: types.CallbackQuery, state: FSMContext):
await state.clear()
await call.message.answer("Рассылка отменена")