def abort_poll(query: catbot.CallbackQuery): data_token = query.data.split('_') try: cmd_chat_id = int(data_token[1]) cmd_id = int(data_token[2]) except ValueError: bot.answer_callback_query(query.id) return with t_lock: poll_list, rec = record_empty_test('poll', list) for i in range(len(poll_list)): p = Poll.from_json(poll_list[i]) if p.chat_id == cmd_chat_id and p.init_id == cmd_id: poll_list.pop(i) break else: bot.answer_callback_query( query.id, text=config['messages']['start_poll_not_found']) return bot.edit_message(query.msg.chat.id, query.msg.id, text=config['messages']['abort_poll_title'] + query.msg.html_formatted_text, parse_mode='HTML') bot.answer_callback_query(query.id, text=config['messages']['abort_poll_answer']) rec['poll'] = poll_list json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False)
def list_trusted(msg: catbot.Message): trusted_list, rec = record_empty_test('trusted', list) resp_list = [] bot.send_message(msg.chat.id, text=config['messages']['list_user_pre'], reply_to_message_id=msg.id) for trusted_id in trusted_list: try: trusted_user = bot.get_chat_member(msg.chat.id, trusted_id) if trusted_user.status == 'left' or trusted_user.status == 'kicked': continue except catbot.UserNotFoundError: continue else: resp_list.append(trusted_user) resp_text: str = config['messages']['list_trusted_succ'] for user in resp_list: resp_text += f'{user.name}、' resp_text = resp_text.rstrip('、') if len(resp_list) == 0: resp_text = config['messages']['list_trusted_empty'] bot.send_message(msg.chat.id, text=resp_text, reply_to_message_id=msg.id)
def list_voter(msg: catbot.Message): resp_list = [] bot.send_message(msg.chat.id, text=config['messages']['list_user_pre'], reply_to_message_id=msg.id) with t_lock: voter_dict, rec = record_empty_test('voter', dict) if str(msg.chat.id) in voter_dict.keys(): for voter_id in voter_dict[str(msg.chat.id)]: try: voter_user = bot.get_chat_member(msg.chat.id, voter_id) if voter_user.status == 'kicked': continue except catbot.UserNotFoundError: continue else: resp_list.append(voter_user) resp_text: str = config['messages']['list_voter_succ'] for user in resp_list: resp_text += f'{user.name}、' resp_text = resp_text.rstrip('、') if len(resp_list) == 0: resp_text = config['messages']['list_voter_empty'] bot.send_message(msg.chat.id, text=resp_text, reply_to_message_id=msg.id)
def stop_poll_scheduled(): with p_lock: poll_list, rec = record_empty_test('poll', list) i = 0 while i < (len(poll_list)): p = Poll.from_json(poll_list[i]) if time.time() > p.end_time and p.open: p.stop() poll_list.pop(i) rec['poll'] = poll_list json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False) bot.send_message( '-100' + str(p.chat_id), text=config['messages']['stop_poll_scheduled'].format( title=p.title), parse_mode='HTML', reply_to_message_id=p.poll_id) resp_text = config['messages']['stop_poll_title'] bot.edit_message('-100' + str(p.chat_id), p.poll_id, text=resp_text + get_poll_text(p), parse_mode='HTML') else: i += 1
def stop_poll(query: catbot.CallbackQuery): callback_token = query.data.split('_') if not len(callback_token) == 4: bot.answer_callback_query(query.id) return try: callback_chat_id = int(query.data.split('_')[1]) callback_init_id = int(query.data.split('_')[2]) except ValueError: bot.answer_callback_query(query.id) return with t_lock: poll_list, rec = record_empty_test('poll', list) for i in range(len(poll_list)): p = Poll.from_json(poll_list[i]) if p.chat_id == callback_chat_id and p.init_id == callback_init_id and p.open: p.stop() poll_list.pop(i) rec['poll'] = poll_list json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False) bot.answer_callback_query(query.id) resp_text = config['messages']['stop_poll_title'] bot.edit_message('-100' + str(callback_chat_id), p.poll_id, text=resp_text + get_poll_text(p), parse_mode='HTML') break
def mark(msg: catbot.Message): mark_rec, rec = record_empty_test('mark', dict) if not str(msg.chat.id).startswith('-100'): bot.send_message(msg.chat.id, text=config['messages']['mark_private'], reply_to_message_id=msg.id) return if not msg.reply: bot.send_message(msg.chat.id, text=config['messages']['mark_empty_reply'], reply_to_message_id=msg.id) return reply_to_id = msg.reply_to_message.id comment = msg.html_formatted_text.lstrip('/mark') if str(msg.chat.id) not in mark_rec.keys(): mark_rec[str(msg.chat.id)] = [{'id': reply_to_id, 'comment': comment}] else: mark_rec[str(msg.chat.id)].append({ 'id': reply_to_id, 'comment': comment }) rec['mark'] = mark_rec json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False) bot.send_message(msg.chat.id, text=config['messages']['mark_succ'], reply_to_message_id=msg.id)
def block_private(msg: catbot.Message): blocked_list, rec = record_empty_test('blocked', list) user_input_token = msg.text.split() id_to_block = [] if len(user_input_token) == 1: bot.send_message(config['operator_id'], text=config['messages']['block_prompt'], reply_to_message_id=msg.id) return else: for item in user_input_token[1:]: try: id_to_block.append(int(item)) except ValueError: continue blocked_set = set(blocked_list) old_blocked_set = blocked_set.copy() blocked_set.update(id_to_block) delta = blocked_set - old_blocked_set if len(delta) == 0: bot.send_message(config['operator_id'], text=config['messages']['block_failed'], reply_to_message_id=msg.id) else: reply_text = config['messages']['block_succ'] for item in delta: reply_text += str(item) + '\n' rec['blocked'] = list(blocked_set) json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False) bot.send_message(config['operator_id'], text=reply_text, reply_to_message_id=msg.id)
def start_poll(query: catbot.CallbackQuery): data_token = query.data.split('_') try: cmd_chat_id = int(data_token[1]) cmd_id = int(data_token[2]) except ValueError: bot.answer_callback_query(query.id) return with t_lock: poll_list, rec = record_empty_test('poll', list) for i in range(len(poll_list)): p = Poll.from_json(poll_list[i]) if p.chat_id == cmd_chat_id and p.init_id == cmd_id: break else: bot.answer_callback_query( query.id, text=config['messages']['start_poll_not_found']) return p.start() button_list = [] for j in range(len(p.option_list)): option = p.option_list[j] button_list.append([ catbot.InlineKeyboardButton( option['text'], callback_data=f'vote_{p.chat_id}_{p.init_id}_{j}') ]) button_list.append([ catbot.InlineKeyboardButton( config['messages']['stop_poll_button'], callback_data=f'vote_{p.chat_id}_{p.init_id}_stop') ]) keyboard = catbot.InlineKeyboard(button_list) poll_msg: catbot.Message = bot.send_message( query.msg.chat.id, text=get_poll_text(p), reply_markup=keyboard, reply_to_message_id=query.msg.id, parse_mode='HTML') bot.edit_message(query.msg.chat.id, query.msg.id, text=query.msg.html_formatted_text, parse_mode='HTML') bot.answer_callback_query(query.id, text=config['messages']['start_poll_answer']) p.poll_id = poll_msg.id poll_list[i] = p.to_json() rec['poll'] = poll_list json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False)
def list_block_private(msg: catbot.Message): blocked_list = record_empty_test('blocked', list)[0] resp_text = '' if len(blocked_list) == 0: bot.send_message(config['operator_id'], text=config['messages']['list_block_empty'], reply_to_message_id=msg.id) return for item in blocked_list: resp_text += f'<a href="tg://user?id={item}">{item}</a>\n' bot.send_message(config['operator_id'], text=resp_text, parse_mode='HTML', reply_to_message_id=msg.id)
def unmark(msg: catbot.Message): mark_rec, rec = record_empty_test('mark', dict) if not str(msg.chat.id).startswith('-100'): bot.send_message(msg.chat.id, text=config['messages']['mark_private'], reply_to_message_id=msg.id) return if str(msg.chat.id) not in mark_rec.keys() or len(mark_rec[str( msg.chat.id)]) == 0: bot.send_message(msg.chat.id, text=config['messages']['mark_list_empty'], reply_to_message_id=msg.id) return unmark_list = [] if msg.reply: unmark_list.append(msg.reply_to_message.id) else: user_input_token = msg.text.split() if len(user_input_token) == 1: bot.send_message(msg.chat.id, text=config['messages']['mark_unmark_prompt'], reply_to_message_id=msg.id) return else: for item in user_input_token[1:]: try: unmark_list.append(int(item)) except ValueError: continue response_text = config['messages']['mark_unmark_succ'] i = 0 while i < len(mark_rec[str(msg.chat.id)]): if mark_rec[str(msg.chat.id)][i]['id'] in unmark_list: unmarked_id = mark_rec[str(msg.chat.id)][i]['id'] mark_rec[str(msg.chat.id)].pop(i) rec['mark'] = mark_rec json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False) response_text += str(unmarked_id) + '\n' else: i += 1 if response_text != config['messages']['mark_unmark_succ']: bot.send_message(msg.chat.id, text=response_text, reply_to_message_id=msg.id) else: bot.send_message(msg.chat.id, text=config['messages']['mark_unmark_failed'], reply_to_message_id=msg.id)
def set_channel_helper(msg: catbot.Message): with t_lock: helper_set, rec = record_empty_test('channel_helper', list) helper_set.append(msg.chat.id) rec['channel_helper'] = helper_set json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False) bot.send_message(msg.chat.id, text=config['messages']['set_channel_helper_succ'], reply_to_message_id=msg.id)
def unset_voter(msg: catbot.Message): with t_lock: voter_dict, rec = record_empty_test('voter', dict) user_input_token = msg.text.split() rm_voter_list = [] if msg.reply: rm_voter_list.append(str(msg.reply_to_message.from_.id)) if msg.reply_to_message.from_.id in voter_dict[str(msg.chat.id)]: voter_dict[str(msg.chat.id)].remove( msg.reply_to_message.from_.id) else: bot.send_message(msg.chat.id, text=config['messages']['unset_voter_failed'], reply_to_message_id=msg.id) return else: if len(user_input_token) == 1: bot.send_message(msg.chat.id, text=config['messages']['unset_voter_prompt'], reply_to_message_id=msg.id) return else: for item in user_input_token[1:]: try: voter_dict[str(msg.chat.id)].remove(int(item)) except ValueError: continue else: rm_voter_list.append(item) if len(rm_voter_list) == 0: bot.send_message(msg.chat.id, text=config['messages']['unset_voter_failed'], reply_to_message_id=msg.id) else: rec['voter'] = voter_dict json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False) resp_text = config['messages']['unset_voter_succ'] + '\n'.join( rm_voter_list) bot.send_message(msg.chat.id, text=resp_text, reply_to_message_id=msg.id)
def channel_helper(msg: catbot.ChatMemberUpdate): with t_lock: helper_set, rec = record_empty_test('channel_helper', list) if msg.chat.id not in helper_set: return if msg.from_.id != msg.new_chat_member.id: return try: bot.kick_chat_member(msg.chat.id, msg.new_chat_member.id, no_ban=True) except catbot.InsufficientRightError: pass except catbot.UserNotFoundError: pass except catbot.RestrictAdminError: pass
def set_voter(msg: catbot.Message): new_voter_id = [] if msg.reply: new_voter_id.append(msg.reply_to_message.from_.id) else: user_input_token = msg.text.split() if len(user_input_token) == 1: bot.send_message(msg.chat.id, text=config['messages']['set_voter_prompt'], reply_to_message_id=msg.id) return else: for item in user_input_token[1:]: try: new_voter_id.append(int(item)) except ValueError: continue with t_lock: voter_dict, rec = record_empty_test('voter', dict) if str(msg.chat.id) not in voter_dict.keys(): voter_dict[str(msg.chat.id)] = [] voter_set = set(voter_dict[str(msg.chat.id)]) old_voter_set = voter_set.copy() voter_set.update(new_voter_id) delta = voter_set - old_voter_set if len(delta) == 0: bot.send_message(msg.chat.id, text=config['messages']['set_voter_failed'], reply_to_message_id=msg.id) else: reply_text = config['messages']['set_voter_succ'] for item in delta: reply_text += str(item) + '\n' voter_dict[str(msg.chat.id)] = list(voter_set) rec['voter'] = voter_dict json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False) bot.send_message(msg.chat.id, text=reply_text, reply_to_message_id=msg.id)
def set_trusted(msg: catbot.Message): trusted_list, rec = record_empty_test('trusted', list) new_trusted_id = [] if msg.reply: new_trusted_id.append(msg.reply_to_message.from_.id) else: user_input_token = msg.text.split() if len(user_input_token) == 1: bot.send_message(msg.chat.id, text=config['messages']['set_trusted_prompt'], reply_to_message_id=msg.id) return else: for item in user_input_token[1:]: try: new_trusted_id.append(int(item)) except ValueError: continue trusted_set = set(trusted_list) old_trusted_set = trusted_set.copy() trusted_set.update(new_trusted_id) delta = trusted_set - old_trusted_set if len(delta) == 0: bot.send_message(msg.chat.id, text=config['messages']['set_trusted_failed'], reply_to_message_id=msg.id) else: reply_text = config['messages']['set_trusted_succ'] for item in delta: reply_text += str(item) + '\n' rec['trusted'] = list(trusted_set) json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False) bot.send_message(msg.chat.id, text=reply_text, reply_to_message_id=msg.id)
def channel_helper_msg_deletion(msg: catbot.Message): with t_lock: helper_set, rec = record_empty_test('channel_helper', list) if msg.chat.id not in helper_set: return if hasattr(msg, 'left_chat_member') and msg.from_.id != bot.id: return if hasattr( msg, 'new_chat_members') and msg.new_chat_members[0].id != msg.from_.id: return try: bot.delete_message(msg.chat.id, msg.id) except catbot.InsufficientRightError: pass except catbot.DeleteMessageError: pass
def list_marked(msg: catbot.Message): mark_rec, rec = record_empty_test('mark', dict) if not str(msg.chat.id).startswith('-100'): bot.send_message(msg.chat.id, text=config['messages']['mark_private'], reply_to_message_id=msg.id) return if str(msg.chat.id) not in mark_rec.keys() or len(mark_rec[str( msg.chat.id)]) == 0: bot.send_message(msg.chat.id, text=config['messages']['mark_list_empty'], reply_to_message_id=msg.id) else: text = '' for record in mark_rec[str(msg.chat.id)]: text += f't.me/c/{str(msg.chat.id).replace("-100", "")}/{record["id"]} {record["comment"]}\n' bot.send_message(msg.chat.id, text=text, reply_to_message_id=msg.id, disable_web_page_preview=True, parse_mode='HTML')
def unblock_private(msg: catbot.Message): blocked_list, rec = record_empty_test('blocked', list) user_input_token = msg.text.split() unblocked_id = [] if len(user_input_token) == 1: bot.send_message(config['operator_id'], text=config['messages']['unblock_prompt'], reply_to_message_id=msg.id) return else: for item in user_input_token[1:]: try: blocked_list.remove(int(item)) except ValueError: continue else: unblocked_id.append(item) if len(unblocked_id) == 0: bot.send_message(config['operator_id'], text=config['messages']['unblock_failed'], reply_to_message_id=msg.id) else: rec['blocked'] = blocked_list json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False) resp_text = config['messages']['unblock_succ'] + '\n'.join(unblocked_id) bot.send_message(config['operator_id'], text=resp_text, reply_to_message_id=msg.id)
def init_poll(msg: catbot.Message): user_input_token = msg.html_formatted_text.split() if len(user_input_token) == 1: bot.send_message(msg.chat.id, text=config['messages']['init_poll_failed'], reply_to_message_id=msg.id) return poll_chat_id = int(str(msg.chat.id).replace('-100', '')) p = Poll(poll_chat_id, msg.id) i = 1 parser = parsedatetime.Calendar() while i < len(user_input_token): if user_input_token[i] == '-n': i += 1 title_list = [] while i < len(user_input_token ) and not user_input_token[i].startswith('-'): title_list.append(user_input_token[i]) i += 1 p.title = ' '.join(title_list) elif user_input_token[i] == '-t': i += 1 t_list = [] while i < len(user_input_token ) and not user_input_token[i].startswith('-'): t_list.append(user_input_token[i]) i += 1 t_str = ' '.join(t_list) p.last_time = time.mktime( parser.parse(datetimeString=t_str)[0]) - time.time() p.readable_time = t_str elif user_input_token[i] == '-o': i += 1 option_text = '' while i < len(user_input_token ) and not user_input_token[i].startswith('-'): option_text += user_input_token[i] + ' ' i += 1 options = option_text.split('!') for j in range(options.count('')): options.remove('') p.set_option(options) elif user_input_token[i] == '-ao': p.anonymous_open = True i += 1 elif user_input_token[i] == '-ac': p.anonymous_closed = True i += 1 elif user_input_token[i] == '-c': p.count_open = True i += 1 elif user_input_token[i] == '-m': p.multiple = True i += 1 elif user_input_token[i] == '-p': i += 1 while i < len(user_input_token ) and not user_input_token[i].startswith('-'): try: p.privilege_level = int(user_input_token[i]) except ValueError: bot.send_message( msg.chat.id, text=config['messages']['init_poll_failed'], reply_to_message_id=msg.id) return else: i += 1 break else: # format error bot.send_message(msg.chat.id, text=config['messages']['init_poll_failed'], reply_to_message_id=msg.id) return if len(p.option_list) == 0: bot.send_message(msg.chat.id, text=config['messages']['init_poll_failed'], reply_to_message_id=msg.id) return with t_lock: poll_list, rec = record_empty_test('poll', list) poll_list.append(p.to_json()) rec['poll'] = poll_list json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False) resp_text = config['messages']['init_poll_succ'].format( title=p.title, last=p.readable_time, anon_open=p.anonymous_open, anon_closed=p.anonymous_closed, count_open=p.count_open, multiple=p.multiple, privilege=config['messages']['vote_privilege'][str(p.privilege_level)]) start_button = catbot.InlineKeyboardButton( config['messages']['start_poll_button'], callback_data=f'vote_{p.chat_id}_{p.init_id}_start') abort_button = catbot.InlineKeyboardButton( config['messages']['abort_poll_button'], callback_data=f'vote_{p.chat_id}_{p.init_id}_abort') keyboard = catbot.InlineKeyboard([[start_button, abort_button]]) bot.send_message(msg.chat.id, text=resp_text, reply_to_message_id=msg.id, reply_markup=keyboard, parse_mode='HTML')
def vote(query: catbot.CallbackQuery): callback_token = query.data.split('_') voter_dict = record_empty_test('voter', dict)[0] trusted_list = record_empty_test('trusted', list)[0] ac_list = record_empty_test('ac', list, file=config['ac_record'])[0] if str(query.msg.chat.id) in voter_dict.keys(): voter_list = voter_dict[str(query.msg.chat.id)] + trusted_list else: voter_list = trusted_list if not len(callback_token) == 4: bot.answer_callback_query(query.id) return try: callback_chat_id = int(query.data.split('_')[1]) callback_init_id = int(query.data.split('_')[2]) choice = int(query.data.split('_')[3]) except ValueError: bot.answer_callback_query(query.id) return with t_lock: poll_list, rec = record_empty_test('poll', list) for i in range(len(poll_list)): p = Poll.from_json(poll_list[i]) if p.chat_id == callback_chat_id and p.init_id == callback_init_id and p.open: # privilege check if p.privilege_level == 1 and query.from_.id not in voter_list: bot.answer_callback_query( query.id, text=config['messages']['vote_ineligible']) return if p.privilege_level == 2 and query.from_.id not in voter_list: for user in ac_list: if user['telegram_id'] == query.from_.id and not user[ 'confirmed']: bot.answer_callback_query( query.id, text=config['messages']['vote_ineligible']) return if user['telegram_id'] == query.from_.id and user[ 'confirmed']: break else: bot.answer_callback_query( query.id, text=config['messages']['vote_ineligible']) return p.vote(query.from_.id, choice) poll_list[i] = p.to_json() rec['poll'] = poll_list json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False) bot.answer_callback_query( query.id, text=config['messages']['vote_received'], show_alert=True) button_list = [] for j in range(len(p.option_list)): option = p.option_list[j] button_list.append([ catbot.InlineKeyboardButton( option['text'], callback_data=f'vote_{p.chat_id}_{p.init_id}_{j}') ]) button_list.append([ catbot.InlineKeyboardButton( config['messages']['stop_poll_button'], callback_data=f'vote_{p.chat_id}_{p.init_id}_stop') ]) keyboard = catbot.InlineKeyboard(button_list) bot.edit_message('-100' + str(callback_chat_id), p.poll_id, text=get_poll_text(p), reply_markup=keyboard, parse_mode='HTML') break elif p.chat_id == callback_chat_id and p.init_id == callback_init_id and not p.open: bot.answer_callback_query( query.id, text=config['messages']['vote_poll_stopped'], show_alert=True) break