HOME FORUMS MEMBERS RECENT POSTS LOG IN  
× Авторизация
Имя пользователя:
Пароль:
Нет аккаунта? Регистрация
Баннер 1   Баннер 2
НОВЫЕ ТОРГОВАЯ НОВОСТИ ЧАТ
loading...
Скрыть
Вернуться   ANTICHAT > ПРОГРАММИРОВАНИЕ > Python
   
 
 
Опции темы Поиск в этой теме Опции просмотра

  #1  
Старый 24.11.2024, 03:03
Python_Fanat
Новичок
Регистрация: 17.10.2024
Сообщений: 24
С нами: 829557

Репутация: 8
По умолчанию

Телеграмбот на Python для создания своих стикерпаков​
Приветствую всех пользователей форума BlastHack! Я долго думал над созданием данного телеграм бота и вот наконец-то придумал идею, так-что представляю вам телеграм бота для создания стикерпаков!

Функционал:


• Возможность создать свой стикерпак и добавлять туда стикеры.

• Ну а так всё!

Скриншоты:





А вот и код:


Телеграм бот для создания стикерпаков:





Код:
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
random
class
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
.
username
async
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
output_path
@dp.message
(
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


 
Ответить с цитированием
 





Здесь присутствуют: 1 (пользователей: 0 , гостей: 1)
 


Быстрый переход




ANTICHAT ™ © 2001- Antichat Kft.