Example #1
0
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')
Example #2
0
class Api:
    def __init__(self, config):
        self.db = DB(config["mongo"])
        self.bot = TeleBot(token=config["token"])
        self.event_loop()

    def event_loop(self):
        updated_ids = set([r.update_id for r in self.bot.get_updates()])
        while True:
            updates = self.bot.get_updates(offset=max(updated_ids))
            for update in updates:
                if update.update_id in updated_ids:
                    continue
                else:
                    updated_ids.add(update.update_id)
                self.process(update.message.json)

    def check_bad_word(self, update):
        update = deepcopy(update)
        chat = update.get("chat")
        chat_id = chat.get("id")
        self.save_chat(chat)
        message_id = update.get("message_id")
        text = update.get("text", "")
        user = update.get("from")
        self.save_user(user)
        tokens = re.split(r"[?,.!\s\n]", text.lower())
        for token in tokens:
            if self.db.get_bad_word(token):
                self.user_report(user.get("id"), token)
                self.bot.send_sticker(chat_id=chat_id,
                                      reply_to_message_id=message_id,
                                      data=self.db.get_shrek())
            break

    def save_user(self, user):
        self.db.insert_user(user)

    def save_chat(self, chat):
        self.db.insert_chat(chat)

    def user_report(self, user_id, token):
        self.db.add_token_to_user(user_id, token)

    def check_register(self, update):
        text = update.get("text")
        chat_id = update.get("chat").get("id")
        user = update.get("from").get("id")
        if "/register" in text:
            tokens = re.split(r"[,.\n\s!]", text)
            tokens = [i for i in tokens if len(i) > 2 and i not in "/register"]
            if not len(tokens) == 1:
                self.bot.send_message(
                    chat_id=chat_id,
                    text="Должно быть только одно слово длины > 2")
            else:
                message = self.db.update_bad_word(tokens[0], user)
                self.bot.send_message(chat_id=chat_id, text=message)

    def check_unregister(self, update):
        pass

    def check_stats(self, update):
        pass

    def process(self, update):
        self.check_bad_word(deepcopy(update))
        self.check_register(deepcopy(update))
        self.check_unregister(deepcopy(update))
        self.check_stats(deepcopy(update))