-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm.py
207 lines (171 loc) · 7.53 KB
/
fsm.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from transitions.extensions import GraphMachine
import global_config
import send
zone = ['東區', '南區', '北區', '安南區', '安平區', '中西區', '新營區', '鹽水區', '白河區', '柳營區', '後壁區', '東山區',
'麻豆區', '下營區', '六甲區', '官田區', '大內區', '佳里區', '學甲區', '西港區', '七股區', '將軍區', '北門區',
'新化區', '善化區', '新市區', '安定區', '山上區', '玉井區', '楠西區', '南化區', '左鎮區', '仁德區', '歸仁區',
'關廟區', '龍崎嶇', '永康區']
yes = ['想', '好', '是', 'yes', 'Yes', '要']
no = ['不想', '不好', '不是', 'no', 'No', '不要', '不']
class TocMachine(GraphMachine):
def __init__(self, **machine_configs):
self.machine = GraphMachine(
model=self,
**machine_configs
)
def is_going_to_state1(self, event):
if event.get("message"):
if event['message'].get('text'):
text = event['message']['text']
if text.find("天氣") == 0:
return True
else:
# print(text)
return False
# def not_going_to_state1(self, event):
# if event.get("message"):
# text = event['message']['text']
# if text.find("天氣") == 0:
# return False
# else:
# print(text)
# return True
def is_going_to_state2(self, event):
if event.get("message"):
if event['message'].get('text'):
text = event['message']['text']
sender_id = event['sender']['id']
if text in zone:
global_config.set_zone(sender_id, text)
return True
elif text.find('區') == 0:
global_config.set_zone(event['sender']['id'], '東區') # 預設
return True
else:
return False
def is_going_to_state3(self, event):
if event.get("message"):
if event['message'].get('text'):
text = event['message']['text']
if text.find("現在") == 0:
return True
else:
return False
def is_going_to_state4(self, event):
if event.get("message"):
if event['message'].get('text'):
text = event['message']['text']
if text.find("一週") == 0 or text.find("未來") == 0:
return True
else:
return False
def is_going_to_state4_2(self, event):
if event.get("message"):
if event['message'].get('text'):
text = event['message']['text']
if text in yes:
return True
else:
return False
def not_going_to_state4_2(self, event):
if event.get("message"):
if event['message'].get('text'):
text = event['message']['text']
if text in no:
return True
else:
return False
def is_going_to_state5(self, event):
if event.get("message"):
if event['message'].get('text'):
text = event['message']['text']
if text.find("溫度") == 0:
return True
else:
return False
def is_going_to_state6(self, event):
if event.get("message"):
if event['message'].get('text'):
text = event['message']['text']
if text.find("降雨") == 0:
return True
else:
return False
def is_going_to_state7(self, event):
if event.get("message"):
if event['message'].get('text'):
text = event['message']['text']
if text.find("天氣") == 0:
return True
else:
return False
def is_going_to_state9(self, event):
if event.get("message"):
if event['message'].get('text'):
text = event['message']['text']
if text.find("結束") == 0:
return True
else:
return False
def on_enter_state_init(self, event):
print("I'm entering state init")
sender_id = event['sender']['id']
global_config.set_state(sender_id,'state_init')
def on_enter_start_state0(self, event):
print("I'm entering state0")
sender_id = event['sender']['id']
global_config.set_state(sender_id,'start_state0')
send.send_start(sender_id,"嗨~\n有什麼想問的嗎?")
def on_enter_ask_zone_state1(self, event):
print("I'm entering state1")
sender_id = event['sender']['id']
global_config.set_state(sender_id,'ask_zone_state1')
send.send_start(sender_id,"你想知道台南的哪個地區呢?\n")
def on_enter_ask_interval_state2(self, event):
print("I'm entering state2")
sender_id = event['sender']['id']
user_zone = global_config.get_zone(sender_id)
send.send_start(sender_id,"你選的是 {0}".format(user_zone))
global_config.set_state(sender_id,'ask_interval_state2')
send.send_quick_replies(sender_id,"你想問\"現在\"還是未來\"一週\"的天氣?", "state2")
def on_enter_realtime_state3(self, event):
print("I'm entering state3")
sender_id = event['sender']['id']
global_config.set_state(sender_id,'realtime_state3')
send.send_quick_replies(sender_id,"你想知道\"溫度\"還是\"降雨機率\"還是\"天氣狀態\"?", "state3")
def on_enter_oneweek_state4(self, event):
print("I'm entering state4")
sender_id = event['sender']['id']
global_config.set_state(sender_id,'oneweek_state4')
send.forecast_1week(sender_id, global_config.get_zone(sender_id))
self.go_to(event)
def on_enter_ask_temp_state5(self, event):
print("I'm entering state5")
sender_id = event['sender']['id']
global_config.set_state(sender_id,'ask_temp_state5')
send.forecast_3day_temp(sender_id, global_config.get_zone(sender_id))
self.go_to(event)
def on_enter_ask_rain_state6(self, event):
print("I'm entering state6")
sender_id = event['sender']['id']
global_config.set_state(sender_id,'ask_rain_state6')
send.forecast_3day(sender_id, global_config.get_zone(sender_id), 'PoP6h')
self.go_to(event)
def on_enter_ask_pheno_state7(self, event):
print("I'm entering state7")
sender_id = event['sender']['id']
global_config.set_state(sender_id,'ask_pheno_state7')
send.forecast_3day(sender_id, global_config.get_zone(sender_id), 'Wx')
self.go_to(event)
def on_enter_ask_oneweek_state8(self, event):
print("I'm entering state8")
sender_id = event['sender']['id']
global_config.set_state(sender_id,'ask_oneweek_state8')
send.send_start(sender_id,"你還想知道未來一週的天氣嗎?")
def on_enter_finish_state9(self, event):
print("I'm entering state9")
sender_id = event['sender']['id']
global_config.set_state(sender_id,'finish_state9')
send.send_start(sender_id,"歡迎下次再來詢問天氣~")
send.send_button_message(sender_id,"氣象開放資料平台")
self.go_back(event)