-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
167 lines (141 loc) · 4.7 KB
/
app.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*-coding:utf8;-*-
from cryptography.fernet import Fernet
from bottle import route, request, post, auth_basic, hook, response
import bottle
import json
import bottle
import telegram
import os
"""
Telegram notification bot with highly privacy/data protection.
author: guangrei
"""
# bottle.BaseRequest.MEMFILE_MAX = 1024 * 1024 # un-comment this to change default request limit!
HOST = os.environ.get("TG_BOT_HOST")
ENCRYPTION_KEY = os.environ.get("ENCRYPTION_KEY")
admin_user = os.environ.get("ADMIN_USER")
admin_password = os.environ.get("ADMIN_PASSWORD")
start_template = """
Your token is `{token}`
You are now ready to start sending messages, please check /help\_send\_text and /help\_send\_file to get started!
"""
help_send_text_template = """
** ENDPOINTS: **
`https://end-points`
** METHOD: ** `POST`
** BODY: **
`{
"text": "message text (required)"
}`
example:
`curl -X POST https://end-points -d '{"text": "Hello, world 🐣"}'`
"""
help_send_file_template = """
** ENDPOINTS: **
`https://end-points`
** METHOD: ** `POST`
** BODY: **
`{
"file": {
"name": "file name with extension (required)",
"content": "base64 file content (required)",
"caption": "file caption (optional)"
}
}`
example:
`echo "this is test file!" > test_file.txt && curl -X POST https://end-points -d "{\\"file\\": {\\"name\\": \\"test_file.txt\\", \\"content\\": \\"$(base64 -w 0 < test_file.txt)\\"}}"`
"""
help_template = """
to get support please open issues <a href="https://github.com/cirebon-dev/notification_bot">here</a>
and follow our channel @anak_tkj
"""
_allow_origin = "*"
_allow_methods = "PUT, GET, POST, DELETE, OPTIONS"
_allow_headers = "Authorization, Origin, Accept, Content-Type, X-Requested-With"
key = ENCRYPTION_KEY.encode()
fernet = Fernet(key)
def is_authenticated_user(user, password):
return user == admin_user and password == admin_password
@hook("after_request")
def enable_cors():
response.headers["Access-Control-Allow-Origin"] = _allow_origin
response.headers["Access-Control-Allow-Methods"] = _allow_methods
response.headers["Access-Control-Allow-Headers"] = _allow_headers
@route("/", method="OPTIONS")
@route("/<path:path>", method="OPTIONS")
def options_handler(path=None):
return
@route("/")
def root_handler():
return "its works!"
@route("/update_webhook")
@auth_basic(is_authenticated_user)
def update_handler():
url = f'https://{HOST}/{telegram.token.replace(":","_")}'
return telegram.set_webhook(url)
@post("/" + telegram.token.replace(":", "_"))
def telegram_hook():
data = request.json
msg = telegram.get_message(data)
msg_id = telegram.get_message_id(data)
chat_id = telegram.get_chat_id(data)
token = fernet.encrypt(str(chat_id).encode())
api_uri = f"{HOST}/h/{token.decode()}"
if msg == "/start":
msg = start_template.format(token=token.decode())
telegram.send_message(
msg,
chat_id,
parse_mode="Markdown",
disable_web_page_preview=True,
reply_to_message_id=msg_id,
)
elif msg == "/help_send_text":
msg = help_send_text_template.replace("end-points", api_uri)
telegram.send_message(
msg,
chat_id,
parse_mode="Markdown",
disable_web_page_preview=True,
reply_to_message_id=msg_id,
)
elif msg == "/help_send_file":
msg = help_send_file_template.replace("end-points", api_uri)
telegram.send_message(
msg,
chat_id,
parse_mode="Markdown",
disable_web_page_preview=True,
reply_to_message_id=msg_id,
)
elif msg == "/help":
telegram.send_message(
help_template, chat_id, parse_mode="Html", reply_to_message_id=msg_id
)
return "OK"
@post("/h/<token>")
def notify_handler(token):
try:
data = request.body.read().decode("utf-8")
data = json.loads(data)
chat_id = int(fernet.decrypt(token.encode()).decode())
if "text" in data:
return telegram.send_message(data["text"], chat_id)
if "file" in data:
if "caption" in data["file"]:
return telegram.send_file(
data["file"]["name"],
data["file"]["content"],
chat_id,
data["file"]["caption"],
)
else:
return telegram.send_file(
data["file"]["name"], data["file"]["content"], chat_id
)
except BaseException as e:
return {"ok": False, "ServerError": str(e)}
bottle.debug(True)
app = application = bottle.default_app()
if __name__ == "__main__":
bottle.run(host="0.0.0.0", port=80, reloader=True)