Skip to content

Instantly share code, notes, and snippets.

@PlugManYT
Created June 27, 2025 13:05
Show Gist options
  • Save PlugManYT/03751c572200c8f89134fd5ce4d7e0fe to your computer and use it in GitHub Desktop.
Save PlugManYT/03751c572200c8f89134fd5ce4d7e0fe to your computer and use it in GitHub Desktop.
Код из первой части курса по написанию телеграм ботов на aiogram==3.13.1
import asyncio
from aiogram import Bot, Dispatcher, types
from aiogram.filters import Command, CommandStart
from aiogram.types import Message, InlineKeyboardButton, InlineKeyboardMarkup
from random import choice
API_TOKEN = "7768927849:AAH5KwOFbzA_it5RppfdK8cC_MG0uRaBi74"
bot = Bot(token=API_TOKEN)
dp = Dispatcher()
def get_color_keyboard():
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[InlineKeyboardButton(text="🔴Красный", callback_data="color_red"),
InlineKeyboardButton(text="🔵Синий", callback_data= "color_blue")],
[InlineKeyboardButton(text="🟢Зелёный", callback_data= "color_green")]
])
return keyboard
@dp.message(CommandStart())
async def send_welcome(message: Message):
await message.answer("Привет! Выбери свой любимый цвет:", reply_markup=get_color_keyboard())
@dp.callback_query()
async def handle_color_choice(callback: types.CallbackQuery):
color_map = {
"color_red": "🔴Красный",
"color_blue": "🔵Синий",
"color_green": "🟢Зелёный"
}
response = color_map.get(callback.data, "Неизвестный цвет")
await callback.message.answer(f"Ты выбрал: {response}")
await callback.answer()
@dp.message(Command("random_color"))
async def send_random_color(message:Message):
colors = ["🔴Красный","🔵Синий","🟢Зелёный"]
color = choice(colors)
await message.answer(f"Случайный цвет: {color}")
async def main():
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment