-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
148 lines (127 loc) · 6.11 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# import libraries
import asyncio
from telebot import types
from transfer_style import Style_Transfer
from telebot.async_telebot import AsyncTeleBot
import os
os.mkdir("pictures")
bot = AsyncTeleBot('2110529010:AAH0tkIHL74pQbphax1XPt-D7avkEc2knL0')
path = 'pictures/'
# reaction to 'start' command
@bot.message_handler(commands=['start'])
async def start(message):
mess_of_start = f"""Hi <b>{message.from_user.first_name}</b>
I'm bot and I was created to transfer style your photos.
You can send me /help to get more details"""
# make button
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
help_button = types.KeyboardButton('/help')
markup.add(help_button)
# send message with button
await bot.send_message(message.chat.id, mess_of_start, parse_mode='html', reply_markup=markup)
# reaction to 'help' command
@bot.message_handler(commands=['help'])
async def help(message):
# message
mess_of_help = f"""I can:
info - to find out about the project
style - to transfer style from a photo to another"""
# make buttons
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
style_button = types.KeyboardButton('style')
info_button = types.KeyboardButton('info')
markup.add(style_button, info_button)
# send message with buttons
await bot.send_message(message.chat.id, mess_of_help, parse_mode='html', reply_markup=markup)
# reaction to 'info' message
@bot.message_handler(commands=['info'])
async def info(message):
# message
mess_of_info = f"""I was created by Rostislav Misiukevich for project's MFTI. Creator's contacts:
426913702 - Stepic
@__r.o.s.t.i.k_ - Instagram
@Rostislav000 - Telegram"""
# make button
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
style_button = types.KeyboardButton('style')
markup.add(style_button)
# send message with button
await bot.send_message(message.chat.id, mess_of_info, parse_mode='html', reply_markup=markup)
# reaction to any text from user
@bot.message_handler(content_types=['text'])
async def get_user_text(message):
if message.text.lower() == 'hi' or message.text.lower() == 'hello':
# make buttons
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
style_button = types.KeyboardButton('style')
info_button = types.KeyboardButton('info')
markup.add(style_button, info_button)
# send message with buttons
await bot.send_message(message.chat.id, f"Hello <b>{message.from_user.first_name}</b>", parse_mode='html',
reply_markup=markup)
elif message.text.isnumeric() is True:
if len(os.listdir(path)) == 2 and 0 < int(message.text) < 11:
result = Style_Transfer(path_style_image='pictures/style.jpg', path_content_image='pictures/cont.jpg',
num_steps_from_user=int(message.text)).sdf()
await bot.send_photo(message.chat.id, result)
elif len(os.listdir(path)) == 2:
await bot.send_message(message.chat.id, 'Send me a number from 1 to 10', parse_mode='html')
else:
# message
mess_of_sm = "I don't understand you, use /help"
# make button
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
help_button = types.KeyboardButton('/help')
markup.add(help_button)
# send message with button
await bot.send_message(message.chat.id, mess_of_sm, parse_mode='html', reply_markup=markup)
elif message.text.lower() == 'style':
# send message
await bot.send_message(message.chat.id, f"Send me a picture from which you want take style", parse_mode='html')
os.remove(path+'cont.jpg')
os.remove(path+'style.jpg')
elif message.text.lower() == 'info':
# message
mess_of_info = f"""I was created by Rostislav Misiukevich for project's MFTI. Creator's contacts:
426913702 - Stepic
@__r.o.s.t.i.k_ - Instagram
@Rostislav000 - Telegram"""
# make buttons
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
style_button = types.KeyboardButton('style')
markup.add(style_button)
# send message with buttons
await bot.send_message(message.chat.id, mess_of_info, parse_mode='html', reply_markup=markup)
else:
# message
mess_of_sm = "I don't understand you, use /help"
# make button
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
help_button = types.KeyboardButton('/help')
markup.add(help_button)
# send message with button
await bot.send_message(message.chat.id, mess_of_sm, parse_mode='html', reply_markup=markup)
# save photo from user, apply function 'transform' and send result to user
@bot.message_handler(content_types=['photo'])
async def get_style_photo(message):
num_of_pictures = len(os.listdir(path))
if num_of_pictures == 2:
os.remove(path+'cont.jpg')
os.remove(path+'style.jpg')
num_of_pictures = len(os.listdir(path))
if num_of_pictures == 0:
await bot.send_message(message.chat.id, 'Saving style...')
file_info = await bot.get_file(message.photo[-1].file_id)
downloaded_file = await bot.download_file(file_info.file_path)
with open('pictures/style.jpg', 'wb') as new_file:
new_file.write(downloaded_file)
await bot.send_message(message.chat.id, 'Send me another photo on which you apply the style')
elif num_of_pictures == 1:
await bot.send_message(message.chat.id, 'Saving content...')
file_info = await bot.get_file(message.photo[-1].file_id)
downloaded_file = await bot.download_file(file_info.file_path)
with open('pictures/cont.jpg', 'wb') as new_file:
new_file.write(downloaded_file)
await bot.send_message(message.chat.id, 'Send me a number from 1 to 10 - degree of transfer')
# continuous working of bot
asyncio.run(bot.polling(none_stop=True, request_timeout=100000000000000000000000))