Ejemplo n.º 1
0
def main(setting_file='settings.json'):
    global scripts
    global users
    global dict_settings
    global database

    try:
        cprint("Load setting file from: \"%s\"" % setting_file, "green")
        with open(setting_file) as f:
            settings_json = json.load(f)
    except (FileNotFoundError):
        cprint("[ERROR] %s is not defined!" % setting_file, "red")
        sys.exit(1)

    for key in dict_settings:
        result, value, message = utils.safe_load_settings(key, settings_json)
        if result is False:
            cprint("[ERROR] {}".format(message), "red")
            if key not in ["telegram_token", "instapy_folder", "allowed_id"]:
                cprint(
                    "[WARNING] Load default value of: {} : {}".format(
                        key, dict_settings[key]), "yellow")
            else:
                sys.exit()
        else:
            dict_settings[key] = value
            cprint("[SUCCESS] {}".format(message), "green")

    if dict_settings['project_path'] != "./":
        sys.path.insert(0, dict_settings['project_path'])

    try:
        from scripts import Scripts
    except (ModuleNotFoundError):
        cprint("[ERROR] Require \"scripts.py\" file!", "red")
        sys.exit(1)

    scripts = Scripts().scripts

    try:
        users = pickle.load(
            open(dict_settings['project_path'] + dict_settings['users_path'],
                 'rb'))
        cprint(
            "[SUCCESS] Load users list from: {}".format(
                dict_settings['users_path']), "green")
    except (FileNotFoundError, IOError):
        pickle.dump(
            users,
            open(dict_settings['project_path'] + dict_settings['users_path'],
                 'wb'))
        cprint(
            "[WARNING] Init user list in: {}".format(
                dict_settings['users_path']), "yellow")

    database = TinyDB('./db.json')

    updater = Updater(dict_settings['telegram_token'],
                      request_kwargs={
                          'read_timeout': 20,
                          'connect_timeout': 20
                      },
                      use_context=True)

    dp = updater.dispatcher

    dp.add_handler(CommandHandler("start", help))
    dp.add_handler(CommandHandler("help", help))

    dp.add_handler(CommandHandler("status", status_process, pass_args=True))
    dp.add_handler(CommandHandler("logs", logs, pass_args=True))
    dp.add_handler(CommandHandler("activity", activity, pass_args=True))

    dp.add_handler(
        CommandHandler("set",
                       set_job,
                       pass_args=True,
                       pass_job_queue=True,
                       pass_chat_data=True))
    dp.add_handler(CommandHandler("now", now, pass_args=True))
    dp.add_handler(CommandHandler("stop", stop, pass_args=True))

    dp.add_handler(
        CommandHandler("unset", unset, pass_args=True, pass_chat_data=True))
    dp.add_handler(CommandHandler("jobs", list_jobs, pass_chat_data=True))
    dp.add_handler(
        CommandHandler("reload",
                       reload_jobs,
                       pass_job_queue=True,
                       pass_chat_data=True))

    dp.add_handler(CommandHandler("add_user", add_user, pass_args=True))
    dp.add_handler(CommandHandler("delete_user", delete_user, pass_args=True))
    dp.add_handler(CommandHandler("users", print_users))

    dp.add_handler(CommandHandler("scripts", list_scripts))

    dp.add_handler(CommandHandler("time", timenow))

    dp.add_handler(
        CallbackQueryHandler(day_choose,
                             pass_job_queue=True,
                             pass_chat_data=True))

    dp.add_error_handler(error)

    updater.start_polling(timeout=25)

    cprint("TELEGRAM-INSTAPY-SCHEDULING IS READY!", "green")

    updater.idle()

    sys.exit(0)
Ejemplo n.º 2
0
def main(settings_file='settings.json'):
    global scripts
    global dict_settings

    try:
        cprint("Load setting file from: \"%s\"" % settings_file, "green")
        with open(settings_file) as f:
            settings_json = json.load(f)
    except (FileNotFoundError):
        cprint("[ERROR] %s is not defined!" % settings_file, "red")
        sys.exit(1)

    for key in dict_settings:
        result, value, message = utils.safe_load_settings(key, settings_json)
        if result is False:
            cprint("[ERROR] {}".format(message), "red")
            if key not in ["telegram_token", "instapy_folder", "allowed_ids"]:
                cprint(
                    "[WARNING] Load default value of: {} : {}".format(
                        key, dict_settings[key]), "yellow")
            else:
                sys.exit()
        else:
            dict_settings[key] = value
            cprint("[SUCCESS] {}".format(message), "green")

    if dict_settings['project_path'] != "./":
        sys.path.insert(0, dict_settings['project_path'])

    try:
        from scripts import Scripts
    except (ModuleNotFoundError):
        cprint("[ERROR] Require \"scripts.py\" file!", "red")
        sys.exit(1)

    scripts = Scripts().scripts

    updater = Updater(token=dict_settings['telegram_token'],
                      request_kwargs={
                          'read_timeout': 20,
                          'connect_timeout': 20
                      })

    dispatcher = updater.dispatcher

    dispatcher.add_handler(CommandHandler('start', start))
    dispatcher.add_handler(CommandHandler('help', help))

    dispatcher.add_handler(CommandHandler("add_user", add_user,
                                          pass_args=True))
    dispatcher.add_handler(
        CommandHandler("delete_user", delete_user, pass_args=True))
    dispatcher.add_handler(CommandHandler("users", print_users))

    dispatcher.add_handler(CommandHandler("actions", list_actions))
    dispatcher.add_handler(CommandHandler("run", run, pass_args=True))
    dispatcher.add_handler(CommandHandler("stop", stop, pass_args=True))

    dispatcher.add_error_handler(error)

    updater.start_polling(timeout=25)

    cprint("INSTAPY TELEGRAM BOT IS READY!", "green")

    updater.idle()

    sys.exit(0)