# Line bot with laravel and heroku ###### tags: `Laravel` `Line bot` ## 基本設定 * [參考筆記](https://hackmd.io/WbAIIb5dQyySoqofEjpE1Q?view#5-%E8%A8%AD%E5%AE%9Aline%E6%A9%9F%E5%99%A8%E4%BA%BA) * ngrok * ngrok http 8000 * 這個要跟本地端開啟的port對上 * 可以開啟本地端當伺服器,將以下的ROOT路徑換成ngrok產生的網址 * ![](https://i.imgur.com/GGID1v5.png) * w7 複製好像是要用enter * Http::post() * ![](https://i.imgur.com/1a8cL9Y.png) * 好用的發送請求class ## 想優化的地方 ```php= public function handleMessageText($string) { // 在此METHOD處理文字訊息 // 轉小寫 $string = strtolower($string); // 判斷該文字在stocks資料庫中存在與否 // !!!!!!!!!!!!!這個邏輯有同代碼多筆的問題存在!!!!待解決 // 目前先這樣測試功能 $contents = []; $tradeFeeRate = 0.001425; $tradeTaxRate = 0.003; $tradeDiscount = 0.28; if ($string === 'stocks') { $allStocks = Stocks::get(); foreach ($allStocks as $stock) { // 未含交易手續費 $sum = $stock->purchase_price * $stock->amount; // 含交易手續費 $sumAfterFee = floor($sum * (1 + $tradeFeeRate * $tradeDiscount)); // 取得現在股價 $nowStockPrice = round($this->getStockPrice($stock->stock_code), 2); // $nowStockPrice = 20; // 計算現在市價 $marketValue = $stock->amount * $nowStockPrice; // 含交易手續費&交易稅 $marketValueSoldPrice = floor($marketValue * (1 - ($tradeFeeRate * $tradeDiscount) - ($tradeTaxRate))); // 與購買時的差額 $profit = number_format($marketValueSoldPrice - $sumAfterFee); // 賺:紅色 虧:綠色 $profitColor = $profit >= 0 ? "#ff0000" : "#00ff00"; // $totalCost += $sumAfterFee; // // 單項明細 // 加手續費後取得金額 $sumAfterFee = number_format($sumAfterFee); // 持有天數 $obtainDays = Carbon::parse($stock->purchase_date)->diffInDays(now()); $contents[] = [ "type" => "text", "text" => "{$stock->stock_code}", "weight" => "bold", "size" => "3xl", "align" => "center" ]; $contents[] = [ "type" => "box", "layout" => "vertical", "margin" => "lg", "spacing" => "sm", "contents" => [ [ "type" => "box", "layout" => "baseline", "spacing" => "xs", "contents" => [ [ "type" => "text", "text" => "持有天數", "color" => "#aaaaaa", "size" => "sm", "flex" => 2, "offsetEnd" => "none" ], [ "type" => "text", "text" => "{$obtainDays}", "wrap" => true, "color" => "#0000ff", "size" => "xxl", "flex" => 5, "align" => "end" ] ] ], [ "type" => "box", "layout" => "baseline", "spacing" => "sm", "contents" => [ [ "type" => "text", "text" => "現時盈虧", "color" => "#aaaaaa", "size" => "sm", "flex" => 2 ], [ "type" => "text", "text" => "{$profit}", "wrap" => true, "color" => $profitColor, "size" => "xxl", "flex" => 5, "align" => "end" ] ] ] ] ]; $contents[] = [ "type" => "separator" ]; } } else { $stockData = Stocks::where('stock_code', '=', $string)->first(); if ($stockData !== null) { // 未含交易手續費 $sum = $stockData->purchase_price * $stockData->amount; // 含交易手續費 $sumAfterFee = floor($sum * (1 + $tradeFeeRate * $tradeDiscount)); // 取得現在股價 $nowStockPrice = round($this->getStockPrice($stockData->stock_code), 2); // $nowStockPrice = 20; // 計算現在市價 $marketValue = $stockData->amount * $nowStockPrice; // 含交易手續費&交易稅 $marketValueSoldPrice = floor($marketValue * (1 - ($tradeFeeRate * $tradeDiscount) - ($tradeTaxRate))); // 與購買時的差額 $profit = number_format($marketValueSoldPrice - $sumAfterFee); // 賺:紅色 虧:綠色 $profitColor = $profit >= 0 ? "#ff0000" : "#00ff00"; // 持有天數 $obtainDays = Carbon::parse($stockData->purchase_date)->diffInDays(now()); $contents[] = [ "type" => "text", "text" => "{$stockData->stock_code}", "weight" => "bold", "size" => "3xl", "align" => "center" ]; $contents[] = [ "type" => "box", "layout" => "vertical", "margin" => "lg", "spacing" => "sm", "contents" => [ [ "type" => "box", "layout" => "baseline", "spacing" => "xs", "contents" => [ [ "type" => "text", "text" => "持有天數", "color" => "#aaaaaa", "size" => "sm", "flex" => 2, "offsetEnd" => "none" ], [ "type" => "text", "text" => "{$obtainDays}", "wrap" => true, "color" => "#0000ff", "size" => "xxl", "flex" => 5, "align" => "end" ] ] ], [ "type" => "box", "layout" => "baseline", "spacing" => "sm", "contents" => [ [ "type" => "text", "text" => "現時盈虧", "color" => "#aaaaaa", "size" => "sm", "flex" => 2 ], [ "type" => "text", "text" => "{$profit}", "wrap" => true, "color" => $profitColor, "size" => "xxl", "flex" => 5, "align" => "end" ] ] ] ] ]; } else { $contents[] = [ "type" => "text", "text" => "無資料", "weight" => "bold", "size" => "3xl", "align" => "center" ]; } } return $contents; } ``` * if else 包了兩層 * 目前想對進來的字串做新的判斷太麻煩