diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..2d86b48 --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +TOKEN1= +TOKEN2= +BACKEND_URL= +ADMIN_ID= diff --git a/main.py b/main.py index 13e60c9..0c739aa 100644 --- a/main.py +++ b/main.py @@ -4,11 +4,15 @@ from aiogram import Bot, Dispatcher, types, filters from aiogram.types import ReplyKeyboardMarkup, KeyboardButton, WebAppInfo from aiogram.fsm.state import StatesGroup, State from aiogram.fsm.context import FSMContext +from dotenv import load_dotenv -TOKEN1 = "7003564044:AAE4R5Nk-E74hOno932iJINNPW3YRjjH8Mo" # web app -TOKEN2 = "8043593409:AAEC4GfxOyfv9LmbfLiwU7_g-mdtuj3y8rI" # delivery +load_dotenv() + +TOKEN1 = os.getenv("TOKEN1") +TOKEN2 = os.getenv("TOKEN2") +BACKEND_URL = os.getenv("BACKEND_URL") +ADMIN_ID = int(os.getenv("ADMIN_ID", "0")) -BACKEND_URL = 'https://agro.felixits.uz' bot1 = Bot(token=TOKEN1) bot2 = Bot(token=TOKEN2) dp1 = Dispatcher() @@ -18,6 +22,10 @@ class CodeStates(StatesGroup): phone = State() full_name = State() +class Broadcast(StatesGroup): + waiting_for_message = State() + + @dp2.message(filters.CommandStart()) async def start_handler(message: types.Message, state: FSMContext): @@ -82,6 +90,36 @@ async def start_handler(message: types.Message): ) await message.answer("Salom! Web Appni ochish uchun tugmani bosing 👇", reply_markup=keyboard) + +@dp1.message(filters.Command("send")) +async def start_broadcast(message: types.Message, state: FSMContext): + if message.from_user.id != ADMIN_ID: + return await message.answer("❌ Sizda ruxsat yo‘q") + + await message.answer("Yubormoqchi bo‘lgan xabaringizni kiriting:") + await state.set_state(Broadcast.waiting_for_message) + + +@dp1.message(Broadcast.waiting_for_message) +async def broadcast_message(message: types.Message, state: FSMContext): + msg = message.text + count = 0 + url = f'{BACKEND_URL}/api/v1/accounts/user/list/' + res = requests.get(url) + for chat_id in res.json(): + if chat_id['tg_id'] == ADMIN_ID: + continue + try: + await bot1.send_message(chat_id['tg_id'], msg) + count += 1 + except Exception as e: + print(f"Xato {chat_id['tg_id']}: {e}") + + await message.answer(f"✅ {count} ta foydalanuvchiga yuborildi") + await state.clear() + + + async def main(): await asyncio.gather( dp1.start_polling(bot1), @@ -90,4 +128,4 @@ async def main(): if __name__ == "__main__": logging.basicConfig(level=logging.INFO, stream=sys.stdout) - asyncio.run(main()) + asyncio.run(main()) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e3ab600..060af11 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,4 +16,5 @@ pydantic_core==2.33.2 typing-inspection==0.4.1 typing_extensions==4.15.0 yarl==1.20.1 -requests \ No newline at end of file +requests +python-dotenv \ No newline at end of file