PHP รายงานยอดขายส่งเข้า Line Notify โดยดึงข้อมูลจาก MYSQL

PHP รายงานยอดขายส่งเข้า Line Notify โดยดึงข้อมูลจาก MYSQL
สามารถตั้งเวลา Run บน Hosting โดยใช้ Cron-Job หรือ Task Scheduler และ
โต้ตอบถามยอดขายได้ตลอดเวลา ทำการตอบโต้ได้



$access_token = '<Channel access token>';
date_default_timezone_set('Asia/Bangkok');
//กำหนดเวลาย้อนหลังของการรายงาน
$date1 = date('Y-m-d H:',strtotime('-2 hours'));
$date2 = date('Y-m-d H:',strtotime('-1 hours'));
$date3 = date('Y-m-d H:');
$now = date('Y-m-d H:i:s');
    $objConnect = mysqli_connect("localhost","root","123456","database") or die("Error Connect to Database");
    $strSQL = "SELECT * FROM shopping where upper(timebuy) LIKE '".$date1."%' or upper(timebuy) LIKE '".$date2."%' or upper(timebuy) LIKE '".$date3."%' ORDER BY id desc";
    $objQuery = mysqli_query($objConnect,$strSQL) or die ("Error Query [".$strSQL."]");
$mynum=mysqli_num_rows($objQuery);
$check = "แจ้งรายงานเวลา ".$now." ยอดขายที่มีล่าสุด ";
if($mynum < 1){
$check .= "\nไม่มียอดขายล่าสุด";
}
  while($objResult = mysqli_fetch_array($objQuery))
    {
    $check .= "\n".$objResult['timebuy']." ".$objResult['buy'];
  }

// Get POST body content
$content = file_get_contents('php://input');
// Parse JSON
$events = json_decode($content, true);
// Validate parsed JSON data
if (!is_null($events['events'])) {
    // Loop through each event
    foreach ($events['events'] as $event) {
        // Reply only when message sent is in 'text' format
        if ($event['type'] == 'message' && $event['message']['type'] == 'text') {
            // Get text sent
            $text = $event['message']['text'];
            // Get replyToken
            $replyToken = $event['replyToken'];
         
             if ($event['message']['text'] == 'รายงานยอดขาย' || $event['message']['text'] == 'hi'){
              
                $messages = [
                    'type' => 'text',
                    'text' => $check,
                ];
                $url = 'https://api.line.me/v2/bot/message/reply';
                $data = [
                    'replyToken' => $replyToken,
                    'messages' => [$messages],
                ];
                $post = json_encode($data);
             
                $headers = array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token);
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                $result = curl_exec($ch);
                curl_close($ch);
                echo $result . "\r\n";
             }
        }
    }
}


แสดงความคิดเห็น

0 ความคิดเห็น