예제 #1
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
예제 #2
0
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
예제 #3
0
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)
예제 #4
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)
예제 #5
0
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