Перейти до змісту

Рекомендовані відповіді

Опубліковано
  • Адміністратор

Телеграм бот на Python для створення своїх стікерпаків

Вітаю всіх користувачів форуму BlastHack! Я довго думав над створенням даного телеграм бота і ось придумав ідею, так що уявляю вам телеграм бота для створення стікерпаків!

Функціонал:
• Можливість створити свій стікерпак та додавати туди стікери.
• Ну, а так все!

Скріншоти:

Screenshot_1.pngScreenshot_1.png

А ось і код:

from aiogram import Bot, Dispatcher, types, F
from aiogram.filters import Command, CommandStart
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import State, StatesGroup
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardRemove
from PIL import Image
import asyncio
import os
import randomclass StickerCreation(StatesGroup):
    waiting_for_name = State()
    waiting_for_first_sticker = State()
    waiting_for_sticker = State()
bot = Bot(token='Ваш токен')
storage = MemoryStorage()
dp = Dispatcher(storage=storage)if not os.path.exists("temp"):
    os.makedirs("temp")
start_kb = ReplyKeyboardMarkup(
    keyboard=[
        [KeyboardButton(text=" Создать стикерпак")]
    ],
    resize_keyboard=True)
sticker_kb = ReplyKeyboardMarkup(
    keyboard=[
        [KeyboardButton(text=" Завершить создание")]
    ],
    resize_keyboard=True)async def get_bot_username():
    bot_info = await bot.get_me()
    return bot_info.usernameasync def generate_pack_name(user_id: int, bot_username: str) -> str:
    random_id = random.randint(1, 999999)
    return f"pack{user_id}_{random_id}_by_{bot_username}"def process_sticker_image(input_path: str) -> str:
    output_path = input_path.replace('.png', '_processed.png')
    with Image.open(input_path) as img:
        if img.mode != 'RGBA':
            img = img.convert('RGBA')
            
        new_img = Image.new('RGBA', (512, 512), (0, 0, 0, 0))
        
        ratio = min(512.0 / img.width, 512.0 / img.height)
        new_size = (int(img.width * ratio), int(img.height * ratio))
        resized_img = img.resize(new_size, Image.Resampling.LANCZOS)
        
        x = (512 - new_size[0]) // 2
        y = (512 - new_size[1]) // 2
        
        new_img.paste(resized_img, (x, y), resized_img)
        
        new_img.save(output_path, 'PNG')
    
    os.remove(input_path)
    return [email protected](CommandStart())async def start_handler(message: types.Message):
    await message.answer(
        "Приветствую! Я помогу создать стикерпак. Нажми кнопку ниже или используй /create для начала либо нажмите кнопку снизу!",
        reply_markup=start_kb
    )@dp.message(Command("create"))@dp.message(F.text == " Создать стикерпак")async def create_stickerpack(message: types.Message, state: FSMContext):
    await message.answer(
        "Пожалуйста, отправьте название для вашего стикерпака",
        reply_markup=ReplyKeyboardRemove()
    )
    await state.set_state(StickerCreation.waiting_for_name)@dp.message(StickerCreation.waiting_for_name)async def process_name(message: types.Message, state: FSMContext):
    await state.update_data(title=message.text)
    await message.answer(
        "Отлично! Теперь отправьте первый стикер для пака (изображение)",
        reply_markup=ReplyKeyboardRemove()
    )
    await state.set_state(StickerCreation.waiting_for_first_sticker)@dp.message(StickerCreation.waiting_for_first_sticker, F.photo)async def process_first_sticker(message: types.Message, state: FSMContext):
    try:
        file_path = f"temp/{message.photo[-1].file_id}.png"
        await bot.download(message.photo[-1].file_id, destination=file_path)
        
        processed_path = process_sticker_image(file_path)
        
        data = await state.get_data()
        user_id = message.from_user.id
        bot_username = await get_bot_username()
        pack_name = await generate_pack_name(user_id, bot_username)
        
        await bot.create_new_sticker_set(
            user_id=user_id,
            name=pack_name,
            title=data['title'],
            stickers=[{
                'sticker': types.FSInputFile(processed_path),
                'emoji_list': [''],
                'format': 'static'
            }],
            sticker_format='static'
        )
        
        await state.update_data(pack_name=pack_name)
        os.remove(processed_path)
        
        await message.answer(
            "Стикерпак создан! Отправьте ещё стикеры или нажмите кнопку для завершения",
            reply_markup=sticker_kb
        )
        await state.set_state(StickerCreation.waiting_for_sticker)
    except Exception as e:
        await message.answer(
            f"Произошла ошибка: {str(e)}",
            reply_markup=start_kb
        )
        await state.clear()@dp.message(StickerCreation.waiting_for_sticker, F.photo)async def process_sticker(message: types.Message, state: FSMContext):
    try:
        file_path = f"temp/{message.photo[-1].file_id}.png"
        await bot.download(message.photo[-1].file_id, destination=file_path)
        
        processed_path = process_sticker_image(file_path)
        
        data = await state.get_data()
        pack_name = data['pack_name']
        user_id = message.from_user.id
        
        await bot.add_sticker_to_set(
            user_id=user_id,
            name=pack_name,
            sticker={
                'sticker': types.FSInputFile(processed_path),
                'emoji_list': [''],
                'format': 'static'
            }
        )
        
        os.remove(processed_path)
        
        await message.answer(
            "Стикер успешно добавлен! Отправьте ещё один или нажмите кнопку для завершения",
            reply_markup=sticker_kb
        )
    except Exception as e:
        await message.answer(f"Произошла ошибка при добавлении стикера: {str(e)}")
        await state.clear()@dp.message(Command("done"))@dp.message(StickerCreation.waiting_for_sticker, F.text == " Завершить создание")async def finish_creation(message: types.Message, state: FSMContext):
    data = await state.get_data()
    pack_name = data['pack_name']
    await message.answer(
        f"Ваш стикерпак готов!\nt.me/addstickers/{pack_name}",
        reply_markup=start_kb
    )
    await state.clear()@dp.message(StickerCreation.waiting_for_first_sticker)@dp.message(StickerCreation.waiting_for_sticker)async def wrong_input(message: types.Message):
    if not message.photo:
        await message.answer("Пожалуйста, отправьте изображение для стикера")async def main():
    await dp.start_polling(bot)if __name__ == '__main__':
    asyncio.run(main())

Бібліотеки необхідні роботи бота:

Бібліотека 1:

pip install Pillow

Бібліотека 2:

pip install aiogram

Для публікації повідомлень створіть обліковий запис або авторизуйтесь

Важлива інформація

By using this site, you agree to our Умови використання.