def answer(self, client: Client, msg: CallbackQuery): msg.data = msg.data.decode(errors = 'ignore') if msg.data.startswith('cancel'): client.edit_message_reply_markup(msg.message.chat.id, msg.message.message_id) msg.answer('Canceled') elif msg.data.startswith('unban'): self.mysqldb.execute("UPDATE `tickets_user` SET `baned` = 0 WHERE `user_id` = {}".format(msg.data.split()[-1])) msg.answer('UNBANED') client.edit_message_reply_markup(msg.message.chat.id, msg.message.message_id) elif msg.data.startswith('refresh'): try: client.edit_message_text( msg.message.chat.id, msg.message.message_id, self.generate_superuser_text(msg.data.split()[-1]), 'markdown', reply_markup = msg.message.reply_markup ) except api.errors.exceptions.bad_request_400.MessageNotModified: pass msg.answer('refreshed') elif 'confirm' in msg.data: try: self.confirm(client, msg) except TimeoutError: msg.answer('Confirmation time out') client.edit_message_reply_markup(msg.message.chat.id, msg.message.message_id) elif any(msg.data.startswith(x) for x in ('close', 'block', 'send', 'bypass', 'reset', 'unlimited', 'del', 'renew', 'setpass')): self.send_confirm(client, msg) else: try: raise ValueError(msg.data) except: client.send_message(self.help_group, traceback.format_exc(), disable_web_page_preview = True)
def handle_callback(self, client: Client, msg: CallbackQuery): msg.data = msg.data.decode(errors='ignore') try: if msg.data.startswith('cancel') or msg.data == 'rm': msg.answer( msg.id, 'Canceled' if not msg.data == 'rm' else 'Button removed') if msg.data.endswith('d'): client.delete_messages(msg.message.chat.id, msg.message.message_id) else: client.edit_message_reply_markup(msg.message.chat.id, msg.message.message_id) if self.join_group_verify is not None and self.join_group_verify.click_to_join( client, msg): return if msg.data.startswith('res'): if time.time() - msg.message.date > 20: raise OperationTimeoutError() _, dur, _type, _user_id = msg.data.split() if client.restrict_chat_member( self.target_group, int(_user_id), int(time.time()) + int(dur), **({ 'write': { 'can_send_messages': True }, 'media': { 'can_send_media_messages': True }, 'stickers': { 'can_send_other_messages': True }, 'link': { 'can_add_web_page_previews': True }, 'read': {} }.get(_type))): msg.answer('The user is restricted successfully.') client.edit_message_text( msg.message.chat.id, msg.message.message_id, 'Restrictions applied to {} Duration: {}'.format( build_html_parse.parse_user(_user_id), '{}s'.format(dur) if int(dur) else 'Forever'), parse_mode='markdown', reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton( text='UNBAN', callback_data='unban {}'.format( _user_id).encode()) ]])) elif msg.data.startswith('unban'): if client.restrict_chat_member(self.target_group, int(msg.data.split()[-1]), 0, True, True, True, True): msg.answer('Unban successfully') client.edit_message_reply_markup(msg.message.chat.id, msg.message.message_id) elif msg.data.startswith('auth'): if time.time() - msg.message.date > 20: raise OperationTimeoutError() auth_system.add_user(msg.data.split()[1]) msg.answer('{} added to the authorized group'.format( msg.data.split()[1])) client.edit_message_text( msg.message.chat.id, msg.message.message_id, '{} added to the authorized group'.format( msg.data.split()[1])) with open('config.ini', 'w') as fout: config.write(fout) elif msg.data.startswith('fwd'): if time.time() - msg.message.date > 30: raise OperationTimeoutError() if 'original' in msg.data: self.conn.insert_ex( client.forward_messages( self.target_group, msg.message.chat.id, msg. message.reply_to_message.message_id).message_id, msg.message.reply_to_message.message_id) else: self.conn.insert_ex( client.send_message( self.target_group, build_html_parse( msg.message.reply_to_message).split_offset(), 'html').message_id, msg.message.reply_to_message.message_id) msg.answer('Forward successfully') client.delete_messages(msg.message.chat.id, msg.message.message_id) elif msg.data.startswith('kick'): if not msg.data.startswith( 'kickc') and msg.from_user.id != int( msg.data.split()[-2]): raise OperatorError() if 'true' not in msg.data: if not msg.data.startswith( 'kickc') and time.time() - msg.message.date > 15: raise OperationTimeoutError() args = [ msg.message.chat.id, msg.message.message_id, 'Press the button again to kick {}\nThis confirmation message will expire after 10 seconds.' .format( build_html_parse.parse_user(msg.data.split()[-1])), ] if msg.data.startswith('kickc'): args.pop(1) r = msg.data.split() r.insert(1, msg.from_user.id) msg.data = ' '.join(str(x) for x in r) del r kwargs = { 'parse_mode': 'markdown', 'reply_markup': InlineKeyboardMarkup( inline_keyboard=[[ InlineKeyboardButton( text='Yes, please.', callback_data=b' '.join(( b'kick true', ' '.join( msg.data.split()[1:]).encode()))) ], [ InlineKeyboardButton( text='Cancel', callback_data=b'cancel') ]]) } (client.send_message if msg.data.startswith('kickc') else client.edit_message_text)(*args, **kwargs) msg.answer( 'Please press again to make sure. Do you really want to kick {} ?' .format(msg.data.split()[-1]), True) else: if msg.message.edit_date: if time.time() - msg.message.edit_date > 10: raise OperationTimeoutError() else: if time.time() - msg.message.date > 10: raise OperationTimeoutError() client.kick_chat_member(self.target_group, int(msg.data.split()[-1])) msg.answer('Kicked {}'.format(msg.data.split()[-1])) client.edit_message_text( msg.message.chat.id, msg.message.message_id, 'Kicked {}'.format( build_html_parse.parse_user(msg.data.split()[-1]))) #app.send_message(self.fudu_group, 'Kicked {}'.format(msg.message.entities[0].user.id)) #client.delete_messages(msg.message.chat.id, msg.message.message_id) elif msg.data.startswith('promote'): if not msg.data.endswith('undo'): if time.time() - msg.message.date > 10: raise OperationTimeoutError() self.botapp.promote_chat_member(self.target_group, int(msg.data.split()[1]), True, can_delete_messages=True, can_restrict_members=True, can_invite_users=True, can_pin_messages=True, can_promote_members=True) msg.answer('Promote successfully') client.edit_message_text( msg.message.chat.id, msg.message.message_id, 'Promoted {}'.format( build_html_parse.parse_user( int(msg.data.split()[1]))), parse_mode='markdown', reply_markup=InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton(text='UNDO', callback_data=' '.join(( msg.data, 'undo')).encode()) ], [ InlineKeyboardButton(text='remove button', callback_data=b'rm') ] ])) else: self.botapp.promote_chat_member(self.target_group, int(msg.data.split()[1]), False, can_delete_messages=False, can_invite_users=False, can_restrict_members=False) msg.answer('Undo Promote successfully') client.edit_message_text( msg.message.chat.id, msg.message.message_id, 'Unpromoted {}'.format( build_html_parse.parse_user( int(msg.data.split()[1]))), parse_mode='markdown') except OperationTimeoutError: msg.answer('Confirmation time out') client.edit_message_reply_markup(msg.message.chat.id, msg.message.message_id) except OperatorError: msg.answer( 'The operator should be {}.'.format(msg.data.split()[-2]), True) except: self.app.send_message(int(config['fuduji']['help_group']), traceback.format_exc().splitlines()[-1]) traceback.print_exc()