class PostSender: def __init__(self, token: str): self.__bot = TeleBot(token) self.__api_worker = ApiWorker() def get_categories(self) -> List[Category]: return self.__api_worker.get_categories() def get_posts(self, category: Union[Category, str], limit: int = 1, likes: int = 1): if isinstance(category, Category): category = category.channel_id return self.__api_worker.get_items(category, limit=limit, likes=likes) def get_recomendation(self, limit=1, likes=1): return self.__api_worker.get_recomendations(limit=limit, likes=likes) def __send_post(self, chat_id: Union[str, int], post_item: PostItem) -> bool: if post_item: try: if post_item.type in ('pic', 'mem'): self.__bot.send_photo(chat_id, photo=post_item.url, caption=post_item.title) elif post_item.type == 'gif_caption': self.__bot.send_animation(chat_id, animation=post_item.url, caption=post_item.title) elif post_item.type == 'video_clip': self.__bot.send_video(chat_id, data=post_item.url, caption=post_item.title) except ApiTelegramException as e: logging.error(f"Bot can't send message error_code: {e.error_code} - {e.result_json['description']}") if e.error_code == 429: print('timeout ', e.result_json['parameters']['retry_after']) time.sleep(e.result_json['parameters']['retry_after']) return self.__send_post(chat_id, post_item) raise return True def _check_chat(self, chat_id) -> bool: try: self.__bot.get_chat(chat_id) return True except ApiTelegramException as e: logging.error(f'bad chat {chat_id}: {e.result_json["description"]}') raise def publish_post(self, chat_id: Union[int, str], post_items: Union[PostItem, List[PostItem]]) -> Union[ bool, List[bool]]: if self._check_chat(chat_id): if isinstance(post_items, PostItem): return self.__send_post(chat_id, post_items) else: for it in post_items: self.__send_post(chat_id, it) time.sleep(1)
def send(self, bot: TeleBot, group): msg: Message if self.type == 'text': msg = bot.send_message(group, self.text) elif self.type == 'photo': msg = bot.send_photo(group, self.photo_id, self.caption) elif self.type == 'video': msg = bot.send_video(group, self.video_id, caption=self.caption) elif self.type == 'document': msg = bot.send_document(group, self.doc_id, caption=self.caption) else: print('А где содержимое письма?!') return msg
def copy_message(bot: telebot.TeleBot, msg: telebot.types.Message, chat_ids: list, disable_notification=False, keyboard=None): last_id = chat_ids[-1] if len(chat_ids) > 1: chat_ids = chat_ids[:-1] else: chat_ids = [] if msg.content_type == 'text': text = check_msg_entities(msg.entities, msg.html_text) for chat_id in chat_ids: try: bot.send_message(chat_id, text=text, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard ) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.send_message(last_id, text=text, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard ) else: caption = check_msg_entities(msg.entities, msg.html_caption) if msg.content_type == 'photo': size = msg.photo[-1] for chat_id in chat_ids: try: bot.send_photo(chat_id, photo=size.file_id, caption=caption, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.send_photo(last_id, photo=size.file_id, caption=caption, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard) elif msg.content_type == 'audio': for chat_id in chat_ids: try: bot.send_audio(chat_id, audio=msg.audio.file_id, caption=caption, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.send_audio(last_id, audio=msg.audio.file_id, caption=caption, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard) elif msg.content_type == 'document': for chat_id in chat_ids: try: bot.send_document(chat_id, data=msg.document.file_id, caption=caption, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.send_document(last_id, data=msg.document.file_id, caption=caption, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard) elif msg.content_type == 'sticker': for chat_id in chat_ids: try: bot.send_sticker(chat_id, data=msg.sticker.file_id, disable_notification=disable_notification, reply_markup=keyboard) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.send_sticker(last_id, data=msg.sticker.file_id, disable_notification=disable_notification, reply_markup=keyboard) elif msg.content_type == 'video': for chat_id in chat_ids: try: bot.send_video(chat_id, data=msg.video.file_id, caption=caption, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.send_video(last_id, data=msg.video.file_id, caption=caption, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard) elif msg.content_type == 'animation': for chat_id in chat_ids: try: bot.send_animation(chat_id, animation=msg.animation.file_id, caption=caption, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.send_animation(last_id, animation=msg.animation.file_id, caption=caption, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard) elif msg.content_type == 'voice': for chat_id in chat_ids: try: bot.send_voice(chat_id, voice=msg.voice.file_id, caption=caption, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.send_voice(last_id, voice=msg.voice.file_id, caption=caption, parse_mode='html', disable_notification=disable_notification, reply_markup=keyboard) elif msg.content_type == 'video_note': for chat_id in chat_ids: try: bot.send_video_note(chat_id, data=msg.video_note.file_id, disable_notification=disable_notification, reply_markup=keyboard) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.send_video_note(last_id, data=msg.video_note.file_id, disable_notification=disable_notification, reply_markup=keyboard) elif msg.content_type == 'contact': for chat_id in chat_ids: try: bot.send_contact(chat_id, phone_number=msg.contact.phone_number, first_name=msg.contact.first_name, last_name=msg.contact.last_name or '', disable_notification=disable_notification, reply_markup=keyboard) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.send_contact(last_id, phone_number=msg.contact.phone_number, first_name=msg.contact.first_name, last_name=msg.contact.last_name or '', disable_notification=disable_notification, reply_markup=keyboard) elif msg.content_type == 'location': for chat_id in chat_ids: try: bot.send_location(chat_id, latitude=msg.location.latitude, longitude=msg.location.longitude, disable_notification=disable_notification, reply_markup=keyboard) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.send_location(last_id, latitude=msg.location.latitude, longitude=msg.location.longitude, disable_notification=disable_notification, reply_markup=keyboard) elif msg.content_type == 'venue': for chat_id in chat_ids: try: bot.send_venue(chat_id, latitude=msg.venue.location.latitude, longitude=msg.venue.location.longitude, title=msg.venue.title, address=msg.venue.address, foursquare_id=msg.venue.foursquare_id, disable_notification=disable_notification, reply_markup=keyboard) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.send_venue(last_id, latitude=msg.venue.location.latitude, longitude=msg.venue.location.longitude, title=msg.venue.title, address=msg.venue.address, foursquare_id=msg.venue.foursquare_id, disable_notification=disable_notification, reply_markup=keyboard) elif msg.content_type == 'poll': for chat_id in chat_ids: try: bot.forward_message(chat_id, msg.chat.id, msg.message_id) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.forward_message(last_id, msg.chat.id, msg.message_id) elif msg.content_type == 'game': for chat_id in chat_ids: try: bot.forward_message(chat_id, msg.chat.id, msg.message_id) except telebot.apihelper.ApiException as e: send_error(bot, e) continue sleep(0.5) return bot.forward_message(last_id, msg.chat.id, msg.message_id) raise ValueError('Can\'t copy this message')
from telebot import TeleBot bot = TeleBot('token') vlad_video = open('app/static/py.MOV', 'rb') caption = 'Приглашение на онлайн мастер-класс. Такой проект вы сможете создать вместе с ProBox. Интересно? Ждем вас сегодня' #bot.send_video(164898079, vlad_video, caption='Приглашение на онлайн мастер-класс. Такой проект вы сможете создать вместе с ProBox. Интересно? Ждем вас сегодня') users = [] print('Кол-во пользователей: ', len(users)) sent = 0 failed = 0 for user in users: try: with open('app/static/py.MOV', 'rb') as vid: bot.send_video(user, vid, caption='') sent += 1 except Exception as e: print(e) failed += 1 print('Видео отправлено кол-ву человек: {}\nУдалили бот кол-во человек: {}'. format(sent, failed))