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
46 lines (40 loc) · 1.38 KB
/
app.py
File metadata and controls
46 lines (40 loc) · 1.38 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
from flask import Flask, jsonify, request
import os
import json
import requests
app = Flask(__name__)
@app.route('/')
def index():
a=os.environ['Authorization']
return "นายสิทธิกร อุติ เลขที่6 ชั้นม.4/3"
@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['originalDetectIntentRequest']['payload']['data']['replyToken']
userText = decoded['queryResult']['intent']['displayName']
if (userText == 'สวัสดี') :
sendText(user,'ดี')
elif (userText == 'บาย') :
sendText(user,'เคๆ')
else :
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()