-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
391 lines (357 loc) · 22.7 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# Test bot of @memvbot - @without_name_bot in telegram
import logging
import config
from database import database
import sys
import time
from config import BOT_TOKEN, BOT_OWNER, CHANNEL_ID
import aiogram.dispatcher
from aiogram import Bot, Dispatcher, executor, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher.filters.state import State, StatesGroup
# Configure logging
logging.basicConfig(level=logging.INFO)
storage = MemoryStorage()
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher(bot, storage=storage)
count = 1
sql = database.Database(user=config.PG_USER,
database=config.PG_DATABASE,
host=config.PG_HOST,
password=config.PG_PASSWORD)
class Answer_bot(StatesGroup):
reply_text = State()
reply_id = State()
# defines
def advertising():
global count
print(f'1 {count}')
if count == 5:
print(f'2 {count}')
count = 0
caption = sql.one('SELECT text, url, count, id FROM advertising ORDER BY RANDOM() LIMIT 1')
sql.commit(f"UPDATE advertising SET count='{caption[2] - 1}' WHERE id='{caption[3]}'")
caption = f'[{caption[0]}]({caption[1]})'
else:
print(f'3 {count}')
caption = None
return caption
def answer_404(user_id):
answer = sql.one('SELECT text, url, count, id FROM advertising ORDER BY RANDOM() LIMIT 1')
sql.commit(f"UPDATE users SET used='"
f"{int(sql.one(f'SELECT used FROM users WHERE id = {user_id}')[0]) + 1}"
f"' WHERE id='{user_id}'")
sql.commit(f"UPDATE advertising SET count='{int(answer[2]) - 1}' WHERE id='{answer[3]}'")
answer = f'[{answer[0]}]({answer[1]})'
return answer
@dp.message_handler(commands=['start'])
async def command_start(message: types.Message):
try:
if message.from_user.language_code == 'ru':
await bot.send_message(message.chat.id,
f'Привет {message.from_user.full_name}, я meme voice bot! Я могу отправлять мемные'
f'голосовые в групповые чаты или даже в личные сообщения!'
f'\nКак? Узнай через /help'
f'\n'
f'\n[Новости о боте можно найти тут](https://t.me/joinchat/AAAAAEsekoTUW0WjerW8wA)',
parse_mode='Markdown')
else:
await bot.send_message(message.chat.id, f'Hello {message.from_user.full_name}, i\`m Meme Voice bot!'
f'I can send voice with memes in group, channel or private message.'
f'\nHow? Use /help'
f'\n'
f'\n[News about bot you can find here]('
f'https://t.me/joinchat/AAAAAEsekoTUW0WjerW8wA)',
parse_mode='Markdown')
if sql.one("SELECT * FROM users WHERE CAST(id AS TEXT) LIKE '{}';".format(message.from_user.id)) is None:
sql.commit(
"INSERT INTO users VALUES('{}','{}','{}','{}','{}','{}','{}','','','','','','','','','','','','',"
"'','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','',"
"'','','','','','','')".format(message.from_user.id,
message.from_user.first_name,
time.strftime("%F %T", time.localtime()),
0, None, '', 'default'))
except Exception as error:
tb = sys.exc_info()[2]
print(error,
'\nat line {}'.format(tb.tb_lineno))
@dp.message_handler(commands=['help'])
async def command_help(message: types.Message):
try:
file_id: str = "CgACAgIAAxkBAAIFgGCkDZuiIAYd9OaUb8spBBGZ2XYbAAJ_CgACOvvwS2TKVs16MCByHwQ"
if message.from_user.language_code == 'ru':
await bot.send_message(message.chat.id,
'Как пользоваться ботом?'
'\n'
'\nНачните писать @memvbot в любом чате, и у вас появится список с мемами. '
'\nПример:'
'\n@memvbot привет'
'\n'
'\n_Почему в появившемся списке нет мемов, которые есть на канале!?_'
'\nВ телеграма есть ограничение на вывод строк в inline режиме, поэтому их '
'там всего 50. По этому было решено добавить теги (текст), по которым можно будет '
'найти мем. Не знаю, как можно было ещё проще сделать, но если есть идеи или '
'предложение, [пишите сюда.](https://t.me/deesiigneer)', parse_mode='Markdown')
await bot.send_animation(message.chat.id, animation=file_id, caption='Наглядный пример')
else:
await bot.send_message(message.chat.id,
"How to use the bot? (How to use) "
"\n"
"\nStart writing @memvbot in any chat and you will have a list with memes. "
"\nExample:"
"\n@memvbot hello"
"\n"
"\n_Why are there no memes in the list that are on the channel !? _"
"\nThe telegram has a restriction on the output of lines in inline mode, so their"
"there are only 50. Therefore, it was decided to add tags (text), by which it will "
"be possible find the meme. I don't know how it could have been made even easier,"
" but if there are ideas or suggestion, [write here.](https://t.me/deesiigneer)",
parse_mode='Markdown')
await bot.send_animation(message.chat.id, animation=file_id, caption='Illustrative example')
except Exception as error:
tb = sys.exc_info()[2]
print(error,
'\nat line {}'.format(tb.tb_lineno))
@dp.message_handler(commands=['price'])
async def command_price(message: types.Message):
try:
if message.from_user.language_code == 'ru':
await bot.send_message(message.chat.id,
f'Привет _{message.from_user.full_name}_, хочешь купить рекламу?'
f'\nПиши создателю - @deesiigneer '
f'\n'
f'\n🎙Реклама в войсах от *100* показов - *{config.IN_VOICES}₽*'
f'\n_Дополнительные_ *100* _просмотров - _*{config.ADDITIONAL}₽*'
f'\n_Разовое приобретение_ *1000* _просмотров - _*55₽*'
f'\n'
f'\n👥Реклама в [группе](https://t.me/joinchat/Sx6ShNRbRaN6tbzA) - 500₽'
f'\n_публикуется на_ *24ч* _с момента публикации_'
f'\n'
f'\n📑*УСЛОВИЯ РАЗМЕЩЕНИЯ:*'
f'\n1️⃣ - Реклама в *войсах* принимается в ввиде *"текст - ссылка"*'
f' пример на картинке ниже '
f'\n2️⃣ - *Не более* _1024_ символов в тексте для рекламы в войсах'
f'\n3️⃣ - Статистика от рекламы в войсах предоставляется по истечению показов '
f'или в режиме онлайн по запросу (но не чаще, чем один раз в сутки).'
f'/ Статистика публикации в группе предоставляется по запросу '
f'*НЕ РАНЕЕ 7 ДНЕЙ* с момента публикации',
parse_mode='Markdown', disable_web_page_preview=True)
else:
await bot.send_message(message.chat.id, f'Hi')
except Exception as error:
tb = sys.exc_info()[2]
print(error,
'\nat line {}'.format(tb.tb_lineno))
# админпанелька
@dp.message_handler(commands=['admintool'])
async def command_admintool(message: types.Message):
try:
if message.from_user.id == BOT_OWNER:
adminkeyboard = types.InlineKeyboardMarkup(row_width=2)
statistics = types.InlineKeyboardButton(text="Статистика", callback_data="statistics")
sql_database = types.InlineKeyboardButton(text="База данных", callback_data="sql_database")
adminkeyboard.add(statistics, sql_database)
await bot.send_message(BOT_OWNER, f'Выбери что желаешь, {message.from_user.full_name}',
reply_markup=adminkeyboard)
except Exception as error:
tb = sys.exc_info()[2]
print(error,
'\nat line {}'.format(tb.tb_lineno))
@dp.inline_handler()
async def chosen_handler(query: types.InlineQuery):
try:
global count
print(query)
if sql.one("SELECT * FROM memevoices WHERE CAST(tag AS TEXT) LIKE '%{}%'".format(query.query)) is None:
not_found = types.InlineQueryResultArticle(id='404',
title="404",
description="Такого войса нет...",
thumb_url=f"https://cdn.frankerfacez.com/emoticon/454732/2",
thumb_width=120,
thumb_height=120,
input_message_content=types.InputTextMessageContent(
message_text=answer_404(query.from_user.id),
parse_mode='Markdown',
disable_web_page_preview=True)
)
await bot.answer_inline_query(query.id, [not_found], cache_time=0)
else:
sql_ = list(sql.many("SELECT * FROM memevoices WHERE CAST(tag AS TEXT) LIKE '%{}%'".format(query.query),
50)
)
cap = advertising()
if query.from_user.id != BOT_OWNER:
results = [types.InlineQueryResultCachedVoice(id=item[0], voice_file_id=item[1], title=item[2],
caption=cap, parse_mode='Markdown')
for item in sql_
]
else:
results = [types.InlineQueryResultCachedVoice(id=item[0], voice_file_id=item[1], title=item[2],
caption=item[2], parse_mode='Markdown')
for item in sql_
]
await query.answer(results, cache_time=0, is_personal=True)
count = count + 1
except Exception as error:
tb = sys.exc_info()[2]
print(error,
'\nat line {}'.format(tb.tb_lineno))
@dp.chosen_inline_handler()
async def chosen(result: types.ChosenInlineResult):
await bot.send_message(config.LOG_CHANNEL_ID,
f"{result}\n\n[{result.from_user.full_name}](tg://user?id={result.from_user.id})",
parse_mode='Markdown')
print(f'result {result}')
@dp.message_handler(content_types=types.ContentType.VOICE)
async def add_audio(message: types.Message):
try:
if message.from_user.id == BOT_OWNER:
if message.voice.file_id != sql.one(
f"SELECT audio_file_id FROM memevoices WHERE audio_file_id = '{message.voice.file_id}'"):
if message.caption is not None:
sql.commit(
f"INSERT INTO memevoices VALUES(DEFAULT, '{message.voice.file_id}', '{message.caption}', '0')")
await bot.send_voice(chat_id=CHANNEL_ID, voice=message.voice.file_id,
caption=f"<code>{message.caption}</code>", parse_mode='HTML')
await message.reply(text="Успешно добавлен!")
else:
await message.reply(text="Войс нужно отправлять с названием!\nПопробуй ещё раз.")
else:
await message.reply(text="Этот войс уже есть в БД!")
except Exception as error:
tb = sys.exc_info()[2]
print(error, '\nat line {}'.format(tb.tb_lineno))
@dp.message_handler(content_types=types.ContentType.ANY)
async def big_eye(message: types.Message):
try:
for m in message:
print(str(m))
if message.from_user.id != BOT_OWNER:
keyboard = types.InlineKeyboardMarkup(row_width=2)
answerbybot = types.InlineKeyboardButton(text="Ответить через бота", callback_data="answerbybot")
keyboard.add(answerbybot)
primary = str(f'Details:'
f'\n └ Message ID: {message.message_id}'
f'\n\tFrom:'
f'\n ├ <a href="tg://user?id={message.from_user.id}">{message.from_user.id}</a>'
f'\n ├ is bot: {message.from_user.is_bot}'
f'\n ├ Fullname: {message.from_user.first_name}'
f'\n ├ Lastname: {message.from_user.last_name}'
f'\n ├ Username: {message.from_user.username}'
f'\n └ Language code: {message.from_user.language_code}'
f'\n\tChat:'
f'\n ├ <a href="tg://user?id={message.chat.id}">{message.chat.id}</a>'
f'\n ├ type: {message.chat.type}'
f'\n ├ title: {message.chat.title}'
f'\n ├ username: {message.chat.username}'
f'\n ├ First name: {message.chat.first_name}'
f'\n ├ Last name: {message.chat.last_name}'
f'\n └ Date: {message.date}'
)
if message.is_forward():
if message.forward_from_chat is None:
if message.forward_from is None:
primary = primary + str(f'\n\tForward from:'
f'\n ├ ID: <code>unknown</code></a>'
f'\n ├ First name: {message.forward_sender_name}'
f'\n └ Forward date: {message.forward_date}')
primary = primary + str(f'\n\tForward from:'
f'\n ├ ID: <a href="tg://user?id={message.forward_from.id}">'
f'{message.forward_from.id}</a>'
f'\n ├ First name: {message.forward_from.first_name}'
f'\n ├ Is bot: {message.forward_from.is_bot}'
f'\n ├ Last name: {message.forward_from.last_name}'
f'\n ├ Username: {message.forward_from.username}'
f'\n ├ Language code: {message.forward_from.language_code}'
f'\n └ Forward date: {message.forward_date}')
elif message.forward_from_chat is not None:
primary = primary + str(f'\n\tForward from chat:'
f'\n ├ ID: <code>{message.forward_from_chat.id}</code>'
f'\n ├ Title: {message.forward_from_chat.title}'
f'\n ├ Username: {message.forward_from_chat.username}'
f'\n ├ Type: {message.forward_from_chat.type}'
f'\n ├ Forward from message id: {message.forward_from_message_id}'
f'\n └ Forward date: {message.forward_date}'
)
# if message.entities is not None: # TODO: сделать проверку на ссылки
# print(f'message.entities: {message.entities}')
# global entitie
# for entities in message.entities:
# entitie = entities
# print(f'Entit: {entitie}')
# primary = primary + str(f'\nEntities'
# f'\n ├ Type: {entitie.type}'
# f'\n ├ Offset: {entitie.offset}'
# f'\n ├ Length: {entitie.length}'
# )
# if entitie.type == 'text_link':
# primary = primary + str(f'\n └ Url: {entitie.url}')
# elif entitie.type == 'text_mention':
# primary = primary + str(f'\nEntities'
# f'\n └ User: {entitie.user}')
if message.caption is not None:
primary = primary + str(f'\n └ Caption: {message.caption}')
if message.text is not None:
primary = primary + str(f'\n └ Text: {message.text}')
await bot.send_message(BOT_OWNER, primary, parse_mode='HTML', reply_markup=keyboard)
if message.content_type == 'audio':
await bot.send_audio(BOT_OWNER, message.audio.file_id, caption=primary, parse_mode='HTML',
reply_markup=keyboard)
if message.content_type == 'document':
await bot.send_document(BOT_OWNER, message.document.file_id, caption=primary, parse_mode='HTML',
reply_markup=keyboard)
if message.content_type == 'photo':
await bot.send_photo(BOT_OWNER, message.photo[-1].file_id, caption=primary, parse_mode='HTML',
reply_markup=keyboard)
if message.content_type == 'sticker':
await bot.send_message(BOT_OWNER, primary, parse_mode='HTML')
await bot.send_sticker(BOT_OWNER, message.sticker.file_id, reply_markup=keyboard,
reply_to_message_id=message.message_id + 1)
if message.content_type == 'video':
await bot.send_video(BOT_OWNER, message.video.file_id, parse_mode='HTML', caption=primary,
reply_markup=keyboard)
if message.content_type == 'animation':
print(f'animation type is = {type(message.animation.file_id)}')
await bot.send_animation(BOT_OWNER, message.animation.file_id, caption=primary, parse_mode='HTML',
reply_markup=keyboard)
if message.content_type == 'voice':
if message.from_user.id != BOT_OWNER:
await bot.send_voice(BOT_OWNER, message.voice.file_id, caption=primary, parse_mode='HTML',
reply_markup=keyboard)
if message.content_type == 'video_note':
await bot.send_message(BOT_OWNER, primary, parse_mode='HTML')
await bot.send_video_note(BOT_OWNER, message.video_note.file_id, reply_markup=keyboard,
reply_to_message_id=message.message_id + 1)
# elif types.message.ContentType.GAME:
# await bot.send_game()
# elif types.message.ContentType.NEW_CHAT_MEMBERS:
# await bot.send_audio(BOT_OWNER, message.audio)
# elif types.message.ContentType.LEFT_CHAT_MEMBER:
# await bot.send_audio(BOT_OWNER, message.audio)
# elif types.message.ContentType.INVOICE:
# await bot.send_audio(BOT_OWNER, message.audio)
# elif types.message.ContentType.SUCCESSFUL_PAYMENT:
# await bot.send_audio(BOT_OWNER, message.audio)
# elif types.message.ContentType.UNKNOWN:
# await bot.send_audio(BOT_OWNER, message.audio)
except Exception as error:
tb = sys.exc_info()[2]
print(error, '\nat line {}'.format(tb.tb_lineno))
@dp.callback_query_handler(text="answerbybot")
async def detailed(call: types.CallbackQuery, state: aiogram.dispatcher.FSMContext):
reply_user_id = int()
for entity in call.message.entities:
reply_user_id = entity.user.id
await Answer_bot.reply_text.set()
await state.update_data(reply_id=int(reply_user_id))
await bot.send_message(call.message.chat.id,
f'Жду сообщения для ответа [{entity.user.full_name}](tg://user?id={call.message.from_user.id}) ...',
parse_mode='Markdown')
@dp.message_handler(state=Answer_bot.reply_text)
async def answer_by_bot(message: types.Message, state: aiogram.dispatcher.FSMContext):
async with state.proxy() as data:
data['reply_text'] = message.text
await bot.send_message(chat_id=data['reply_id'], text=data['reply_text'])
await state.finish()
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)