コード例 #1
0
ファイル: main.py プロジェクト: qvant/idle_rpg_bot
def read_keyboard(trans: L18n):
    keyboard = [
        InlineKeyboardButton(trans.get_message(M_FEEDBACK_DONE),
                             callback_data=READ_MENU_DONE),
        InlineKeyboardButton(trans.get_message(M_FEEDBACK_REPLY),
                             callback_data=READ_MENU_REPLY)
    ]

    return pretty_menu(keyboard)
コード例 #2
0
ファイル: main.py プロジェクト: qvant/idle_rpg_bot
def shutdown_keyboard(trans: L18n):
    keyboard = [
        InlineKeyboardButton(trans.get_message(M_SHUTDOWN_NORMAL),
                             callback_data=SHUTDOWN_MENU_NORMAL),
        InlineKeyboardButton(trans.get_message(M_SHUTDOWN_IMMEDIATE),
                             callback_data=SHUTDOWN_MENU_IMMEDIATE),
        InlineKeyboardButton(trans.get_message(M_SHUTDOWN_BOT),
                             callback_data=SHUTDOWN_MENU_BOT)
    ]

    return pretty_menu(keyboard)
コード例 #3
0
ファイル: main.py プロジェクト: qvant/idle_rpg_bot
def admin_keyboard(trans: L18n):
    keyboard = [
        InlineKeyboardButton(trans.get_message(M_SERVER_STATS),
                             callback_data=ADMIN_MENU_STATS),
        InlineKeyboardButton(trans.get_message(M_BOT_STATS),
                             callback_data=ADMIN_MENU_BOT_STATS),
        InlineKeyboardButton(trans.get_message(M_SHUTDOWN_LABEL),
                             callback_data=ADMIN_MENU_SHUTDOWN_BASIC),
        InlineKeyboardButton(trans.get_message(M_GET_FEEDBACK),
                             callback_data=ADMIN_MENU_GET_FEEDBACK),
    ]

    return pretty_menu(keyboard)
コード例 #4
0
ファイル: main.py プロジェクト: qvant/idle_rpg_bot
def class_keyboard(trans: L18n):
    global class_list
    keyboard = []
    if not class_list:
        return None
    for i in class_list:
        keyboard.append(
            InlineKeyboardButton(trans.get_message(i),
                                 callback_data="class_" + str(i)))
    return pretty_menu(keyboard)
コード例 #5
0
ファイル: main.py プロジェクト: qvant/idle_rpg_bot
def locale_keyboard(trans: L18n):
    global translations
    keyboard = []
    for i in translations:
        keyboard.append(
            InlineKeyboardButton(i, callback_data=LOCALE_PREFIX + i))
    keyboard.append(
        InlineKeyboardButton(trans.get_message(M_DYNAMIC_LOCALE),
                             callback_data=LOCALE_PREFIX + M_DYNAMIC_LOCALE))

    return pretty_menu(keyboard)
コード例 #6
0
ファイル: main.py プロジェクト: qvant/idle_rpg_bot
def main_keyboard(chat_id: int, trans: L18n):
    global config
    keyboard = [
        InlineKeyboardButton(trans.get_message(M_NEW_CHARACTER),
                             callback_data=MAIN_MENU_CREATE),
        InlineKeyboardButton(trans.get_message(M_ABOUT_LABEL),
                             callback_data=MAIN_MENU_ABOUT),
        InlineKeyboardButton(trans.get_message(M_DELETE_CHARACTER),
                             callback_data=MAIN_MENU_DELETE),
        InlineKeyboardButton(trans.get_message(M_GET_CHARACTER),
                             callback_data=MAIN_MENU_STATUS),
        InlineKeyboardButton(trans.get_message(M_SETTINGS),
                             callback_data=MAIN_MENU_SETTINGS),
        InlineKeyboardButton(trans.get_message(M_FEEDBACK),
                             callback_data=MAIN_MENU_FEEDBACK),
    ]
    if chat_id in config.admin_list:
        keyboard.append(
            InlineKeyboardButton(trans.get_message(M_ADMIN_LABEL),
                                 callback_data=MAIN_MENU_ADMIN))

    return pretty_menu(keyboard)
コード例 #7
0
ファイル: main.py プロジェクト: qvant/idle_rpg_bot
def main():
    global class_list
    global class_descriptions
    global creation_process
    global deletion_process
    global feedback_process
    global feedback_reading
    global feedback_replying
    global characters
    global out_channel
    global updater
    global queue_logger
    global telegram_logger
    global config
    global is_shutdown
    global user_locales
    global translations
    global startup_time
    global user_settings

    is_shutdown = False
    class_list = []
    class_descriptions = {}
    creation_process = {}
    deletion_process = {}
    feedback_process = {}
    feedback_reading = {}
    feedback_replying = {}
    characters = {}
    user_locales = {}
    translations = {}
    startup_time = datetime.datetime.now().replace(microsecond=0)

    parser = argparse.ArgumentParser(description='Idle RPG telegram bot.')
    parser.add_argument("--config",
                        '-cfg',
                        help="Path to config file",
                        action="store",
                        default="cfg//main.json")
    parser.add_argument("--test_users",
                        help="Number of test users of each class created",
                        action="store",
                        default=None)
    parser.add_argument("--delay",
                        help="Number of test users of each class created",
                        action="store",
                        default=None)
    args = parser.parse_args()
    if args.delay is not None:
        time.sleep(int(args.delay))
    config = Config(args.config)

    logger = get_logger(LOG_MAIN, config.log_level)
    queue_logger = get_logger(LOG_QUEUE, config.log_level)
    telegram_logger = get_logger(LOG_TELEGRAM, config.log_level)
    # set_basic_logging(config.log_level)

    user_settings = Persist(config)
    user_settings.check_version()
    user_locales = user_settings.get_all_locale()

    for dirpath, dirnames, filenames in os.walk("l18n"):
        for lang_file in filenames:
            logger.info(
                "Start process localization file {0}".format(lang_file))
            translations[lang_file[:2]] = L18n()
            translations[lang_file[:2]].set_locale(lang_file[:-4])
            logger.info(
                "Finish process localization file {0}".format(filenames))

    updater = Updater(token=config.secret, use_context=True)
    dispatcher = updater.dispatcher

    start_handler = CommandHandler('start', start)
    create_handler = CommandHandler('create', create)
    # TODO: make patterns with regexp
    class_menu_handler = CallbackQueryHandler(class_menu, pattern="class_")
    main_menu_handler = CallbackQueryHandler(main_menu, pattern="main_")
    admin_menu_handler = CallbackQueryHandler(admin_menu, pattern="admin_")
    shutdown_menu_handler = CallbackQueryHandler(shutdown_menu,
                                                 pattern="shutdown_")
    locale_menu_handler = CallbackQueryHandler(set_locale,
                                               pattern=LOCALE_PREFIX)
    read_menu_handler = CallbackQueryHandler(read_menu, pattern="confirm_")
    echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
    dispatcher.add_handler(start_handler)
    dispatcher.add_handler(create_handler)
    dispatcher.add_handler(main_menu_handler)
    dispatcher.add_handler(echo_handler)
    dispatcher.add_handler(class_menu_handler)
    dispatcher.add_handler(admin_menu_handler)
    dispatcher.add_handler(shutdown_menu_handler)
    dispatcher.add_handler(locale_menu_handler)
    dispatcher.add_handler(read_menu_handler)

    out_queue = get_mq_connect(config)
    out_channel = out_queue.channel()
    out_channel.queue_declare(queue=QUEUE_NAME_INIT)
    out_channel.queue_declare(queue=QUEUE_NAME_CMD, durable=True)
    out_channel.queue_declare(queue=QUEUE_NAME_RESPONSES, durable=True)
    out_channel.queue_declare(queue=QUEUE_NAME_DICT, durable=True)

    msg = {"cmd_type": CMD_GET_CLASS_LIST}
    out_channel.basic_publish(exchange="",
                              routing_key=QUEUE_NAME_INIT,
                              body=json.dumps(msg),
                              properties=pika.BasicProperties(
                                  content_type="application/json",
                                  content_encoding="UTF-8",
                                  app_id=QUEUE_APP_ID))

    logger.info("Asked server for class list")

    if args.test_users is not None:
        out_channel.basic_consume(queue=QUEUE_NAME_DICT,
                                  on_message_callback=dict_response_callback,
                                  auto_ack=True)

        for method_frame, properties, body in out_channel.consume(
                QUEUE_NAME_DICT, inactivity_timeout=1):
            if class_list:
                break
        out_channel.cancel()
        logger.info("Class list received")
        test_start_time = datetime.datetime.now()
        cnt_users = 0
        logger.info("Started create test users")
        for i in range(int(args.test_users)):
            for j in class_list:
                cmd = {
                    "cmd_type": CMD_CREATE_CHARACTER,
                    "name": j + '_' + str(i + 1),
                    "class": j,
                    "locale": "en"
                }
                cnt_users += 1
                enqueue_command(cmd)
        test_finish_time = datetime.datetime.now()
        logger.info(
            "Finish create test users, was created {}. Started at {}, finish at {}"
            .format(cnt_users, test_start_time, test_finish_time))

    updater.start_polling()

    logger.info("Start listen server responses")

    for i in config.admin_list:
        trans = get_locale(None, i)
        reply_markup = InlineKeyboardMarkup(admin_keyboard(trans))
        updater.dispatcher.bot.send_message(
            chat_id=i,
            text=trans.get_message(M_BOT_STARTED_UP).format(
                datetime.datetime.now()),
            reply_markup=reply_markup)
    while True:
        try:
            for method_frame, properties, body in out_channel.consume(
                    QUEUE_NAME_RESPONSES, inactivity_timeout=5,
                    auto_ack=False):
                if body is not None:
                    logger.info(
                        "Received user message {0} with delivery_tag {1}".
                        format(body, method_frame.delivery_tag))
                    cmd_response_callback(None, method_frame, properties, body)
                    out_channel.basic_ack(method_frame.delivery_tag)
                    logger.info("User message " + str(body) +
                                " with delivery_tag " +
                                str(method_frame.delivery_tag) +
                                " acknowledged")
                else:
                    logger.info(
                        "No more messages in {0}".format(QUEUE_NAME_RESPONSES))
                    out_channel.cancel()
                    break
            for method_frame, properties, body in out_channel.consume(
                    QUEUE_NAME_DICT, inactivity_timeout=5, auto_ack=False):
                if body is not None:
                    logger.info(
                        "Received server message {0} with delivery_tag {1}".
                        format(body, method_frame.delivery_tag))
                    dict_response_callback(None, method_frame, properties,
                                           body)
                    out_channel.basic_ack(method_frame.delivery_tag)
                    logger.info("Received server message " + str(body) +
                                " with delivery_tag " +
                                str(method_frame.delivery_tag) +
                                " acknowledged")
                else:
                    logger.info(
                        "No more messages in {0}".format(QUEUE_NAME_DICT))
                    out_channel.cancel()
                    break
        except pika.exceptions.AMQPError as exc:
            logger.critical(
                "Error {0} when consume in queue, reconnect.".format(exc))
            out_queue = get_mq_connect(config)
            out_channel = out_queue.channel()
        # should be in QUEUE_NAME_DICT listener, but to make things easier put it here
        if is_shutdown:
            updater.stop()
            sys.exit(0)