-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot
270 lines (212 loc) · 10.5 KB
/
Bot
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import json
from project.settings import TG_TOKEN_ADMIN
from tg_admin_bot import core
from tg_admin_bot.core import AdminBot
from tg_admin_bot.utils import keyboards
if not TG_TOKEN_ADMIN:
raise ValueError('TG_TOKEN_ADMIN не может быть пустым')
bot = AdminBot(TG_TOKEN_ADMIN)
# Команда /start
@bot.message_handler(commands=['start'])
def start_message(message):
chat_id = message.chat.id
bot.send_message(chat_id=chat_id, text=bot.get_start_message())
user = core.User(chat_id)
if not user.is_authenticated():
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
return
bot.send_message(chat_id=chat_id, text=bot.get_instruction(), reply_markup=keyboards.get_main_menu_keyboard())
def authorization(message):
chat_id = message.chat.id
token = message.text
username = message.from_user.username
if core.User.authorization(chat_id=chat_id, token=token, username=username):
bot.send_message(chat_id=chat_id, text=bot.get_instruction(), reply_markup=keyboards.get_main_menu_keyboard())
else:
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
@bot.message_handler(regexp='^Основное меню$')
def main_menu(message):
chat_id = message.chat.id
user = core.User(chat_id)
if not user.is_authenticated():
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
return
bot.send_message(chat_id=chat_id, text='Основное меню',
reply_markup=keyboards.get_main_menu_keyboard())
@bot.message_handler(regexp='^Авторы')
def authors(message):
chat_id = message.chat.id
user = core.User(chat_id)
if not user.is_authenticated():
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
return
bot.send_message(chat_id=chat_id, text=bot.get_authors())
@bot.message_handler(regexp='^Начать трансляцию$')
def text_translation(message):
chat_id = message.chat.id
user = core.User(chat_id)
if not user.is_authenticated():
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
return
matches_queryset = core.TextTranslation.get_matches_of_the_day()
if matches_queryset:
bot.send_message(chat_id=chat_id,
text='Трансляцию какого матча Вы собираетесь вести?',
reply_markup=keyboards.get_inline_text_translation_matches_keyboard(matches_queryset))
else:
bot.send_message(chat_id=chat_id,
text='Сегодня матчей нет', reply_markup=keyboards.get_main_menu_keyboard())
@bot.callback_query_handler(func=lambda message: 'match' in message.data)
def text_translation_start(message):
chat_id = message.message.chat.id
message_id = message.message.message_id
match_id = json.loads(message.data)['match']
match = core.TextTranslation(match_id=match_id).object
if match:
bot.edit_message_text(chat_id=chat_id, message_id=message_id, text=match.title)
msg = bot.send_message(chat_id=chat_id,
text='Окей! Запускаем вещание...\n'
'Все, Вы в эфире.\n\n'
'Для того, чтобы прекратить трансляцию, необходимо нажать на кнопку “Стоп”',
reply_markup=keyboards.get_stop_text_translation_keyboard())
bot.register_next_step_handler(msg, translation, match_id=match_id)
else:
bot.send_message(chat_id=chat_id,
text=bot.get_error_text(),
reply_markup=keyboards.get_main_menu_keyboard())
def translation(message, match_id):
chat_id = message.chat.id
user = core.User(chat_id)
if not user.is_authenticated():
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
return
text = message.text
if text == 'Стоп':
text_translation_stop(message)
return
match_translation = core.TextTranslation(match_id)
bot.send_message(chat_id=chat_id, text='Отправка...')
match_translation.send_text_translation_message(text=text)
msg = bot.send_message(chat_id=chat_id, text='Собщение отправлено')
bot.register_next_step_handler(msg, translation, match_id=match_id)
@bot.message_handler(regexp='^Стоп$')
def text_translation_stop(message):
chat_id = message.chat.id
user = core.User(chat_id)
if not user.is_authenticated():
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
return
bot.send_message(chat_id=chat_id,
text='Трансляция остановлена',
reply_markup=keyboards.get_main_menu_keyboard())
@bot.message_handler(regexp='^Новости$')
def news_menu(message):
chat_id = message.chat.id
user = core.User(chat_id)
if not user.is_authenticated():
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
return
bot.send_message(chat_id=chat_id, text='Новости',
reply_markup=keyboards.get_news_keyboard())
@bot.message_handler(regexp='^Опубликовать новость$')
def write_news_start(message):
chat_id = message.chat.id
user = core.User(chat_id)
if not user.is_authenticated():
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
return
bot.send_message(chat_id=chat_id, text='Для того, чтобы опубликовать новость, '
'напишите текст новости в чат.\n\n'
'Ждем вестей!',
reply_markup=keyboards.get_cancel_write_news_keyboard())
msg = bot.send_message(chat_id=chat_id, text='ВАЖНО!\n'
'Пожалуйста, '
'Перед отправкой проверьте текст новости на наличие ошибок и опечаток.')
bot.register_next_step_handler(msg, write_news)
def write_news(message):
chat_id = message.chat.id
user = core.User(chat_id)
if not user.is_authenticated():
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
return
text = message.text
if text == 'Отмена':
write_news_cancel(message)
return
photo = None
if message.content_type == 'photo':
file_id = message.photo[-1].file_id
file_info = bot.get_file(file_id)
photo = bot.download_file(file_info.file_path)
text = message.caption
news = core.TgNews(text=text, author=user)
if news.object:
bot.send_message(chat_id=chat_id, text='Отправка...')
news.send_news(photo=photo)
bot.send_message(chat_id=chat_id, text='Новость успшно отправлена',
reply_markup=keyboards.get_main_menu_keyboard())
else:
bot.send_message(chat_id=chat_id, text=bot.get_error_text(), reply_markup=keyboards.get_news_keyboard())
@bot.message_handler(regexp='^Отмена$')
def write_news_cancel(message):
chat_id = message.chat.id
user = core.User(chat_id)
if not user.is_authenticated():
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
return
bot.send_message(chat_id=chat_id, text='Отмена',
reply_markup=keyboards.get_news_keyboard())
@bot.message_handler(regexp='^Удалить последнюю новость$')
def delete_last_news(message):
chat_id = message.chat.id
user = core.User(chat_id)
if not user.is_authenticated():
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
return
last_news = core.TgNews.get_last_news()
if last_news:
bot.send_message(chat_id=chat_id, text=f'Вы уверены что хотите удалить новость:\n'
f'"{last_news}"?',
reply_markup=keyboards.get_inline_confirm_delete_news_keyboard(last_news.id))
else:
bot.send_message(chat_id=chat_id, text='Список новостей пуст',
reply_markup=keyboards.get_news_keyboard())
@bot.callback_query_handler(func=lambda message: 'delete_news' in message.data)
def delete_last_news_confirm(message):
chat_id = message.message.chat.id
message_id = message.message.message_id
data = json.loads(message.data)['delete_news']
bot.delete_message(chat_id=chat_id, message_id=message_id)
if data == 'cancel':
return
bot.send_message(chat_id=chat_id, text='Удаляем...')
delete_news_id = data
core.TgNews.delete_news(news_id=delete_news_id)
bot.send_message(chat_id=chat_id, text='Новость удалена', reply_markup=keyboards.get_main_menu_keyboard())
@bot.message_handler(
content_types=['audio', 'photo', 'voice', 'video', 'document', 'text', 'location', 'contact', 'sticker'])
def invalid_message(message):
"""
Ответ на текст, который бот не понимает.
Функция должна быть последней по порядку!
:return:
"""
chat_id = message.chat.id
user = core.User(chat_id)
if not user.is_authenticated():
msg = bot.send_message(chat_id=chat_id, text=bot.get_register_message())
bot.register_next_step_handler(msg, authorization)
return
bot.send_invalid_message_answer(chat_id=chat_id)