-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
156 lines (115 loc) · 4.77 KB
/
test.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
from config import config
import asyncio
import logging
from aiogram import Bot, Dispatcher, F
from aiogram.types import Message, WebAppInfo, KeyboardButton, CallbackQuery, InputMediaPhoto
from aiogram.filters import Command
from aiogram.utils.keyboard import ReplyKeyboardBuilder, InlineKeyboardBuilder, InlineKeyboardButton
from aiogram.fsm.context import FSMContext
from aiogram.fsm.strategy import FSMStrategy
import json
TOKEN = config.bot_token.get_secret_value()
CHAT_ID = config.chat_id.get_secret_value()
MODER = config.moder.get_secret_value()
WEB_PREFIX = "https://maxvog2020.github.io/telegram-bot-test/web"
logging.basicConfig(level=logging.INFO)
bot = Bot(token=TOKEN)
dp = Dispatcher(fsm_strategy=FSMStrategy.USER_IN_CHAT)
#########################
async def test_callback(message: Message, values):
data = values['json_data']
name = data['name'].strip()
address = data['address'].strip()
description = data['description'].strip()
contacts = data['contacts'].strip()
telegram = data['telegram']
text = ''
text += f'🆕 Продаётся <b>{name}</b> 🆕\n\n'
text += f'🗺 {address}\n\n'
text += f'ℹ {description}\n\n'
text += f'👤 {contacts}'
if telegram and contacts != "":
text += f', '
if telegram:
text += get_telegram_ref(message)
await send_with_images(CHAT_ID, text, values.get('images'))
await send_with_images(MODER, text + '\n\n\n<b>By</b> ' + get_telegram_ref(message), values.get('images'))
callbacks = {
"test_callback": test_callback,
}
#########################
def get_telegram_ref(message: Message):
return f'<a href="tg://user?id={message.from_user.id}">{message.from_user.full_name}</a>'
async def send_with_images(chat_id, text, images):
if images == [] or images == None:
return await bot.send_message(chat_id, text, parse_mode="HTML")
media = [
InputMediaPhoto(media=images[0].photo[-1].file_id, caption=text, parse_mode="HTML")
]
for i in range(1, len(images)):
media.append(InputMediaPhoto(media=images[i].photo[-1].file_id))
return (await bot.send_media_group(chat_id, media))[0]
async def publish(message: Message, state: FSMContext):
values = await state.get_data()
callback = values.get('callback')
await callback(message, values)
for pic in values.get('images') or []:
await pic.delete()
if values.get('to_delete') != None:
await values.get('to_delete').delete()
await state.clear()
message = await message.answer('Опубликовано!')
await asyncio.sleep(3)
await message.delete()
@dp.message(F.photo)
async def on_get_photo(message: Message, state: FSMContext):
values = await state.get_data()
image_count = int(values.get('image_count')) or 0
images = values.get('images') or []
if image_count == 0:
await message.delete()
return
else:
images.append(message)
image_count -= 1
await state.update_data(images=images, image_count=image_count)
if image_count == 0:
await publish(message, state)
@dp.message(F.web_app_data)
async def on_get_data(message: Message, state: FSMContext):
data = message.web_app_data.data
json_data = json.loads(data)
callback = callbacks[json_data['callback']]
image_count = int(json_data['image_count'])
await message.delete()
await state.update_data(callback=callback, image_count=image_count, json_data=json_data)
if image_count == 0:
await publish(message, state)
else:
to_delete = await message.answer(f"Приложите фотографии ({image_count} шт.)")
await state.update_data(to_delete=to_delete)
@dp.callback_query()
async def on_callbacks(callback: CallbackQuery, state: FSMContext):
url = WEB_PREFIX + callback.data
markup = ReplyKeyboardBuilder()
markup.add(KeyboardButton(text="Перейти в форму", web_app=WebAppInfo(url=url)))
message = await callback.message.answer(text="Нажмите на кнопку для перехода в форму", reply_markup=markup.as_markup())
await callback.answer()
await asyncio.sleep(3)
await message.delete()
@dp.message(Command("start"))
async def on_start(message: Message):
markup = InlineKeyboardBuilder()
markup.add(InlineKeyboardButton(text="Создать объявление", callback_data="/"))
await message.answer("<b>➡️ Меню ⬅️</b>", reply_markup=markup.as_markup(), parse_mode="HTML")
await message.delete()
@dp.message()
async def delete_everything_else(message: Message):
await message.delete()
#########################
async def main():
await dp.start_polling(bot)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()