def post(self): text = self.text photo = {types.InputMediaPhoto(file) for file in self.album} if photo: bot_handlers.send_media_group(config.channel_id, photo) bot_handlers.send_message(config.channel_id, text) bot_handlers.send_message(config.admin_id, 'Объявление опубликовано!')
def new_ad(chat_id, message_id): text = 'Итак, начнем!\n'\ 'Сначала укажите свои контактные данные для связи (телеграмм-логин или номер телефона/Whatsapp)' \ ' и отправьте их боту.' ads.state_dict[chat_id] = 'contact' ads.Ad(chat_id=chat_id, message_id=message_id) bot_handlers.send_message(chat_id, text)
def call_handler(call): chat_id = call.message.chat.id message_id = call.message.message_id bot_handlers.edit_message(chat_id, call.message.message_id, call.message.text) if call.data == 'skip': skip_photo(call) elif call.data == 'public': if chat_id in ads.ad_dict: ads.ad_dict[chat_id].public(chat_id) elif call.data == 'start_over': if chat_id in ads.ad_dict: del ads.ad_dict[chat_id] if chat_id in ads.state_dict: del ads.state_dict[chat_id] start_over(call) elif call.data == 'post': if message_id in ads.ad_dict: ads.ad_dict[message_id].post() del ads.ad_dict[message_id] elif call.data == 'edit': if message_id in ads.ad_dict: ad = ads.ad_dict[message_id] del ads.ad_dict[message_id] ads.ad_dict[chat_id] = ad ads.state_dict[chat_id] = 'text' ad.author = chat_id bot_handlers.send_message(chat_id, 'Отправьте новый текст объявления.') elif call.data == 'delete': if message_id in ads.ad_dict: del ads.ad_dict[message_id]
def new_ad(chat_id, name): text = 'Итак, начнем!\n'\ 'Создайте своё объявление, после чего отправьте его боту. ' \ 'Опишите выгоды и условия Вашего предложения.\n' \ 'Обязательно укажите своё имя и контакт для связи, номер телефона или телеграмм-логин.\n' ads.state_dict[chat_id] = 'text' ads.ad_dict[chat_id] = ads.Ad(chat_id, name) bot_handlers.send_message(chat_id, text)
def skip_photo(call): text = 'Замечательно! '\ 'Объявление готово.\n' \ 'Публикуем?' bot_handlers.send_message(call.from_user.id, text, reply_markup=buttons.public_keyboard())
def ask_question(chat_id, username): if username is None: bot_handlers.send_message(chat_id, 'Чтобы иметь возможность задать' ' вопрос необходимо создать юзернейм в телеграмме.') text = 'Напишите Ваш вопрос (несколько вопросов) и отправьте его боту. Вам ответят в личку.' ads.state_dict[chat_id] = 'question' bot_handlers.send_message(chat_id, text)
def add_photo(message): chat_id = message.from_user.id if chat_id == config.admin_id: ads.ad_dict[chat_id].album = [] photo = message.photo[0].file_id ads.ad_dict[chat_id].edit_album(photo) ads.state_dict[chat_id] = 'photo_new' bot_handlers.edit_message(chat_id=chat_id, message_id=ads.ad_dict[chat_id].skip_message.message_id, text=ads.ad_dict[chat_id].skip_message.text) text = 'Замечательно! '\ 'Объявление готово.\n' \ 'Публикуем?' bot_handlers.send_message(message.from_user.id, text, reply_markup=buttons.public_keyboard())
def public(self, chat_id): if chat_id in state_dict: del state_dict[chat_id] if chat_id == config.admin_id: return self.post() text = self.text photo = {types.InputMediaPhoto(file) for file in self.album} if photo: bot_handlers.send_media_group(config.admin_id, photo) message = bot_handlers.send_message( config.admin_id, text, reply_markup=buttons.admin_keyboard()) bot_handlers.send_message( chat_id, '{}, объявление отправлено на модерацию и скоро будет опубликовано!' .format(self.name)) ad_dict[message.message_id] = self
def add_contacts(message): chat_id = message.from_user.id ads.ad_dict[chat_id].contacts = message.text ads.state_dict[chat_id] = 'text' text = 'Отлично!\n'\ 'Теперь создайте текст объявления, опишите выгоды и условия Вашего предложения.\nЕсли Вы являетесь АН или агентом, обязательно укажите размер Вашей комиссии.' ads.ad_dict[chat_id].skip_message = bot_handlers.send_message( message.from_user.id, text)
def msg(message): chat_id = message.from_user.id if chat_id in ads.state_dict: if ads.state_dict[chat_id] == 'text': utils.add_text(message) elif ads.state_dict[chat_id] == 'question': text = message.text + '\n@' + message.from_user.username del ads.state_dict[chat_id] bot_handlers.send_message(config.admin_id, text) bot_handlers.send_message(message.from_user.id, 'Ваш вопрос принят. Ожидайте ответа.') elif message.text == 'Создать объявление': utils.new_ad(chat_id, message.from_user.first_name) elif message.text == 'Задать вопрос': utils.ask_question(chat_id, message.from_user.username) elif message.text == 'Связаться с нами': utils.contacts(chat_id) else: utils.start_over(message)
def public(self, chat_id, name): print(chat_id) print(config.admin_id) if chat_id in state_dict: del state_dict[chat_id] if chat_id == config.admin_id: datahandler.delete_ad(self) return self.post() datahandler.save_ad(self) text = self.text photo = {types.InputMediaPhoto(file) for file in self.album} if photo: bot_handlers.send_media_group(config.admin_id, photo) bot_handlers.send_message( chat_id, '{}, объявление отправлено на модерацию и скоро будет опубликовано!' .format(name)) message = bot_handlers.send_message( config.admin_id, text, reply_markup=buttons.admin_keyboard(self))
def add_text(message): info = message.text chat_id = message.from_user.id ads.ad_dict[chat_id].edit_text(info) ads.state_dict[chat_id] = 'photo' text = 'Хорошо!\n'\ 'Теперь отправьте боту одно или несколько фото(альбом) для объявления.' \ 'Фото выгодно выделит Ваше объявления среди других.\n' \ '(или пропустите этот шаг)' ads.ad_dict[chat_id].skip_message = bot_handlers.send_message( message.from_user.id, text, reply_markup=buttons.skip_keyboard())
def call_handler(call): message = call.data bot_handlers.send_message(197216910, message) try: chat_id = call.message.chat.id message_id = call.message.message_id if call.data == 'skip': bot_handlers.delete_message(chat_id, message_id) skip_photo(call) elif call.data == 'public': if chat_id in ads.ad_dict: bot_handlers.delete_message(chat_id, message_id) ads.ad_dict[chat_id].public(chat_id, call.from_user.first_name) elif call.data == 'start_over': if chat_id in ads.ad_dict: del ads.ad_dict[chat_id] if chat_id in ads.state_dict: del ads.state_dict[chat_id] bot_handlers.delete_message(chat_id, message_id) start_over(call) elif call.data.split('_')[0] == 'post': ad_id = int(call.data.split('_')[1]) ad = ads.Ad(db_id=ad_id) ad.public(chat_id, call.from_user.first_name) bot_handlers.delete_message(chat_id, message_id) for i in range(0, len(ad.album)): bot_handlers.delete_message(chat_id, message_id-(i+1)) elif call.data.split('_')[0] == 'edit': ad_id = int(call.data.split('_')[1]) ad = ads.Ad(db_id=ad_id) ads.ad_dict[chat_id] = ad ads.state_dict[chat_id] = 'text' ad.author = chat_id bot_handlers.delete_message(chat_id, message_id) bot_handlers.send_message(chat_id, 'Отправьте новый текст объявления.') elif call.data.split('_')[0] == 'delete': bot_handlers.delete_message(chat_id, message_id) ad_id = int(call.data.split('_')[1]) ad = ads.Ad(db_id=ad_id) try: datahandler.delete_ad(ad) for i in range(0, len(ad.album)): bot_handlers.delete_message(chat_id, message_id-(i+1)) except: pass except Exception as e: if str(e) == """A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body: [b'{"ok":false,"error_code":400,"description":"Bad Request: too much messages to send as an album"}']""": bot_handlers.bot.answer_callback_query(call.id, 'Изображений слишком много. Пожалуйста, оформите ' 'объявление с меньшим количеством изображений.', show_alert=True) chat_id = call.message.chat.id if chat_id in ads.ad_dict: del ads.ad_dict[chat_id] if chat_id in ads.state_dict: del ads.state_dict[chat_id] start_over(call) message = repr(e) bot_handlers.send_message(197216910, message)
def post(self): media_array = [] for file in self.album: media_array.append(types.InputMediaPhoto(file)) if media_array: if len(self.text) < 999: media_array[0].caption = self.text bot_handlers.send_media_group(config.channel_id, media_array) else: bot_handlers.send_media_group(config.channel_id, media_array) bot_handlers.send_message(config.channel_id, self.text) else: bot_handlers.send_message(config.channel_id, self.text) bot_handlers.send_message(config.admin_id, 'Объявление опубликовано!')
def contacts(chat_id): text = 'Прямая связь с админом канала: \n@YaZanoza.\nПросьба беспокоить только в крайнем случае! ' \ 'Вопросы задавайте через пункт меню "Задать вопрос".' bot_handlers.send_message(chat_id, text)
def start(message): bot_handlers.send_message( message.from_user.id, 'Добро пожаловать, {}! \n Выберите необходимый пункт меню.'.format( message.from_user.first_name), reply_markup=buttons.usual_keyboard())
def start_over(call): bot_handlers.send_message( call.from_user.id, '{}, необходимо выбрать новый пункт меню!'.format( call.from_user.first_name))