yuripeyamashita
commited on
Commit
•
6a17841
1
Parent(s):
ca51d48
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,63 @@
|
|
|
|
1 |
from flask import Flask, request
|
2 |
-
from gradio_client import Client
|
3 |
-
from uuid import uuid4
|
4 |
-
import os, json, requests, hashlib
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
-
secret = os.environ.get("auth")
|
9 |
-
headers = {'Authorization': os.environ.get("auth")}
|
10 |
-
|
11 |
-
res_dict = {}
|
12 |
-
|
13 |
@app.route('/', methods=['GET'])
|
14 |
def index():
|
15 |
return {}, 200
|
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 |
if __name__ == "__main__":
|
|
|
1 |
+
import os, json, requests
|
2 |
from flask import Flask, request
|
|
|
|
|
|
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
|
|
|
|
|
|
|
|
|
|
|
6 |
@app.route('/', methods=['GET'])
|
7 |
def index():
|
8 |
return {}, 200
|
9 |
|
10 |
+
LINE_REPLY_URL = "https://api.line.me/v2/bot/message/reply"
|
11 |
+
LINE_CHANNEL_ACCESS_TOKEN = os.environ.get("LINE_CHANNEL_ACCESS_TOKEN")
|
12 |
+
|
13 |
+
LINE_REPLY_HEADERS = {
|
14 |
+
"Content-Type": "application/json; charset=UTF-8",
|
15 |
+
"Authorization": "Bearer " + LINE_CHANNEL_ACCESS_TOKEN
|
16 |
+
}
|
17 |
+
|
18 |
+
@app.route("/api/", methods=["POST"])
|
19 |
+
def api():
|
20 |
+
try:
|
21 |
+
payload = get_payload_dict(request.get_json())
|
22 |
+
print(payload)
|
23 |
+
# res_text = opai.private_translate_api(payload.get("msg_text"))
|
24 |
+
# send_text(payload.get("token"), res_text)
|
25 |
+
except:
|
26 |
+
pass
|
27 |
+
return "", 200
|
28 |
+
|
29 |
+
|
30 |
+
def get_payload_dict(raw_payload):
|
31 |
+
print(raw_payload)
|
32 |
+
token = raw_payload.get("events", [{}])[0].get("replyToken")
|
33 |
+
sender_type = raw_payload.get("events", [{}])[0].get("source", {}).get("type", "user")
|
34 |
+
sender_id = raw_payload.get("events", [{}])[0].get("source", {}).get(str(sender_type+"Id"))
|
35 |
+
msg_type = raw_payload.get("events", [{}])[0].get("message", {}).get("type")
|
36 |
+
msg_id = raw_payload.get("events", [{}])[0].get("message", {}).get("id")
|
37 |
+
msg_text = raw_payload.get("events", [{}])[0].get("message", {}).get("text")
|
38 |
+
postback_data = raw_payload.get("events", [{}])[0].get("postback", {}).get("data")
|
39 |
+
|
40 |
+
payload = {"token": token,
|
41 |
+
"sender_type": sender_type,
|
42 |
+
"sender_id": sender_id,
|
43 |
+
"msg_type": msg_type,
|
44 |
+
"msg_id": msg_id,
|
45 |
+
"msg_text": msg_text,
|
46 |
+
"postback_data": postback_data
|
47 |
+
}
|
48 |
+
return payload
|
49 |
+
|
50 |
+
|
51 |
+
def send_text(token, text):
|
52 |
+
res = requests.post(LINE_REPLY_URL, headers=LINE_REPLY_HEADERS, json={
|
53 |
+
"replyToken": token,
|
54 |
+
"messages": [
|
55 |
+
{
|
56 |
+
"type": "text",
|
57 |
+
"text": text
|
58 |
+
}
|
59 |
+
]
|
60 |
+
})
|
61 |
|
62 |
|
63 |
if __name__ == "__main__":
|