-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
80 lines (62 loc) · 1.84 KB
/
db.py
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 json
from decouple import config
from typing import Optional, Tuple
from uuid import UUID
from deta import Deta
deta = Deta(config("DETA_TOKEN"))
telegram_db = deta.Base("telegram_db")
telegram_user_map_db = deta.Base("telegram_user_map_db")
# BOT CONFIGURATION
def get_bot_config():
res = telegram_db.get("bot_config")
if res is None:
return telegram_db.put({"connected_chats": []}, "bot_config")
return res
def set_bot_config(config):
telegram_db.put(config, "bot_config")
# USERNAME AND TELEGRAM USERID MAP
def get_user_from_telegram_user_id(telegram_user_id) -> Optional[Tuple[str, bool]]:
res = telegram_user_map_db.get(str(telegram_user_id))
if res is None:
return None, None
return res['user_id'], res['disabled']
def put_user_from_telegram_user_id(telegram_user_id, user_id, disabled=True):
telegram_user_map_db.put(
{"user_id": user_id, "disabled": disabled}, str(telegram_user_id))
# DRAFT JS
draft_js_template = {
"blocks": [{
"key": "110h8",
"text": "",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [],
"entityRanges": [],
"data": {}
},
{
"key": "8icme",
"text": "This message is sent from 🤖 Telegram bot",
"type": "blockquote",
"depth": 0,
"inlineStyleRanges": [
{
"offset": 0,
"length": 40,
"style": "fontsize-12"
},
{
"offset": 0,
"length": 40,
"style": "CODE"
}
],
"entityRanges": [],
"data": {}
}],
"entityMap": {}
}
def convert_text_to_draft_js_raw(text: str) -> str:
draft_js_raw = draft_js_template
draft_js_raw["blocks"][0]["text"] = text
return json.dumps(draft_js_raw)