-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (38 loc) · 1.36 KB
/
Copy pathmain.py
File metadata and controls
51 lines (38 loc) · 1.36 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
from typing import Any
from functools import partial
from flask import Request, Response, abort
import functions_framework # type: ignore
from database import UserTable, DictionaryTable
from router import RequestRouter
from telegram import TelegramBot, ParseMode
app = RequestRouter()
bot = TelegramBot.from_config()
user_table = UserTable.from_config()
dictionary_table = DictionaryTable.from_config()
app_url = "http://t.me/PracticeTurkishBot/quiz"
def send_to_telegram(user_id: int, text: str) -> None:
print(text)
index = dictionary_table.register_dictionary(text)
text += f'\n\n<a href="{app_url}?startapp={index}"><b>Practice!</b></a>'
bot.send_text(user_id, text, ParseMode.HTML)
@app.route("/", "POST")
def redirect(data: dict[str, Any]) -> Response:
user_id = data["user id"]
token = data["token"]
if not user_table.check_token(user_id, token):
return abort(403)
text = data["text"]
response = Response("Message successfully sent!", 200)
response.call_on_close(partial(send_to_telegram, user_id, text))
return response
@app.route("/", "GET")
def status(_: dict[str, Any]) -> Response:
return Response(
"""<title>PracticeTurkishBot</title>
<H1>The bot is online</H1>
""",
200,
)
@functions_framework.http
def RedirectFunction(request: Request) -> Response:
return app.dispatch(request)