def proceed_new_members(update, callback): bot = callback.bot members = update.message.new_chat_members logger.info(members) if len(members) == 0: return chat_id = update.message.chat_id API.delete_message(bot, chat_id, update.message.message_id) with dataWorker.session_scope() as session: chat: GroupStatus = session.query(GroupStatus).get(chat_id) reply_template = chat.wel_message if chat is None or reply_template is None: return for member in members: reply = reply_template.replace("""{$name}""", f"""<a href="tg://user?id={member.id}">{member.first_name}</a>""") kwargs = {"chat_id": chat_id, "text": reply, "disable_web_page_preview": True} if not chat.new_users_blocked or not can_restrict_users(bot, update): message = send_wel_message(bot, kwargs) time.sleep(30) if can_delete_messages(bot, update): API.delete_message(bot, chat_id=message.chat_id, message_id=message.message_id) else: unlock_button = telegram.InlineKeyboardButton("Я прочитал", callback_data="applied " + str(member.id)) reply_markup = InlineKeyboardMarkup(build_menu([unlock_button], n_cols=1)) kwargs["reply_markup"] = reply_markup msg = send_wel_message(bot, kwargs) chat.add_muted(member.id, msg.message_id) bot.restrict_chat_member(chat_id, member.id, telegram.ChatPermissions())
def _execute(self, bot, update, txt, method, **kwargs): with self.dbWorker.session_scope() as session: mdl: GroupStatus = session.query(GroupStatus).get( update.message.chat_id) if mdl is None: mdl = GroupStatus() mdl.id = update.message.chat_id session.add(mdl) reply_user_id = None if update.message.reply_to_message is not None \ and not is_user_admin(bot, update, update.message.reply_to_message.from_user): reply_user_id = update.message.reply_to_message.from_user.id banned_user = list( filter(lambda x: x.user_id == reply_user_id, mdl.banned_users)) if len(banned_user) > 0: user = banned_user[0] else: user = BannedUser() user.user_id = reply_user_id user.reason = txt user.username = update.message.reply_to_message.from_user.username mdl.banned_users.append(user) API.use_custom_api_method(method, chat_id=update.message.chat_id, user_id=reply_user_id, **kwargs) if can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id)
def proceed_non_text_message(update, callback): bot = callback.bot proceed_new_members(update, callback) if is_need_delete(update.message.chat_id) and \ not is_user_admin(bot, update) and \ can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id) return False return True
def kick_user(user, bot, session): if not can_delete_messages(bot, None, user.chat_id) or \ not can_restrict_users(bot, None, user.chat_id): return chat_member = API.get_chat_member(bot, user.chat_id, user.user_id) if chat_member.is_member: API.kick_chat_member(bot, user.chat_id, user.user_id, until_date=datetime.datetime.utcnow() + datetime.timedelta( seconds=60)) delete_welcome_message(user, bot) session.delete(user)
def execute(self, bot, update, txt): with self.dbWorker.session_scope() as session: elem = self.parse_command(txt) status = session.query(GroupStatus).get(update.message.chat_id) for i in status.messages: if i.command == elem[0]: i.message = elem[1] if can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id)
def execute(self, bot, update, txt): chat_id = update.message.chat_id with self.dbWorker.session_scope() as session: txt = txt.strip().lower() session.query(BlockedPhrases) \ .filter(BlockedPhrases.blockedPhrase == txt) \ .delete(synchronize_session=False) API.send_message(bot, chat_id=chat_id, text="Фраза удалена") if can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id)
def delete_welcome_message(lock_info: MutedUser, bot: telegram.bot): chat_id = lock_info.chat_id if not can_delete_messages(bot, chat_id=chat_id): return for message in lock_info.time_messages: API.delete_message(bot, chat_id=chat_id, message_id=message.msg_id) mess_id = lock_info.welcome_msg_id API.delete_message(bot, chat_id=chat_id, message_id=mess_id)
def execute(self, bot, update, txt): with self.dbWorker.session_scope() as session: del_cmd = self.parse_command(txt)[0] mdl = session.query(GroupStatus).get(update.message.chat_id) # mdl = self.dbWorker.get_model(session, GroupStatus, update.message.chat_id) if mdl is not None: mdl.messages = list( filter(lambda x: x.command != del_cmd, mdl.messages)) if can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id)
def __set_read_only(update, context, status, message): bot = context.bot if not is_user_admin(bot, update): return if can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id) change_read_only(update.message.chat_id, status) cmd, txt = Command.parse_command(update.message.text) API.send_message(bot, chat_id=update.message.chat_id, text=message) if txt is not None and txt != "": API.send_message(bot, chat_id=update.message.chat_id, text=txt)
def execute(self, bot, update, txt): with self.dbWorker.session_scope() as session: group_messages = self.__get_group_messages(bot, update, session) group_messages = list(map(lambda x: str(x), group_messages)) res_string = "Команды для чата {!r}: \n".format( update.message.chat.title) append_str = "\n".join(group_messages) API.send_message(bot, chat_id=update.message.from_user.id, text=res_string + append_str) if can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id)
def execute(self, bot, update, txt): with self.dbWorker.session_scope() as session: chat: GroupStatus = session.query(GroupStatus).get( update.message.chat_id) if chat is None: return txt = txt.strip() pattern = re.compile("^(\\d+)").search(txt) if pattern is None: return duration = int(pattern.group(0)) chat.time_to_mute = duration if can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id)
def execute(self, bot, update, txt): chat_id = update.message.chat_id with self.dbWorker.session_scope() as session: chat: GroupStatus = session.query(GroupStatus).get( update.message.chat_id) if chat is None: return txt = txt.strip().lower() phrase = BlockedPhrases() phrase.blockedPhrase = txt chat.blocked_phrases.append(phrase) API.send_message(bot, chat_id=chat_id, text="Фраза добавлена") if can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id)
def delete_blocked_phrases(update, context): chat_id = update.effective_chat.id bot = context.bot message_id = update.message.message_id if not can_delete_messages(bot, update) or is_user_admin(bot, update): return with dataWorker.session_scope() as session: phrases_query = session.query(BlockedPhrases).filter(BlockedPhrases.chat_id == chat_id) phrases = phrases_query.all() text = " " + update.message.text.lower() + " " for phrase in phrases: search_phrase = " " + phrase.blockedPhrase + " " if search_phrase in text: API.delete_message(bot, chat_id, message_id) return
def execute(self, bot, update, txt): with self.dbWorker.session_scope() as session: msg = GroupMessage() elem = self.parse_command(txt) msg.command = elem[0] msg.message = elem[1] mdl = session.query(GroupStatus).get(update.message.chat_id) if mdl is None: mdl = GroupStatus() mdl.status = False mdl.id = update.message.chat_id session.add(mdl) mdl.messages.append(msg) if can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id)
def proceed_message(update, context): bot = context.bot if not proceed_non_text_message(update, context): return cmd, txt = Command.parse_command(update.message.text) logger.info(f"chat_id: {update.message.chat_id} user_id: {update.message.from_user.id} cmd: {cmd}") if cmd == "": delete_blocked_phrases(update, context) return command_to_exec = CommandFactory.get_command_handler(cmd, dataWorker) if command_to_exec.is_admin_rights_needed and not is_user_admin(bot, update): if can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id) else: command_to_exec.execute(bot, update, txt)
def execute(self, bot, update, txt): with self.dbWorker.session_scope() as session: mdl = session.query(GroupStatus).get(update.message.chat_id) if mdl is None: return cm = list(filter(lambda x: x.command == self.cmd, mdl.messages)) if len(cm) == 0 or cm[0].message is None: return if update.message.reply_to_message is not None \ and not is_user_admin(bot, update, update.message.reply_to_message.from_user): reply_msg_id = update.message.reply_to_message.message_id else: reply_msg_id = None API.send_message(bot, chat_id=update.message.chat_id, reply_to_message_id=reply_msg_id, text=cm[0].message) if can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id)
def execute(self, bot, update, txt): pattern = re.compile('^(?P<param>b )?(?P<text>[\s\S\w]+)') isBlocking = False if txt is not None: m = pattern.search(txt.strip()) text = m.group('text') isBlocking = m.group('param') isBlocking = isBlocking is not None and "" != isBlocking with self.dbWorker.session_scope() as session: group = session.query(GroupStatus).get(update.message.chat_id) if group is None: group = GroupStatus() group.id = update.message.chat_id session.add(group) if txt is None or text == '': group.wel_message = None else: group.wel_message = text group.new_users_blocked = isBlocking if can_delete_messages(bot, update): API.delete_message(bot, chat_id=update.message.chat_id, message_id=update.message.message_id)