This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
generated from NCKU-CCS/TOC-Project-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm.py
282 lines (249 loc) · 12.5 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from transitions import Machine
from linebot.models import ImageSendMessage, ImageCarouselColumn, URITemplateAction, MessageTemplateAction, TemplateSendMessage,ImageCarouselTemplate,FlexSendMessage
from API.Stock import TaiwanStock, USAStock, CryptoStock
from API.Flex_message import Flex_Message_Crypto, TW_Stock_Each
from API.SimpleMovingAverage import SimpleMovingAverage as SMA
import json
from utils import send_text_message, send_button_message_NoneURL, send_Multi_Image, send_Flex_message, CountDay,send_button_message_URL
class TocMachine(Machine):
def __init__(self, **machine_configs):
self.machine = Machine(model=self, **machine_configs)
self.TW_STOCK = TaiwanStock()
self.US_STOCK = USAStock()
self.CRYPTO_STOCK = CryptoStock()
# State 1 #
def is_going_to_Start(self, event):
return True
def is_going_to_Search(self, event):
text = event.message.text
return text.lower() == "查詢"
def is_going_to_Crypto(self, event):
text = event.message.text
return text.lower() == "虛擬貨幣"
def is_going_to_Crypto_Fetch(self, event):
text = event.message.text
return True
def is_going_to_Return_Crypto(self, event):
return event.message.text.lower() == "繼續搜尋"
def is_going_to_Return_Start(self, event):
return event.message.text.lower() == "探索" or event.message.text.lower() == "離開"
def is_going_to_Stock(self, event):
text = event.message.text
return text.lower() == "股票" or text.lower() == "繼續查詢"
def is_going_to_Stock_US(self, event):
text = event.message.text
return text.lower() == "美股"
def is_going_to_Stock_US_Index(self,event):
return event.message.text == "美股指數"
def is_going_to_Stock_US_Individual(self,event):
return event.message.text == "美國個別股"
def is_going_to_Stock_US_Index_four(self,event):
text = event.message.text
print(text)
return text == 'GSPC' or text == 'IXIC' or text == 'NDX' or text == 'DJI'
def is_going_to_Stock_US_Fetch(self,event):
return True
def is_going_to_Stock_TW(self, event):
text = event.message.text
return text.lower() == "台股"
def is_going_to_Stock_TW_Fetch(self, event):
if event.message.text != 'More':
return True
return False
def is_going_to_Stock_TW_Fetch_2_End(self, event):
return event.message.text.lower() == "離開"
def is_going_to_TodayHigh(self, event):
text = event.message.text
return text.lower() == "平均線"
def on_enter_Start(self, event):
print("I'm entering Search")
actions=[
MessageTemplateAction(label='今日股市',text='查詢'),
MessageTemplateAction(label='近期股市分析',text='股票分析')]
send_button_message_NoneURL(
reply_token = event.reply_token,
title='請問需要甚麼服務',
text='選擇功能',
btn = actions,
)
def on_enter_Search(self, event):
reply_token = event.reply_token
actions=[
MessageTemplateAction(label='Crypto',text='虛擬貨幣'),
MessageTemplateAction(label='Stock',text='股票')]
send_button_message_NoneURL(
reply_token = event.reply_token,
title='請選擇幣別',
text='虛擬貨幣(Crypto Currency)\n股票(Stock)',
btn = actions,)
def on_enter_Crypto(self, event):
message = TemplateSendMessage(
alt_text='Carousel template',
template = ImageCarouselTemplate(
columns=[
ImageCarouselColumn(
image_url='https://i.imgur.com/bVKzgFV.png',
action=MessageTemplateAction(label='BTC',text='BTC'),),
ImageCarouselColumn(
image_url='https://i.imgur.com/UOqQuCW.png',
action=MessageTemplateAction(label='BAT',text='BAT'),),
ImageCarouselColumn(
image_url='https://i.imgur.com/H54bizg.png',
action=MessageTemplateAction(label='ETH',text='ETH'),),
ImageCarouselColumn(
image_url='https://i.imgur.com/qRIxXS7.png',
action=MessageTemplateAction(label='BNB',text='BNB'),),
ImageCarouselColumn(
image_url='https://i.imgur.com/z2uYzbT.png',
action=MessageTemplateAction(label='BTC Cash',text='BCH'),),
ImageCarouselColumn(
image_url='https://i.imgur.com/my1xVPD.png',
action=MessageTemplateAction(label='Dogecoin',text='DOGE'),),
ImageCarouselColumn(
image_url='https://i.imgur.com/KHX1Rae.png',
action=MessageTemplateAction(label='SHIB',text='SHIB'),),
]
)
)
send_Multi_Image(reply_token = event.reply_token,message = message)
def on_enter_Crypto_Fetch(self, event):
datum = self.CRYPTO_STOCK.getJson(event.message.text)
reply_token = event.reply_token
send_Flex_message(event.reply_token,Flex_Message_Crypto(event.message.text, datum))
def on_enter_Stock(self, event):
reply_token = event.reply_token
actions=[
MessageTemplateAction(label='台灣股市',text='台股'),
MessageTemplateAction(label='美國股市',text='美股')]
send_button_message_NoneURL(
reply_token = event.reply_token,
title='請選擇',
text='台灣股市/美國股市',
btn = actions,)
def on_enter_Stock_US(self, event):
reply_token = event.reply_token
actions=[
MessageTemplateAction(label='美股指數',text='美股指數'),
MessageTemplateAction(label='個別股票',text='美國個別股')]
send_button_message_NoneURL(
reply_token = event.reply_token,
title='請問需要甚麼服務',
text='選擇功能',
btn = actions,
)
def on_enter_Stock_US_Index(self, event):
message = json.load(open('Flex/US_Stock.json','r',encoding='utf-8'))
send_Flex_message(event.reply_token,message)
def on_enter_Stock_US_Individual(self,event):
message = json.load(open('Flex/US_Stock_Individual.json','r',encoding='utf-8'))
send_Flex_message(event.reply_token,message)
def on_enter_Stock_US_Fetch(self, event):
datum = self.US_STOCK.getJson(event.message.text)
send_Flex_message(event.reply_token,TW_Stock_Each(url="https://i.imgur.com/RQMpQyo.png",datum=datum))
def on_enter_Stock_US_Index_four(self,event):
datum = self.US_STOCK.getJson(event.message.text)
send_Flex_message(event.reply_token,TW_Stock_Each(url="https://i.imgur.com/RQMpQyo.png",datum=datum))
def on_enter_Stock_TW(self, event):
message = json.load(open('Flex/TW_Stock.json','r',encoding='utf-8'))
send_Flex_message(event.reply_token,message)
def on_enter_Stock_TW_Fetch(self,event):
datum = self.TW_STOCK.getJson(event.message.text)
send_Flex_message(event.reply_token,TW_Stock_Each(url="https://i.imgur.com/e7yvQGQ.png",datum=datum))
def is_going_to_SMA(self,event):
return event.message.text.lower() == "股票分析"
def on_enter_SMA(self,event):
message = json.load(open('Flex/TW_SAM.json','r',encoding='utf-8'))
send_Flex_message(event.reply_token,message)
def is_going_to_SMA_S(self,event):
return event.message.text.lower() == "台股短線"
def on_enter_SMA_S(self,event):
message = json.load(open('Flex/TW_Stock.json','r',encoding='utf-8'))
send_Flex_message(event.reply_token,message)
def is_going_to_SMA_M(self,event):
return event.message.text.lower() == "台股中線"
def on_enter_SMA_M(self,event):
message = json.load(open('Flex/TW_Stock.json','r',encoding='utf-8'))
send_Flex_message(event.reply_token,message)
def is_going_to_SMA_L(self,event):
return event.message.text.lower() == "台股長線"
def on_enter_SMA_L(self,event):
message = json.load(open('Flex/TW_Stock.json','r',encoding='utf-8'))
send_Flex_message(event.reply_token,message)
def is_going_to_SMA_S_US(self,event):
return event.message.text.lower() == "美股短線"
def on_enter_SMA_S_US(self,event):
message = json.load(open('Flex/US_Stock_Individual.json','r',encoding='utf-8'))
send_Flex_message(event.reply_token,message)
def is_going_to_SMA_M_US(self,event):
return event.message.text.lower() == "美股中線"
def on_enter_SMA_M_US(self,event):
message = json.load(open('Flex/US_Stock_Individual.json','r',encoding='utf-8'))
send_Flex_message(event.reply_token,message)
def is_going_to_SMA_L_US(self,event):
return event.message.text.lower() == "美股長線"
def on_enter_SMA_L_US(self,event):
message = json.load(open('Flex/US_Stock_Individual.json','r',encoding='utf-8'))
send_Flex_message(event.reply_token,message)
def is_going_to_S_Stock(self,event):
if self.TW_STOCK.getJson(event.message.text)["當日最高"] == "-":
return False
self.sma = SMA(stock=event.message.text+".TW",stype="short",start=CountDay(60),end=CountDay(0))
return True
def on_enter_S_Stock(self,event):
url = self.sma.upload_url()
message = ImageSendMessage(original_content_url=url,preview_image_url=url)
send_Multi_Image(event.reply_token,message)
def is_going_to_M_Stock(self,event):
if self.TW_STOCK.getJson(event.message.text)["當日最高"] == "-":
return False
self.sma = SMA(stock=event.message.text+".TW",stype="medium",start=CountDay(240),end=CountDay(0))
return True
def on_enter_M_Stock(self,event):
url = self.sma.upload_url()
message = ImageSendMessage(original_content_url=url,preview_image_url=url)
send_Multi_Image(event.reply_token,message)
def is_going_to_L_Stock(self,event):
if self.TW_STOCK.getJson(event.message.text)["當日最高"] == "-":
return False
self.sma = SMA(stock=event.message.text+".TW",stype="long",start=CountDay(480),end=CountDay(0))
return True
def on_enter_L_Stock(self,event):
url = self.sma.upload_url()
message = ImageSendMessage(original_content_url=url,preview_image_url=url)
send_Multi_Image(event.reply_token,message)
def is_going_to_S_Stock_US(self,event):
if self.US_STOCK.getJson(event.message.text)["當日最高"] == "-":
return False
self.sma = SMA(stock=event.message.text,stype="short",start=CountDay(60),end=CountDay(0))
return True
def on_enter_S_Stock_US(self,event):
url = self.sma.upload_url()
message = ImageSendMessage(original_content_url=url,preview_image_url=url)
send_Multi_Image(event.reply_token,message)
def is_going_to_M_Stock_US(self,event):
if self.US_STOCK.getJson(event.message.text)["當日最高"] == "-":
return False
self.sma = SMA(stock=event.message.text,stype="medium",start=CountDay(240),end=CountDay(0))
return True
def on_enter_M_Stock_US(self,event):
url = self.sma.upload_url()
message = ImageSendMessage(original_content_url=url,preview_image_url=url)
send_Multi_Image(event.reply_token,message)
def is_going_to_L_Stock_US(self,event):
if self.US_STOCK.getJson(event.message.text)["當日最高"] == "-":
return False
self.sma = SMA(stock=event.message.text,stype="long",start=CountDay(480),end=CountDay(0))
return True
def on_enter_L_Stock_US(self,event):
url = self.sma.upload_url()
message = ImageSendMessage(original_content_url=url,preview_image_url=url)
send_Multi_Image(event.reply_token,message)
def is_going_to_Restart_SMA(self,event):
return event.message.text.lower() != "股票分析" and event.message.text.lower() != "進階"
# def is_going_to_Stock_Analysis(self,event):
# return event.message.text.lower() == "進階"
#
# def on_enter_Stock_Analysis(self,event):
# url = self.sma.upload_url()
# message = ImageSendMessage(original_content_url=url,preview_image_url=url)
# send_Multi_Image(event.reply_token,message)