-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive_services.py
More file actions
49 lines (35 loc) · 1.61 KB
/
live_services.py
File metadata and controls
49 lines (35 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import requests
from notify_i18n_support import build_telegram_message, translate as t
_FIRESTORE_CLIENT = None
def get_firestore_client():
global _FIRESTORE_CLIENT
if _FIRESTORE_CLIENT is None:
from google.cloud import firestore
_FIRESTORE_CLIENT = firestore.Client()
return _FIRESTORE_CLIENT
def get_state_doc_ref(*, collection="strategy", document="MULTI_ASSET_STATE"):
return get_firestore_client().collection(collection).document(document)
def load_trade_state(*, normalize_fn, default_state_factory, normalize=True, collection="strategy", document="MULTI_ASSET_STATE"):
try:
doc = get_state_doc_ref(collection=collection, document=document).get()
if doc.exists:
payload = doc.to_dict()
return normalize_fn(payload) if normalize else payload
return default_state_factory() if normalize else {}
except Exception as exc:
print(t("firestore_get_state_failed", error=exc))
return None
def save_trade_state(data, *, normalize_fn, collection="strategy", document="MULTI_ASSET_STATE"):
try:
persisted_state = normalize_fn(data)
get_state_doc_ref(collection=collection, document=document).set(persisted_state)
except Exception as exc:
print(t("firestore_write_failed", error=exc))
def send_tg_msg(token, chat_id, text):
if not token or not chat_id:
return
url = f"https://api.telegram.org/bot{token}/sendMessage"
try:
requests.post(url, data={"chat_id": chat_id, "text": build_telegram_message(text)}, timeout=10)
except Exception:
print(t("telegram_send_failed"))