forked from arkhoms/lineBot2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
69 lines (61 loc) · 2.22 KB
/
app.py
File metadata and controls
69 lines (61 loc) · 2.22 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
from flask import Flask, jsonify, request
import os
import json
import requests
app = Flask(__name__)
@app.route('/')
def index():
a=os.environ['Authorization']
try:
f = open("student.csv", "r")
for line in f.readlines():
# print(line)
a = line.split(",")
if(a[0]=="21007"):
return a[4]
f.close()
except Exception:
return "Could not read to file"
return "นายอาคม สุวรรณประเสริฐ เลขที่ 0 ชั้น ม.4/"
@app.route("/webhook", methods=['POST'])
def webhook():
if request.method == 'POST':
return "OK"
@app.route('/callback', methods=['POST'])
def callback():
json_line = request.get_json()
json_line = json.dumps(json_line)
decoded = json.loads(json_line)
# user = decoded["events"][0]['replyToken']
# userText = decoded["events"][0]['message']['text']
user = decoded['originalDetectIntentRequest']['payload']['data']['replyToken']
userText = decoded['queryResult']['intent']['displayName']
userAction = decoded['queryResult']['parameters']['studentId']
if(userText=="ถามชื่อ"):
try:
f = open("student.csv", "r")
for line in f.readlines():
a = line.split(",")
if(userAction==a[0]):
# nameList=nameList+", "+a[4]
sendText(user,a[4])
f.close()
# sendText(user,nameList)
except Exception:
sendText(user,"ขออภัย..ไม่สามารถเปิดไฟล์ได้")
elif(userText=="ไอ้บ้า"):
sendText(user,"ไม่บ้านะ")
return '',200
def sendText(user, text):
LINE_API = 'https://api.line.me/v2/bot/message/reply'
headers = {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': os.environ['Authorization'] # ตั้ง Config vars ใน heroku พร้อมค่า Access token
}
data = json.dumps({
"replyToken":user,
"messages":[{"type":"text","text":text}]
})
r = requests.post(LINE_API, headers=headers, data=data) # ส่งข้อมูล
if __name__ == '__main__':
app.run()