Skip to content

Commit

Permalink
Change configuration management
Browse files Browse the repository at this point in the history
Signed-off-by: alfred richardsn <rchrdsn@protonmail.ch>
  • Loading branch information
r4rdsn committed Nov 14, 2019
1 parent 5a267ec commit a8aa270
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Project files
config.py
config.ini

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@


## Installation and launch
1. Create a new Telegram bot by talking to [@BotFather](https://t.me/BotFather) and get its API Token.
2. Install and start MongoDB server
3. Install Python version no less than 3.8
4. Clone the repository:
1. Clone the repository:
```bash
git clone https://github.com/fincubator/tellerbot
cd tellerbot
```
5. Install requirements:
2. Install Python version no less than 3.8.
3. Install requirements:
```bash
pip install -r requirements.txt
```
6. Create config file from template:
4. Create config file from template:
```bash
cp config.py.sample src/config.py
cp config.ini.example config.ini
```
7. Personalize settings by modifying ```config.py``` with your preferable text editor.
8. Launch TellerBot:
5. Personalize settings by modifying ```config.ini``` with your preferable text editor.
6. Create a new Telegram bot by talking to [@BotFather](https://t.me/BotFather) and get its API token.
7. Create a file containing Telegram bot's API token with filename specified in ```token_filename``` from ```config.ini```.
8. Install and start MongoDB server.
9. Launch TellerBot:
```bash
cd ..
python tellerbot
python .
```

## Contributing
Expand Down
22 changes: 22 additions & 0 deletions config.ini.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[Connection]
token_filename = /run/secrets/tbtoken
server_host = example.com
server_port = 5000
webhook_path = /tellerbot/webhook
database_name = tellerbot

[Logging]
logger_level = INFO
log_filename = /var/log/tellerbot.log

[Chat IDs]
support = -123456789
exceptions = -1234567890123

[Orders]
count = 10
limit_hours = 24
limit_count = 10

[Blockchain]
wif_filename = /run/secrets/wif.json
14 changes: 0 additions & 14 deletions config.py.sample

This file was deleted.

1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@
html_static_path = ["_static"]

master_doc = "index"
autodoc_mock_imports = ["src.config"]
2 changes: 2 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from aiogram.utils import executor

from src import bot
from src import config
from src import handlers # noqa: F401
from src import notifications
Expand Down Expand Up @@ -46,6 +47,7 @@ def main():
url_token = secrets.token_urlsafe()
webhook_path = config.WEBHOOK_PATH + "/" + url_token

bot.setup()
executor.start_webhook(
dispatcher=dp,
webhook_path=webhook_path,
Expand Down
19 changes: 12 additions & 7 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,28 @@
from aiogram.dispatcher import Dispatcher

from src import config
from src.database import storage
from src.database import MongoStorage
from src.i18n import i18n


if isinstance(config.TOKEN, str):
tg = Bot(config.TOKEN, loop=asyncio.get_event_loop())
dp = Dispatcher(tg, storage=storage)
tg = Bot(None, loop=asyncio.get_event_loop(), validate_token=False)
dp = Dispatcher(tg)


def setup():
"""Set API token from config to bot and setup dispatcher."""
with open(config.TOKEN_FILE, "r") as token_file:
tg._ctx_token.set(token_file.read().strip())

dp.storage = MongoStorage()

i18n.reload()
dp.middleware.setup(i18n)

logging.basicConfig(
filename=config.LOG_FILENAME, filemode="a", level=config.LOGGER_LEVEL
)
dp.middleware.setup(LoggingMiddleware())
else:
tg = Bot("", validate_token=False)
dp = Dispatcher(tg)


def private_handler(*args, **kwargs):
Expand Down
25 changes: 25 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from configparser import ConfigParser
from pathlib import Path

ROOT_DIR = Path(__file__).parent.parent.resolve()

parser = ConfigParser()
parser.read(ROOT_DIR / "config.ini")

TOKEN_FILE = parser.get("Connection", "token_filename")
SERVER_HOST = parser.get("Connection", "server_host")
SERVER_PORT = parser.getint("Connection", "server_port")
WEBHOOK_PATH = parser.get("Connection", "webhook_path")
DATABASE_NAME = parser.get("Connection", "database_name")

LOGGER_LEVEL = parser.get("Logging", "logger_level")
LOG_FILENAME = parser.get("Logging", "log_filename")

SUPPORT_CHAT_ID = parser.getint("Chat IDs", "support")
EXCEPTIONS_CHAT_ID = parser.getint("Chat IDs", "exceptions")

ORDERS_COUNT = parser.getint("Orders", "count")
ORDERS_LIMIT_HOURS = parser.getint("Orders", "limit_hours")
ORDERS_LIMIT_COUNT = parser.getint("Orders", "limit_count")

WIF_FILENAME = parser.get("Blockchain", "wif_filename")
3 changes: 0 additions & 3 deletions src/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,3 @@ async def wait_closed(self) -> None:
async def close(self):
"""Disconnect from MongoDB."""
client.close()


storage = MongoStorage()
3 changes: 2 additions & 1 deletion src/escrow/blockchain/golos_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from golos.exceptions import TransactionNotFound
from golos.ws_client import error_handler

from src import config
from src.database import database
from src.escrow.blockchain import BaseBlockchain
from src.escrow.blockchain import BlockchainConnectionError
Expand Down Expand Up @@ -114,7 +115,7 @@ async def get_limits(self, asset: str):
return limits.get(asset)

async def transfer(self, to: str, amount: Decimal, asset: str):
with open("wif.json") as wif_file:
with open(config.WIF_FILENAME) as wif_file:
transaction = await get_running_loop().run_in_executor(
None,
self._golos.transfer,
Expand Down
4 changes: 2 additions & 2 deletions src/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
# along with TellerBot. If not, see <https://www.gnu.org/licenses/>.
import os
import typing
from pathlib import Path

from aiogram.contrib.middlewares.i18n import I18nMiddleware
from aiogram.types import User
from babel import Locale

from src import config
from src.database import database


Expand Down Expand Up @@ -62,4 +62,4 @@ async def get_user_locale(
return None


_ = i18n = I18nMiddlewareManual("bot", Path(__file__).parents[1] / "locale")
_ = i18n = I18nMiddlewareManual("bot", config.ROOT_DIR / "locale")

0 comments on commit a8aa270

Please sign in to comment.