-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram_handler.py
224 lines (187 loc) · 7.53 KB
/
telegram_handler.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import urllib
from config import *
import os
import requests
import telegram
import json
from PIL import Image
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
t_bot = telegram.Bot(BOT_TOKEN)
class ImgRequest:
def __init__(self, id_request):
self.id_request = id_request
#0 - is not set
#1 - waiting
#2 - set
self.got_img = 0
self.got_style = 0
self.img_token = None
self.style_token = None
self.queue_id = None
def download_img(self):
img_file = t_bot.getFile(self.img_token)
img_file_name = 'tmp/' + self.img_token + '.jpg'
img_file.download(img_file_name)
def download_style(self):
style_file_name = 'tmp/' + self.style_token + '.jpg'
style_file = t_bot.getFile(self.style_token)
style_file.download(style_file_name)
def set_img(self, token):
if self.got_img == 3:
os.remove('tmp/' + self.img_token + '.jpg')
self.img_token = token
self.got_img = 2
try:
self.download_img()
except Exception as err:
raise err
self.got_img = 3
if self.got_style == 3:
self.process_images()
def set_style(self, token):
if self.got_style == 3:
os.remove('tmp/' + self.style_token + '.jpg')
self.style_token = token
self.got_style = 2
try:
self.download_style()
except Exception as err:
raise err
self.got_style = 3
if self.got_img == 3:
self.process_images()
def process_images(self):
img_file_name = 'tmp/' + self.img_token + '.jpg'
style_file_name = 'tmp/' + self.style_token + '.jpg'
files = {'style': open(style_file_name, 'rb'),
'subject': open(img_file_name, 'rb')}
print('Sending...')
r = requests.post(NEURAL_API_HOST + '/api/image', files=files, data={'args': json.dumps({'iterations':50,
'back_host': THIS_HOST})})
print(r.text)
if 'id' in r.json():
self.queue_id = r.json()['id']
print(self.queue_id)
else:
raise ValueError('Oops')
class Client:
def __init__(self, chat_id):
self.chat_id = chat_id
self.requests = {}
self.num_requests = 0
def new_request(self):
cur_num = self.num_requests
self.num_requests += 1
self.requests[cur_num] = ImgRequest(cur_num)
return self.requests[cur_num]
def last_request(self):
if self.num_requests == 0:
return None
return self.requests[self.num_requests - 1]
clients = {}
uuids = {}
def done_img(img_id):
urllib.urlretrieve(NEURAL_API_HOST + '/api/image/' + str(img_id), 'tmp/' + str(img_id) + '.jpg')
client_id, img_num = uuids[img_id]
t_bot.sendMessage(client_id, text='Your image #{}'.format(img_num))
t_bot.sendPhoto(client_id, open('tmp/' + str(img_id) + '.jpg', 'rb'))
def start(bot, update):
bot.sendMessage(update.message.chat_id, text='Hi! Use /new to process a new image')
chat_id = update.message.chat_id
clients[chat_id] = Client(chat_id)
def process_image(bot, update):
chat_id = update.message.chat_id
bot.sendMessage(update.message.chat_id, text='Well, now send me an image')
if chat_id not in clients:
clients[chat_id] = Client(chat_id)
clients[chat_id].new_request()
request = clients[chat_id].last_request().got_img = 1
def got_img(bot, update):
chat_id = update.message.chat_id
img = update.message.photo
img_token = None
for ph in img:
if ph.width < 700 and ph.height < 700:
img_token = ph.file_id
if not img_token:
bot.sendMessage(update.message.chat_id, text='Image must be less than 700x700!')
return
request = clients[chat_id].last_request()
if request and request.got_img != 0:
if request.got_img != 3:
request.set_img(img_token)
bot.sendMessage(update.message.chat_id, text='Great! Now send me a style')
#except Exception as e:
# bot.sendMessage(update.message.chat_id, text=e.message)
else:
if request.got_style != 3:
request.set_style(img_token)
uuids[clients[chat_id].last_request().queue_id] = (chat_id, clients[chat_id].num_requests - 1, )
bot.sendMessage(update.message.chat_id, text="Ok, images are in processing now. "
"You'll get a result when it is done")
bot.sendMessage(update.message.chat_id, text="Your image's id is {}. You can use /status"
" with id to get your image's status".format(clients[chat_id].num_requests - 1))
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
def status(bot, update):
chat_id = update.message.chat_id
if chat_id not in clients:
clients[chat_id] = Client(chat_id)
if clients[chat_id].num_requests == 0:
bot.sendMessage(update.message.chat_id, text='You have no images in processing')
return
mes = update.message.text.split()
id = 0
if len(mes) == 1:
id = clients[chat_id].num_requests - 1
else:
id = mes[1]
if mes[1].isnumeric():
id = int(id)
if id not in clients[chat_id].requests:
bot.sendMessage(update.message.chat_id, text='You have no such image')
return
r = requests.get(NEURAL_API_HOST + '/api/image/' + clients[chat_id].requests[id].queue_id + '/status')
print(r.json())
if r.json()['status'] == 'queued' or r.json()['status'] == 'initializing':
bot.sendMessage(update.message.chat_id, text='This image is in queue')
elif r.json()['status'] == 'done':
bot.sendMessage(update.message.chat_id, text='This image is done')
else:
proc = int(100 * r.json()['done_iterations'] / r.json()['iterations_number'])
bot.sendMessage(update.message.chat_id, text='Processed {}%'.format(proc))
def delete(bot, update):
chat_id = update.message.chat_id
if chat_id not in clients:
clients[chat_id] = Client(chat_id)
if clients[chat_id].num_requests == 0:
bot.sendMessage(update.message.chat_id, text='You have no images in processing')
return
mes = update.message.text.split()
id = 0
if len(mes) == 1:
id = clients[chat_id].num_requests - 1
else:
id = mes[1]
if mes[1].isnumeric():
id = int(id)
if id not in clients[chat_id].requests:
bot.sendMessage(update.message.chat_id, text='You have no such image')
return
r = requests.delete(NEURAL_API_HOST + '/api/image/' + clients[chat_id].requests[id].queue_id)
bot.sendMessage(update.message.chat_id, text='Image #{} was deleted!'.format(id))
del clients[chat_id].requests[id]
def start_bot():
updater = Updater(BOT_TOKEN)
dp = updater.dispatcher
dp.addHandler(CommandHandler("start", start))
dp.addHandler(CommandHandler("new", process_image))
dp.addHandler(CommandHandler("status", status))
dp.addHandler(CommandHandler("delete", delete))
dp.addHandler(MessageHandler([Filters.photo], got_img))
dp.addErrorHandler(error)
updater.start_polling()