diff --git a/.gitignore b/.gitignore index d4b6024..9ac0792 100644 --- a/.gitignore +++ b/.gitignore @@ -168,4 +168,5 @@ cython_debug/ # Project cache data -logs \ No newline at end of file +logs +config.json \ No newline at end of file diff --git a/README.md b/README.md index 9949438..cbf728c 100644 --- a/README.md +++ b/README.md @@ -57,8 +57,10 @@ To make this bot run at first you need to have a Python interpreter, Photos API, 6. Configure "bot" and "owner" with your favorite text editor: - `nano config.json` - You can edit with vim, nano, on Windows it's Notepad or Notepad++. Whatever. + 1. Copy file `config_example.json` to `config.json` + 2. Open `config.json` using your favorite text editor. For example `nano config.json`, but you can edit with vim, nano, on Windows it's Notepad or Notepad++. Whatever + 3. Change `"owner"`, `"bot.api_id"`, `"bot.api_hash"` and `"bot.bot_token"` keys' values. + If you don't know where to find bot_token and your id - here you can find some hints: [get bot token](https://www.siteguarding.com/en/how-to-get-telegram-bot-api-token), [get your id](https://www.alphr.com/telegram-find-user-id), [get api_hash and api_id](https://core.telegram.org/api/obtaining_api_id). 7. Configure database and API: diff --git a/config.json b/config_example.json similarity index 100% rename from config.json rename to config_example.json diff --git a/modules/default_config.json b/modules/default_config.json new file mode 100644 index 0000000..692e452 --- /dev/null +++ b/modules/default_config.json @@ -0,0 +1,121 @@ +{ + "locale": "en", + "locale_log": "en", + "locale_fallback": "en", + "owner": 0, + "admins": [], + "bot": { + "api_id": 0, + "api_hash": "", + "bot_token": "" + }, + "database": { + "user": null, + "password": null, + "host": "127.0.0.1", + "port": 27017, + "name": "tgposter" + }, + "mode": { + "post": true, + "submit": true + }, + "reports": { + "sent": false, + "error": true, + "startup": true, + "shutdown": true + }, + "logging": { + "size": 512, + "location": "logs" + }, + "locations": { + "tmp": "tmp", + "data": "data", + "cache": "cache", + "sent": "data/sent", + "queue": "data/queue", + "index": "data/index.json", + "locale": "locale" + }, + "posting": { + "channel": 0, + "silent": false, + "move_sent": false, + "use_interval": false, + "interval": "1h30m", + "page_size": 300, + "submitted_caption": { + "enabled": true, + "ignore_admins": true, + "text": "#submitted" + }, + "extensions": { + "photo": [ + "jpg", + "png", + "gif", + "jpeg" + ], + "video": [ + "mp4", + "avi", + "mkv", + "webm", + "mov" + ] + }, + "time": [ + "08:00", + "10:00", + "12:00", + "14:00", + "16:00", + "18:00", + "20:00", + "22:00" + ], + "api": { + "address": "http://localhost:8054", + "address_external": "https://photos.domain.com", + "username": "", + "password": "", + "album": "" + } + }, + "caption": { + "enabled": false, + "link": null, + "text": [ + "sample text" + ] + }, + "submission": { + "timeout": 30, + "file_size": 15728640, + "tmp_size": 15728640, + "allow_duplicates": false, + "send_uploaded_id": false, + "require_confirmation": { + "users": true, + "admins": true + }, + "mime_types": [ + "image/png", + "image/gif", + "image/jpeg", + "video/mp4", + "video/quicktime" + ] + }, + "commands": [ + "start", + "rules" + ], + "commands_admin": [ + "import", + "export", + "reboot" + ] +} \ No newline at end of file diff --git a/modules/utils.py b/modules/utils.py index ab518d8..9c863f4 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -1,4 +1,4 @@ -from os import kill +from os import kill, path from os import name as osname from os import sep from sys import exit @@ -73,9 +73,20 @@ def configGet(key: str, *args: str): * any: Value of provided key """ this_dict = jsonLoad("config.json") - this_key = this_dict - for dict_key in args: - this_key = this_key[dict_key] + try: + this_key = this_dict + for dict_key in args: + this_key = this_key[dict_key] + except KeyError: + print( + f"Could not find config key '{key}' under path {args}: falling back to default config", + flush=True, + ) + fallback_dict = jsonLoad(path.join("modules", "default_config.json")) + this_key = fallback_dict + for dict_key in args: + this_key = this_key[dict_key] + configSet(key, this_key[key], args) return this_key[key]