-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
168 lines (128 loc) · 4.84 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
168
import os
import sys
import requests
import aiml
from datetime import datetime
from flask import Flask, request, abort
from linebot import ( LineBotApi, WebhookHandler )
from linebot.exceptions import ( InvalidSignatureError )
from linebot.models import( MessageEvent, TextMessage, TextSendMessage )
app = Flask(__name__)
# channel_access_token = os.environ['LINE_CHANNEL_ACCESS_TOKEN']
# channel_secret = os.environ['LINE_CHANNEL_SECRETE']
# get channel_secret and channel_access_token from your environment variable
channel_secret = os.getenv('LINE_CHANNEL_SECRET', None)
channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', None)
if channel_secret is None:
print("Specify line-channel secret as environmental variable")
sys.exit(1)
if channel_access_token is None:
print("Specify Line-Channel-Access-Token as environmental variable")
sys.exit(1)
line_bot_api = LineBotApi(channel_access_token)
handler = WebhookHandler(channel_secret)
trash_schedule = {
0: "燃えないゴミ ・ 有害危険ゴミ",
1: "燃えるゴミ",
2: "資源物1類 ビン・カン・ペットボトル",
3: "捨てられるゴミはありません",
4: "燃えるゴミ",
5: "捨てられるゴミはありません",
6: "資源回収(資源2類)古着・新聞紙・雑誌"
}
@app.route("/")
def hello_world():
#return app.send_static_file('./index.html')
return "Hello world"
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
print(request.headers)
signature = request.headers['X-Line-Signature']
print("signature " + signature)
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
# push notification
@app.route('/push_trash', methods=['GET'])
def push_trash():
return_text = "ーー今日 捨てられるゴミーー\n"
# 0 : Mon
# 1 : Tue
# 2 : Web
# 3 : Thu
# 4 : Fri
# 5 : Sat
# 6 : Sun
"""trash_schedule = {
0: "燃えないゴミ ・ 有害危険ゴミ",
1: "燃えるゴミ",
2: "資源物1類 ビン・カン・ペットボトル",
3: "捨てられるゴミはありません",
4: "燃えるゴミ",
5: "捨てられるゴミはありません",
6: "資源回収(資源2類)古着・新聞紙・雑誌"
}"""
weekday = datetime.now().weekday()
return_text += trash_schedule[weekday]
to = "Ua21ed0b7a6399fad17ba2adfd9efaa03"
line_bot_api.push_message(to, TextSendMessage(text=return_text))
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
text = " "
weekday = datetime.now().weekday()
if event.message.text == "今日":
text = trash_schedule[weekday]
elif event.message.text == "明日":
text = trash_schedule[weekday + 1]
elif event.message.text == "明後日":
text = trash_schedule[weekday + 2]
else:
print(event)
trash_name = event.message.text
query = 'trash_name:' + trash_name
params = (
('indent', 'on'),
('q', query),
('wt', 'json'),
)
# get request to solr
# response = requests.get('http://4d8d496e.ngrok.io/solr/trash/select', params=params)
response = requests.get('https://us-west-1.websolr.com/solr/219d367e048f/select', params=params)
# Original curl command
# curl http://localhost:8983/solr/trash/select?indent=on&q=trash_name:雨戸&wt=json
# response = requests.get('http://localhost:8983/solr/trash/select?indent=on&q=trash_name:雨戸&wt=json')
if response.status_code == 200:
search_result = response.json()
docs = search_result['response']['docs']
print(search_result)
for doc in docs:
text += doc['trash_name'] + "\n"
print(doc['trash_name'])
text += "分別カテゴリー : " + doc['category'] + "\n"
if doc['method'] == " ":
text += "捨てる方法 : " + "普通の家庭ごみとしてゴミ捨て場に!"
else:
text += "捨てる方法 : " + doc['method']
print(text)
else:
print("Error: " + response.status_code)
if text == " ":
text += '見つけられませんでした。'
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text))
if __name__ == "__main__":
port = int(os.getenv('PORT', 5000))
app.run(debug=True, host='0.0.0.0',
port=port)
print("app is running")
# useful site
#https://curl.trillworks.com/#