-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive_services.py
More file actions
80 lines (64 loc) · 2.86 KB
/
Copy pathlive_services.py
File metadata and controls
80 lines (64 loc) · 2.86 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import hashlib
import requests
from notify_i18n_support import build_telegram_message, translate as t
def _get_document_store():
"""Lazy-init the cloud-agnostic document store."""
from quant_platform_kit.cloud import get_document_store
return get_document_store()
def get_firestore_client():
"""Return the underlying Firestore client for direct collection/document access.
NOTE: this relies on the GCP provider's ``.client`` property and will
raise AttributeError when the active provider is not GCP.
"""
return _get_document_store().client
def get_state_doc_ref(*, collection="strategy", document="MULTI_ASSET_STATE"):
"""Return a Firestore document reference for the given collection/document."""
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:
payload = _get_document_store().get(collection=collection, document_id=document)
if payload is not None:
return normalize_fn(payload) if normalize else payload
return default_state_factory() if normalize else {}
except Exception:
print(t("firestore_get_state_failed", error="state_load_failed"))
return None
def save_trade_state(data, *, normalize_fn, collection="strategy", document="MULTI_ASSET_STATE"):
try:
persisted_state = normalize_fn(data)
_get_document_store().set(collection=collection, document_id=document, data=persisted_state)
return True
except Exception:
print(t("firestore_write_failed", error="state_persistence_failed"))
return False
def send_tg_msg(token, chat_id, text):
message = build_telegram_message(text)
receipt = {
"sink": "telegram",
"delivery_status": "failed",
"transport_acknowledged": False,
"compact_text_sha256": hashlib.sha256(message.encode("utf-8")).hexdigest(),
"compact_text_length": len(message),
}
if not token or not chat_id:
return {**receipt, "error_type": "missing_target"}
url = f"https://api.telegram.org/bot{token}/sendMessage"
try:
response = requests.post(
url,
data={"chat_id": chat_id, "text": message},
timeout=10,
)
if int(getattr(response, "status_code", 500)) >= 400:
return {**receipt, "error_type": "http_error"}
payload = response.json()
if not isinstance(payload, dict) or payload.get("ok") is not True:
return {**receipt, "error_type": "telegram_rejected"}
return {
**receipt,
"delivery_status": "sent",
"transport_acknowledged": True,
}
except Exception as exc:
print(t("telegram_send_failed"))
return {**receipt, "error_type": type(exc).__name__}