def handle_message(event): ###訊息傳遞功能 ### 抓到顧客的資料 ### profile = line_bot_api.get_profile(event.source.user_id) uid = profile.user_id #使用者ID usespeak = event.message.text #取得使用者訊息 #####################################系統功能按鈕############################## if re.match('[0-9]{4}[<>][0-9]', usespeak): # 先判斷是否是使用者要用來存股票的 0到9的資料輸入4個,內容要0到9 mongodb.write_user_stock_fountion(stock=usespeak[0:4], bs=usespeak[4:5], price=usespeak[5:]) line_bot_api.push_message(uid, TextSendMessage(usespeak[0:4] + '已經儲存成功')) return 0 elif re.match('刪除[0-9]{4}', usespeak): # 刪除存在資料庫裡面的股票 mongodb.delete_user_stock_fountion(stock=usespeak[2:]) line_bot_api.push_message(uid, TextSendMessage(usespeak + '已經刪除成功')) return 0 elif re.match('我愛你', usespeak): # 刪除存在資料庫裡面的股票 # mongodb.delete_user_stock_fountion(stock=usespeak[2:]) line_bot_api.push_message(uid, TextSendMessage('我愛你一生一世')) return 0 elif re.match('[0-9]{4}', usespeak): # 如果只有給四個數字就判斷是股票查詢 line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) ######### 開始請求網站,報價 ########## url = 'https://tw.stock.yahoo.com/q/q?s=' + message # 要請求的網址 list_req = requests.get(url) #請求 soup = BeautifulSoup(list_req.content, "html.parser") # 取得所有網站內容 getstock = soup.find('b').text # 拉出股價 line_bot_api.push_message(yourid, TextSendMessage(text=getstock)) #推訊息出去瞜 return 0 elif re.match('我的股票', usespeak): # 秀出所有自動推撥的股票 get = mongodb.show_user_stock_fountion() msg = '' if len(get) > 0: for i in get: msg += i['stock'] + " " + i['bs'] + " " + str( i['price']) + '\n' line_bot_api.push_message(uid, TextSendMessage(msg)) else: line_bot_api.push_message(uid, TextSendMessage('沒有資料')) return 0 else: # 都找不到就回答看不懂 line_bot_api.push_message(uid, TextSendMessage(usespeak + '是啥? 看不懂')) return 0
def job(): data = mongodb.show_user_stock_fountion() for i in data: stock = i['stock'] bs = i['bs'] price = i['price'] #yahoo的 要使用list_req.text url = 'https://tw.stock.yahoo.com/q/q?s=' + stock list_req = requests.get(url, headers=headers) try: #要使用try catch 不然爬蟲更新價格時有時還沒有更新會抓不到資料會有error #要使用list_req.text 不是使用list_req.content不然會有亂碼 soup = BeautifulSoup(list_req.text, "html.parser") tables = soup.find_all('table')[1] #裡面所有文字內容 table1 = soup.find_all('table')[2] a = table1.find_all("a")[0].text[4:] #股票名稱 tds = tables.find_all("td")[3] getstock = tds.find('b').text if float(getstock): if bs == '<': if float(getstock) < price: get = stock + a + ' 的價格:' + str( getstock) + ' 已低於您設定的價格' + str( price) + '元,' + '即可買入!!' #line_bot_api.push_message(yourid, TextSendMessage(text=get)) print(get) else: if float(getstock) > price: get = stock + a + ' 的價格:' + str( getstock) + ' 已高於您設定的價格' + str( price) + '元,' + '即可賣出!!' #line_bot_api.push_message(yourid, TextSendMessage(text=get)) print(get) else: #line_bot_api.push_message(yourid, TextSendMessage(text='這個有問題')) print("有問題") except IndexError: pass
def job(): # 執行時間測試,確認時間有在執行 若想確認是否有跑排程 打開以下註記 # linenotify.lineNotifyMessage(token, msg='執行時間:' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) data = mongodb.show_user_stock_fountion() for i in data: stock = i['stock'] bs = i['bs'] price = i['price'] url = 'https://tw.stock.yahoo.com/q/q?s=' + stock list_req = requests.get(url) soup = BeautifulSoup(list_req.content, "html.parser") getstock = soup.findAll('b')[1].text # 裡面所有文字內容 if float(getstock): if bs == '<': if float(getstock) < price: get = stock + '(可買進)的價格:' + getstock linenotify.lineNotifyMessage(token, get) else: if float(getstock) > price: get = stock + '(可賣出)的價格:' + getstock linenotify.lineNotifyMessage(token, get) else: linenotify.lineNotifyMessage(token, msg='抓取有問題異常')
def handle_message(event): ### 抓到顧客的資料 ### profile = line_bot_api.get_profile(event.source.user_id) nameid = profile.display_name #使用者名稱 uid = profile.user_id #使用者ID ### 如果這個使用者沒有紀錄的話,就記錄他 ### if mongodb.show_user(uid): mongodb.write_user(nameid, uid) ### 把使用者的資料儲存下來 ### usespeak = str(event.message.text) #使用者講的話 #####################################系統功能按鈕############################## if re.match('[0-9]{4}[<>][0-9]', usespeak): # 先判斷是否是使用者要用來存股票的 mongodb.write_user_stock_fountion(stock=usespeak[0:4], bs=usespeak[4:5], price=usespeak[5:]) line_bot_api.push_message(uid, TextSendMessage(usespeak[0:4] + '已經儲存成功')) return 0 elif re.match('刪除[0-9]{4}', usespeak): # 刪除存在資料庫裡面的股票 mongodb.delete_user_stock_fountion(stock=usespeak[2:]) line_bot_api.push_message(uid, TextSendMessage(usespeak + '已經刪除成功')) return 0 elif re.match('[0-9]{4}', usespeak): # 如果只有給四個數字就判斷是股票查詢 mongodb.update_temporary_stock(uid, usespeak) url = 'https://tw.stock.yahoo.com/q/q?s=' + usespeak #請求網站 list_req = requests.get(url) #將整個網站的程式碼爬下來 soup = BeautifulSoup(list_req.content, "html.parser") #找到b這個標籤 getstock = soup.findAll('b')[1].text #抓到收盤價格 line_bot_api.push_message( uid, TextSendMessage(usespeak + '目前的價格是' + getstock)) return 0 elif re.match('我的股票', usespeak): # 秀出所有自動推撥的股票 get = mongodb.show_user_stock_fountion() msg = '' if len(get) > 0: for i in get: msg += i['stock'] + " " + i['bs'] + " " + str( i['price']) + '\n' line_bot_api.push_message(uid, TextSendMessage(msg)) return 0 else: line_bot_api.push_message(uid, TextSendMessage('沒有資料')) return 0 elif re.match('籌碼面分析', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥籌碼面分析 line_bot_api.push_message( uid, TextSendMessage(Institutional_Investors.stockII(usespeak))) return 0 elif re.match('KD圖', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥KD line_bot_api.push_message( uid, TextSendMessage(Technical_Analysis.stock_KD(usespeak))) return 0 elif re.match('MA圖', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥MA line_bot_api.push_message( uid, TextSendMessage(Technical_Analysis.stock_MA(usespeak))) return 0 elif re.match('MACD圖', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥MACD line_bot_api.push_message( uid, TextSendMessage(Technical_Analysis.stock_MACD(usespeak))) return 0 elif re.match('OBV圖', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥OBV line_bot_api.push_message( uid, TextSendMessage(Technical_Analysis.stock_OBV(usespeak))) return 0 elif re.match('威廉圖', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥威廉 line_bot_api.push_message( uid, TextSendMessage(Technical_Analysis.stock_William(usespeak))) return 0 elif re.match('ATR圖', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥ATR line_bot_api.push_message( uid, TextSendMessage(Technical_Analysis.stock_ATR(usespeak))) return 0 elif re.match('ADX圖', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥ADX line_bot_api.push_message( uid, TextSendMessage(Technical_Analysis.stock_ADX(usespeak))) return 0 elif re.match('RSI圖', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥RSI line_bot_api.push_message( uid, TextSendMessage(Technical_Analysis.stock_RSI(usespeak))) return 0 elif re.match('MFI圖', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥MFI line_bot_api.push_message( uid, TextSendMessage(Technical_Analysis.stock_MFI(usespeak))) return 0 elif re.match('ROC圖', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥ROC line_bot_api.push_message( uid, TextSendMessage(Technical_Analysis.stock_ROC(usespeak))) return 0 elif re.match('毛利率大於90%', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥毛利率大於90% line_bot_api.push_message( uid, TextSendMessage('毛利率大於90%的股票:\n' + Fundamental_Analysis.gpm())) return 0 elif re.match('每股淨值大於100', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥每股淨值大於100 line_bot_api.push_message( uid, TextSendMessage('每股淨值大於100的股票:\n' + Fundamental_Analysis.pbr())) return 0 elif re.match('每股盈餘大於5', usespeak): usespeak = mongodb.cache_temporary_stock(uid) line_bot_api.push_message(uid, TextSendMessage('稍等一下, 雲端運算中...')) # 推撥每股盈餘大於5 line_bot_api.push_message( uid, TextSendMessage('每股盈餘大於5的股票:\n' + Fundamental_Analysis.eps())) return 0 # 傳送多重按鈕介面訊息 elif re.match('技術面分析', usespeak): message = TemplateSendMessage( alt_text='技術面分析(Technical Analysis)', template=CarouselTemplate(columns=[ CarouselColumn( thumbnail_image_url='https://i.imgur.com/7FwK6MA.png', title='技術面分析', text='Technical Analysis', actions=[ MessageTemplateAction(label='ROC圖', text='ROC圖'), MessageTemplateAction(label='MA圖', text='MA圖'), MessageTemplateAction(label='MACD圖', text='MACD圖') ]), CarouselColumn( thumbnail_image_url='https://imgur.com/9BlDjoF.png', title='技術面分析', text='Technical Analysis', actions=[ MessageTemplateAction(label='OBV圖', text='OBV圖'), MessageTemplateAction(label='威廉圖', text='威廉圖'), MessageTemplateAction(label='ATR圖', text='ATR圖') ]), CarouselColumn( thumbnail_image_url='https://imgur.com/OkpeEZ7.png', title='技術面分析', text='Technical Analysis', actions=[ MessageTemplateAction(label='ADX圖', text='ADX圖'), MessageTemplateAction(label='RSI圖', text='RSI圖'), MessageTemplateAction(label='MFI圖', text='MFI圖') ]) ])) line_bot_api.push_message(uid, message) return 0 elif re.match('基本面分析', usespeak): buttons_template = TemplateSendMessage( alt_text='基本面分析', template=ButtonsTemplate( title='基本面分析(Fundamental Analysis)', text='請選擇', actions=[ MessageTemplateAction(label='毛利率大於90%', text='毛利率大於90%'), MessageTemplateAction(label='每股淨值大於100', text='每股淨值大於100'), MessageTemplateAction(label='每股盈餘大於5', text='每股盈餘大於5'), ])) line_bot_api.push_message(uid, buttons_template) return 0 else: # 都找不到就回答看不懂 line_bot_api.push_message(uid, message) return 0
def handle_message(event): #取得顧客資訊 profile = line_bot_api.get_profile(event.source.user_id) uid = profile.user_id #使用者ID usespeak=str(event.message.text) #使用者講的話 if re.match('[0-9]{4,5}[<>][0-9]',usespeak): # 先判斷是否是使用者要用來存股票的 if ">" in usespeak: symbol=">" spilt_stock_info=usespeak.split(">") mongodb.write_user_stock_fountion(stock=spilt_stock_info[0], bs=symbol, price=spilt_stock_info[1]) line_bot_api.push_message(uid, TextSendMessage(spilt_stock_info[0]+'已經儲存成功')) return 0 else: symbol="<" spilt_stock_info=usespeak.split("<") mongodb.write_user_stock_fountion(stock=spilt_stock_info[0], bs=symbol, price=spilt_stock_info[1]) line_bot_api.push_message(uid, TextSendMessage(spilt_stock_info[0]+'已經儲存成功')) return 0 elif re.match('刪除[0-9]{4,5}',usespeak): # 刪除存在資料庫裡面的股票 mongodb.delete_user_stock_fountion(stock=usespeak[2:]) line_bot_api.push_message(uid, TextSendMessage(usespeak+'已經刪除成功')) return 0 elif re.match('測試',usespeak): # 取得id #line_bot_api.push_message(uid,TextSendMessage("你的id"+uid)) #line_bot_api.push_message(uid,TextSendMessage("測試")) #line_bot_api.reply_message(event.reply_token,"測試") line_bot_api.reply_message(event.reply_token,TextSendMessage(text="請輸入您的姓名")) #line_bot_api.push_message(uid,TextSendMessage("請輸入您的姓名")) #username=str(event.message.text) #if username=="test": #line_bot_api.reply_message(event.reply_token,TextSendMessage(text="已輸入"+username)) """ if re.match('[^0-9]',username): #if event.message.text !="": #line_bot_api.reply_message(uid,"請輸入您的token碼") line_bot_api.push_message(uid,TextSendMessage("請輸入您的token碼")) usertoken=str(event.message.text) if re.match('[^0-9]',usertoken): #if event.message.text !="": #usertoken=str(event.message.text) mongodb.write_user_information_fountion(uid,username,usertoken) """ return 0 elif re.match("姓名[^0-9]",usespeak): # 取得id data_list(name=usespeak[2:],token="") line_bot_api.reply_message(event.reply_token,TextSendMessage(text="請輸入您的token碼")) return 0 elif re.match("token[a-zA-Z0-9]",usespeak): # 取得id datalist=data_list(name="",token=usespeak[5:]) mongodb.write_user_information_fountion(uid,datalist[0],datalist[1]) line_bot_api.reply_message(event.reply_token,TextSendMessage(text="已新增 您的姓名:"+datalist[0]+"及token碼:"+datalist[1])) return 0 elif re.match('[0-9]{4,5}價格',usespeak): # 先判斷是否是使用者要用來存股票的 data = mongodb.show_user_stock_fountion() stock_price=[] for i in data: stock=i['stock'] bs=i['bs'] price=i['price'] stock_price.append(stock) spilt_stock_info=usespeak.split("價格") if spilt_stock_info[0] in stock_price: url = 'https://tw.stock.yahoo.com/q/q?s=' + spilt_stock_info[0] list_req = requests.get(url) soup = BeautifulSoup(list_req.text, "html.parser") tables=soup.find_all('table')[1] #裡面所有文字內容 table1=soup.find_all('table')[2] a=table1.find_all("a")[0].text[4:]#股票名稱 tds=tables.find_all("td")[3] getstock= tds.find('b').text getstock=float(getstock) get=str(spilt_stock_info[0])+a+' 的價格:' + str(getstock) line_bot_api.push_message(uid, TextSendMessage(get)) else: #print("查無此股票價格!!") line_bot_api.push_message(uid,TextSendMessage(spilt_stock_info[0]+"查無此股票價格!!")) return 0 elif re.match('買股票',usespeak): # 刪除存在資料庫裡面的股票 #line_bot_api.push_message(uid,TextSendMessage("請輸入你要的選股策略:")) #line_bot_api.reply_message(event.reply_token,TextSendMessage(text="請輸入你要的選股策略:")) #usespeakStrategy=str(event.message.text) #使用者講的話 if uid=="U0db823667b9edd2dfea67e380d87cf41": #選擇產業類股 Carousel_template=stock_template() line_bot_api.reply_message(event.reply_token,Carousel_template) return 0 else: line_bot_api.push_message(uid,TextSendMessage("無權限")) return 0
def handle_message(event): profile = line_bot_api.get_profile(event.source.user_id) uid = profile.user_id # 使用者ID usespeak = str(event.message.text) if re.match('[0-9]{4}[<>][0-9]', usespeak): # 先判斷是否是使用者要用來存股票的 mongodb.write_user_stock_fountion(stock=usespeak[0:4], bs=usespeak[4:5], price=usespeak[5:]) line_bot_api.push_message(uid, TextSendMessage(usespeak[0:4] + '已經儲存成功')) return 0 elif re.match('刪除[0-9]{4}', usespeak): # 刪除存在資料庫裡面的股票 mongodb.delete_user_stock_fountion(stock=usespeak[2:]) line_bot_api.push_message(uid, TextSendMessage(usespeak + '已經刪除成功')) return 0 ################################我的股票######################################## elif re.match('我的股票', usespeak): get = mongodb.show_user_stock_fountion() msg = '' if len(get) > 0: for i in get: msg += i['stock'] + " " + i['bs'] + " " + str( i['price']) + '\n' line_bot_api.push_message(uid, TextSendMessage(msg)) return 0 else: line_bot_api.push_message(uid, TextSendMessage('沒有資料')) return 0 ################################################################################ ################################標準差分析####################################### elif re.match('[0-9]{4}', usespeak): # 如果只有給四個數字就判斷是股票查詢 mongodb.update_temporary_stock(uid, usespeak) line_bot_api.push_message(uid, TextSendMessage('Boss請稍等我正在運算中...')) url = 'https://histock.tw/stock/' + usespeak + '/%E5%88%A9%E6%BD%A4%E6%AF%94%E7%8E%87' list_req = requests.get(url) soup = BeautifulSoup(list_req.content, "html.parser") table = soup.find("table", class_="tb-stock tbBasic") data_txt = table.text line_bot_api.push_message(yourID, TextSendMessage(data_txt + '\n')) # 推撥標準插圖 imgurl = Standard_Deviation.stockSD(usespeak) line_bot_api.push_message( uid, ImageSendMessage(original_content_url=imgurl, preview_image_url=imgurl)) line_bot_api.push_message( uid, TextSendMessage(Standard_Deviation.searchstock(usespeak))) return 0 ################################################################################ ################################黃金交叉######################################## ######################################### # 當短期5日線突破20日線 # # 股本6億~20億 # ######################################### elif event.message.text == '黃金交叉': line_bot_api.push_message(uid, TextSendMessage('Boss請稍等我正在運算中...')) elected = '' url = 'https://tw.screener.finance.yahoo.net/screener/ws?PickID=100205,100533,100534,100535,100536,100537&f=j&764' list_req = requests.get(url) soup = BeautifulSoup(list_req.content, "html.parser") getjson4 = json.loads(soup.text) for e in getjson4['items']: elected += e['symname'] + e['symid'] + '\n' #print(elected) if elected != '': line_bot_api.push_message( yourID, TextSendMessage(text="黃金交叉選股結果:\n" + elected)) else: line_bot_api.push_message(yourID, TextSendMessage(text="黃金交叉選股,沒有可以買的股票")) ################################################################################ ################################黃金交叉學生##################################### ######################################### # 當短期5日線突破20日線 # # 股本6億~20億 # # 價格在20塊以下 # ######################################### elif event.message.text == '學生選股': line_bot_api.push_message(uid, TextSendMessage('Boss請稍等我正在運算中...')) elected_student = '' url = 'https://tw.screener.finance.yahoo.net/screener/ws?PickID=100205,100533,100534,100535,100536,100537&f=j&764' list_req = requests.get(url) soup = BeautifulSoup(list_req.content, "html.parser") getjson = json.loads(soup.text) for e in getjson['items']: if e['close_price'] < '20': elected_student += e['symname'] + e['symid'] + '\n' #print(elected_student) if elected_student != '': line_bot_api.push_message( yourID, TextSendMessage(text="學生選股結果:\n" + elected_student)) else: line_bot_api.push_message(yourID, TextSendMessage(text="學生選股,沒有可以買的股票")) ################################################################################ ################################################################################ ########################################## # 外資連續買超6天 # # 股本2億~20億 # ######################################### elif event.message.text == '籌碼面選股': line_bot_api.push_message(uid, TextSendMessage('Boss請稍等我正在運算中...')) elected_chip_analysis = '' url = 'https://tw.screener.finance.yahoo.net/screener/ws?PickID=465,100533,100534,100535,100536,100537&f=j&47' list_req = requests.get(url) soup = BeautifulSoup(list_req.content, "html.parser") getjson2 = json.loads(soup.text) for e in getjson2['items']: elected_chip_analysis += e['symname'] + e['symid'] + '\n' #print(elected_student) if elected_chip_analysis != '': line_bot_api.push_message( yourID, TextSendMessage(text="籌碼面選股結果:\n" + elected_chip_analysis)) else: line_bot_api.push_message( yourID, TextSendMessage(text="籌碼面選股結果:沒有可以選的股票\n")) ################################################################################ ################################################################################ elif event.message.text == '殖利率選股': line_bot_api.push_message(uid, TextSendMessage('Boss請稍等我正在運算中...')) elected_Dividend_yield = '' url = 'https://www.twse.com.tw/exchangeReport/BWIBBU_d?response=json&date=&selectType=&_=1574844091489' list_req = requests.get(url) soup = BeautifulSoup(list_req.content, "html.parser") getjson3 = json.loads(soup.text) for e in getjson3['data']: if e[2] > '6': elected_Dividend_yield += e[0] + ' ' + e[1] + '\n' if elected_Dividend_yield != '': line_bot_api.push_message( yourID, TextSendMessage(text="殖利率選股結果:\n" + elected_Dividend_yield)) else: line_bot_api.push_message( yourID, TextSendMessage(text="殖利率選股結果:沒有可以選的股票\n")) ################################################################################ elif event.message.text == '呼叫小秘書': message = TextSendMessage(text='Boss我能幫你甚麼?') else: message = TextSendMessage(text='') line_bot_api.reply_message(event.reply_token, message)