예제 #1
0
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)
예제 #2
0
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)
예제 #3
0
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
예제 #4
0
def get_user_id(msg: catbot.Message):
    if msg.reply:
        res_id = msg.reply_to_message.from_.id
    else:
        res_id = msg.from_.id
    bot.send_message(chat_id=msg.chat.id,
                     text=res_id,
                     reply_to_message_id=msg.id)
예제 #5
0
파일: pm.py 프로젝트: The-Earth/Cat-big-bot
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)
예제 #6
0
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)
예제 #7
0
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)
예제 #8
0
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)
예제 #9
0
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)
예제 #10
0
파일: pm.py 프로젝트: The-Earth/Cat-big-bot
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)
예제 #11
0
def get_permalink(msg: catbot.Message):
    id_list = []
    user_input_token = msg.text.split()
    if len(user_input_token) == 1:
        bot.send_message(msg.chat.id,
                         text=config['messages']['permalink_prompt'],
                         reply_to_message_id=msg.id)
        return
    else:
        for item in user_input_token[1:]:
            try:
                id_list.append(int(item))
            except ValueError:
                continue

    resp_text = ''
    for item in id_list:
        resp_text += f'<a href="tg://user?id={item}">{item}</a>\n'
    bot.send_message(msg.chat.id,
                     text=resp_text,
                     parse_mode='HTML',
                     reply_to_message_id=msg.id)
예제 #12
0
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)
예제 #13
0
파일: pm.py 프로젝트: The-Earth/Cat-big-bot
def pass_on(msg: catbot.Message):
    from_username = '******' + msg.from_.username if msg.from_.username != '' else 'No username'
    try:
        bot.send_message(chat_id=config['operator_id'],
                         text=config['messages']['pass_on_new_msg_to_op'].format(from_id=msg.from_.id,
                                                                                 from_name=msg.from_.name,
                                                                                 from_username=from_username),
                         parse_mode='HTML')
        bot.forward_message(from_chat_id=msg.chat.id, to_chat_id=config['operator_id'], msg_id=msg.id)
    except catbot.APIError:
        bot.send_message(chat_id=msg.from_.id, text=config['messages']['pass_on_sending_failed'])
        raise
    else:
        bot.send_message(chat_id=msg.from_.id, text=config['messages']['pass_on_sent_to_op_succ'])
예제 #14
0
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)
예제 #15
0
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)
예제 #16
0
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')
예제 #17
0
파일: pm.py 프로젝트: The-Earth/Cat-big-bot
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)
예제 #18
0
def bot_help(msg: catbot.Message):
    bot.send_message(msg.chat.id,
                     text=config['messages']['help'],
                     reply_to_message_id=msg.id)
예제 #19
0
def start(msg: catbot.Message):
    bot.send_message(chat_id=msg.chat.id, text=config['messages']['start'])
예제 #20
0
def get_chat_id(msg: catbot.Message):
    bot.send_message(chat_id=msg.chat.id,
                     text=msg.chat.id,
                     reply_to_message_id=msg.id)
예제 #21
0
파일: pm.py 프로젝트: The-Earth/Cat-big-bot
def reply(msg: catbot.Message):
    if not msg.reply:
        bot.send_message(config['operator_id'], text=config['messages']['pass_on_reply_invalid'])
        return
    to_id = msg.text.split(' ')[0].split('_')[1]
    try:
        bot.send_message(chat_id=int(to_id), text=config['messages']['pass_on_reply_from_op'])
        bot.forward_message(from_chat_id=config['operator_id'], to_chat_id=int(to_id), msg_id=msg.reply_to_message.id)
    except catbot.ChatNotFoundError:
        bot.send_message(chat_id=config['operator_id'], text=config['messages']['pass_on_sending_failed'])
    except ValueError:
        bot.send_message(chat_id=config['operator_id'], text=config['messages']['pass_on_reply_invalid'])
    except catbot.APIError:
        bot.send_message(chat_id=config['operator_id'], text=config['messages']['pass_on_sending_failed'])
    else:
        bot.send_message(chat_id=config['operator_id'],
                         text=config['messages']['pass_on_reply_succ'].format(to_id=to_id))
예제 #22
0
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')