วันพุธที่ 31 พฤษภาคม พ.ศ. 2560

ข้อมูลสำคัญของหน่วยงานความมั่นคงสหรัฐรั่วนับแสนรายการ ถูก Contractor ปล่อยสาธารณะบน AWS

Chris Vickery นักวิเคราะห์ด้านความเสี่ยงในเชิงไซเบอร์แห่ง UpGuard ได้ค้นพบไฟล์สำคัญนับแสนหน่วยงานความมั่นคงสหรัฐบน Cloud Storage ของ Amazon โดยไฟล์เหล่านั้นสามารถถูกเข้าถึงได้อย่างสาธารณะ ไม่มีรหัสผ่านใดๆ ป้องกันเลย และมีขนาดรวมกันกว่า 28GB เลยทีเดียว

Credit: ShutterStock.com

เอกสารที่ค้นพบนี้เป็นเอกสารของโครงการหนึ่งในหน่วยงาน National Geospatial-Intelligence Agency (NGA) โดยนอกจากเอกสารข้อมูลที่ใช้ทำงานภายในหน่วยงานภาครัฐแล้ว เอกสารในไฟล์เหล่านี้ก็ยังมีทั้งรหัสผ่านของระบบสำคัญในรัฐบาลสหรัฐ, รหัสผ่านของพนักงานอาวุโสใน Booz Allen Hamilton ซึ่งเป็นหนึ่งในบริษัทที่ทำงานให้กับหน่วยงานความมั่นคงของสหรัฐ รวมถึงรหัสผ่านของคู่สัญญารายอื่นๆ ที่ทำงานกับหน่วยงานรัฐนี้ด้วย ทำให้ถึงแม้ข้อมูลต่างๆ ที่รั่วไหลออกมานี้ถึงจะไม่ได้เป็นความลับอะไรมากนัก แต่รหัสผ่านเหล่านี้ก็อาจนำไปสู่ข้อมูลความลับอื่นๆ มากมายได้ รวมถึงสามารถเข้าถึง Code Repository ต่างๆ ไปจนถึงระบบที่มีการป้องกันอย่างหนาแน่นของ Pentagon ได้
ในตอนแรกนั้น ไฟล์เหล่านี้ดูเหมือนจะถูกซ่อนเอาไว้จนคนทั่วๆ ไปที่ไม่รู้ช่องทางที่ชัดเจนก็ไม่อาจเข้าถึงได้ แต่กับคนที่มีวัตถุประสงค์อย่าง Vickery หรือคนที่มีวัตถุประสงค์อื่นๆ นั้นก็อาจค้นหาช่องทางจนโหลดไฟล์เหล่านั้นมาได้ทั้งหมด และอาจนำไปสู่การเข้าถึงระบบที่มีความสำคัญสูงต่อไปได้ โดยไม่ต้องอาศัยเทคนิคในการ Hack ระบบแต่อย่างใดเลย
Vickery นั้นเป็นผู้ที่ค้นพบข้อมูลรั่วไหลมาหลายต่อหลายครั้งจนมีชื่อเสียงโด่งดัง ไม่ว่าจะเป็นกรณีของฐานข้อมูลผู้ลงคะแนนเสียงในสหรัฐกว่า 191 ล้านรายเมื่อปี 2015, ฐานขจ้อมูลผู้ใช้งาน MacKeeper 13 ล้านรายเมื่อปี 2015, ฐานข้อมูล River City Media (RCM) ที่บันทึกข้อมูลของผู้ใช้งานไว้กว่า 1,400 ล้านรายการเมื่อเร็วๆ นี้ และกรณีอื่นๆ อีกมากมาย
ปัจจุบันทั้ง NGA และ Booz Allen Hamilton เองต่างก็กำลังสืบสวนและแก้ไขปัญหาร่วมกันอยู่ และระบุว่าที่ผ่านมายังไม่มีใครนำข้อมูลที่รั่วไหลเหล่านี้ไปใช้โจมตีหรือขโมยข้อมูลความลับแต่อย่างใด
ก็ถือเป็นอีกบทเรียนที่ดีสำหรับเหล่าองค์กรต่างๆ ที่มีการจ้าง Contractor ภายนอกเข้ามาทำงาน ที่ควรจะตรวจสอบการทำงานและควบคุมการนำข้อมูลไปใช้ในกรณีต่างๆ ให้มีความรัดกุมในแง่ของความมั่นคงปลอดภัยด้วย โดยเฉพาะการใช้งาน Cloud นั้นก็ควรใช้ด้วยความระมัดระวัง และมีความรู้ที่ถูกต้องครบถ้วนในเทคโนโลยีที่กำลังใช้งานอยู่ครับ

Google เสริมความปลอดภัยให้ Gmail ป้องกัน Malware ด้วย Machine Learning และวิธีการอื่นๆ มากมาย

Google ได้ออกมาประกาศเสริมความมั่นคงปลอดภัยให้กับเหล่าผู้ใช้งาน Gmail ด้วยการทำเทคนิคหลากหลายรวมถึงการใช้ Machine Learning มาป้องกันผู้ใช้งานจาก Malware ดังต่อไปนี้


  • ใช้ Machine Learning ตรวจจับการโจมตีแบบ Phishing ที่เกิดขึ้นใน Gmail
  • มีการแจ้งเตือนผู้ใช้งานที่ทำการคลิกลิงค์ใน Gmail หากลิงค์นั้นถูกตรวจสอบแล้วว่าเป็นลิงค์อันตราย
  • เพิ่มการแจ้งเตือนเวลามีการ Reply ไปยังเมล์ที่ภายนอกโดยไม่ตั้งใจ
  • เพิ่มวิธีการตรวจสอบ Attachment ให้มั่นคงปลอดภัยขึ้นไปอีก ด้วยการทำ Correlation ระหว่าง Spam Signal, Attachment, Sender Heuristics เพื่อทำนายว่าเมล์นั้นๆ จะมี Malware ตระกูลใหม่ปรากฎอยู่หรือไม่ ป้องกัน Zero-day, Ransomware และ Malware ได้ดีขึ้น
  • ทำการบล็อคไฟล์นามสกุลที่มีความสุ่มเสี่ยงด้านความมั่นคงปลอดภัย รวมถึงไฟล์ Executable และ Javascript ด้วย
ฟีเจอร์ทั้งหมดนี้จะเปิดให้ผู้ใช้งาน Google G Suite ทุก Edition ได้ใช้ภายใน 1-3 วันนับถัดจากนี้
Google ยังได้ระบุด้วยว่าที่ผ่านมา Machine Learning ได้ช่วยให้ Gmail สามารถตรวจจับ Spam ได้แม่นยำถึง 99% และคาดว่าด้วยวิธีการป้องกันแบบใหม่ทั้งหมดที่เสนอมานี้ ก็จะทำให้ในแต่ละวัน Gmail สามารถคัดกรองเมล์ทิ้งเพิ่มได้อีกวันละหลายร้อยล้านฉบับอย่างแม่นยำ
ก่อนหน้านี้ Google ได้พยายามเพิ่มวิธีการใหม่ๆ ในการป้องกันให้ระบบ Gmail มีความปลอดภัยมาโดยตลอด โดย 3 แนวทางหลักที่ Google ใช้รับมือกับเหล่า Scammer นั้นก็ได้แก่
  • คัดกรองเมล์ที่มีการตรวจพบ Virus ทิ้ง และส่งเมล์กลับไปแจ้งเตือนผู้ส่ง
  • ยับยั้งผู้ใช้งาน Gmail ไม่ให้ส่ง Attachment ที่แฝงอันตรายออกไป
  • ป้องกันผู้ใช้งาน Gmail ไม่ให้โหลด Attachment ที่มีการตรวจพบ Virus

Google Compute Engine ปรับบริการ – เปิดให้เลือก CPU Platform, เพิ่มลิมิต memory สูงสุด

สัปดาห์นี้ Google มีการเปลี่ยนแปลงการให้บริการ Google Compute Engine หลายรายการ และหนึ่งในนั้นคือเปิดให้เลือกประเภท microarchitecture ของ CPU ของเครื่อง VMs ได้แล้ว

การเปลี่ยนแปลงทั้งหมดโดยสรุปมีดังนี้
  • ปรับสถานะ CPU Skylake Xeon เป็น GA หลังจากเปิด beta ไปเมื่อเดือนกุมภาพันธ์ที่ผ่านมา โดยสามารถใช้งานได้กับเครื่องใน region Western US (รัฐ Oregon), Western Europe (ประเทศเบลเยี่ยม), Eastern Asia Pacific (ประเทศไต้หวัน) และสำหรับการให้บริการใน region อื่นๆจะตามมาเร็วๆนี้
  • ในระยะเริ่มต้น กูเกิลจะให้บริการ Compute Engine นี้ด้วยอัตราค่าบริการเท่ากับประเภท CPU อื่นๆ แต่หลังจากช่วงโปรโมชั่น 60 วันราคาจะถูกปรับขึ้น 6-10%
  • ผู้ใช้จะสามารถระบุประเภทของ microarchitecture ของ CPU ได้ โดยมีตัวเลือกได้แก่ Sandy Bridge, Ivy Bridge, Haswell, Broadwell, และ Skylake ซึ่งระบบจะการันตีว่า VM ของผู้ใช้จะตรงตามประเภทที่เลือกหรือสูงกว่า
  • เปิดให้เลือกใช้ CPU Broadwell ในทุก region
  • ผู้ใช้สามารถสร้าง VM ที่มี CPU ได้สูงสุด 64 vCPUs แล้ว
  • ปรับให้ผู้ใช้สามารถเลือก memory ได้สูงสุด 455GB จากเดิมที่บังคับอัตราส่วนสูงสุด 6.5GB ต่อ 1 vCPU แต่การเลือกหน่วยความจำที่สูงกว่าอัตราส่วนนี้ก็จะมีค่าใช้จ่ายเพิ่มขึ้น $0.009550 ต่อ GB ต่อชั่วโมง

เปิดตัว Intel SSD DC P4501 ความจุ 4TB เร็ว 360,000 IOPS

Intel ประกาศเปิดตัว SSD รุ่นล่าสุด Intel SSD DC P4501 ที่มีความจุสูงถึง 4TB พร้อมความเร็วระดับ 360,000 IOPS รองรับการใช้งานแบบ Read Intensive เป็นหลัก

Credit: Intel

ถือเป็น SSD ความจุที่ค่อนข้างใหญ่พอตัวทีเดียวด้วยความจุ 4TB ต่อลูก จากการใช้เทคโนโลยี Intel 3D NAND แบบ TLC โดยรองรับ Form Factor ทั้งแบบ U.2 2.5″ และ M.2 ด้วย Interface บบ PCIe 3.1 x4, NVMe 1.2 อีกทั้งยังรองรับการทำ Data-at-Rest Encryption เพื่อลดความเสี่ยงที่จะเกิดเหตุข้อมูลรั่วไหล ในขณะที่ยังสามารถบริหารจัดการได้ผ่านทาง NVM Express-Management Interface (NVMe-MI) ทำให้สามารถติดตามประสิทธิภาพและความทนทานของตัว SSD ได้ ที่สำคัญรุ่นนี้มาพร้อมประกัน 5 ปี สำหรับตอบโจทย์ตลาด Data Center โดยเฉพาะ
Credit: Intel

สำหรับสเป็ครวมๆ ของ Intel DC P4501 มีดังนี้
  • Capacities: 500GB, 1TB, 2TB, 4TB
  • Performance:
    • 64K Sequential read/write: up to 3,200/900 MB/s
    • 4K Random read/write: up to 360,000/46,000 IOPS
  • Manageability: Support for NVM Express-Management Interface (NVMe-MI), NVMe SMART/Health and Log Pages
  • Reliability: End-to-end data protection from silent data corruption; uncorrectable bit error rate < 1 sector per 1017 bits read PLI protects data during power loss
  • Interface: PCIe 3.1 x4, NVMe 1.2
  • Form Factors:
    • U.2 2.5 in x 7 mm (for serviceability, hot-plug, and density)
    • M.2 110 x 22 mm (for ultra-high density)
  • Media: Intel 3D NAND, TLC
  • Endurance: Random/JEDEC up to 1 DWPD or 5 PBW, sequential up to 3 DWPD or 20 PBW
  • Tunable Maximum Power:
    • U.2: 8, 10, 12.5 W
    • M.2: 6 to 8.25 W
  • Idle Power
    • U.2 <5 li="" watts="">
    • M.2 <3 li="" watts="">
  • Warranty: 5-year warranty
ปัจจุบัน Intel DC P4501 ประกาศวางจำหน่ายแล้ว สำหรับรายละเอียดเพิ่มเติมสามารถศึกษาได้ที่ http://www.intel.com/content/www/us/en/products/memory-storage/solid-state-drives/data-center-ssds/dc-p4501-series.html ส่วนการซื้อในประเทศไทย ต้องสอบถามทางตัวแทนจำหน่ายของ Intel นะครับ

วันอาทิตย์ที่ 28 พฤษภาคม พ.ศ. 2560

มาลองทำ Line Chat Bot กัน

          Line เปิดให้บริการ Messaging API มาระยะนึงแล้วแต่ก็ยังไม่ได้ลองเขียน ChatBot สักที ครั้งนี้ก็เลยมาลองเขียนดูบ้าง
             Messaging API สามารถส่งข้อความไปยังผู้ใช้ได้ ส่งข้อความโต้ตอบผู้ใช้ ซึี่งรูปแบบข้อความความสามารถอยู่ในรูปแบบ rich content ได้ เช่น image, audio, video, button, carousel เป็นต้น เราสามารถใช้ rich content โต้ตอบกับผู้ใช้ได้ เช่น สั่งซื้อสินค้า, แนะนำสถานที่โดยใช้ location ของผู้ใช้, บริการแปลภาษา ฯลฯ
          สำหรับ LINE Bot SDK มีให้เลือกใช้หลายภาษา :) มาลองเล่นกัน อันดับแรกต้องมี LINE@ account จากนั้นสั่ง Enable Messanging API ให้เรียบร้อย
         มาสร้างส่วน Webhook กัน LINE มีข้อกำหนดเรื่อง https เราใช้ PaaS อย่าง Heroku ที่มาพร้อมกับ https จะช่วยได้มาก

mkdir -p linebot/web
cd linebot
composer require php
composer require linecorp/line-bot-sdk
composer require monolog/monolog
echo "vendor/" > .gitignore
echo "web: vendor/bin/heroku-php-apache2 web/" > Procfile
echo " web/index.php
git init
git add . && git commit -am "init"
heroku create
git push heroku master
จากนั้นสั่ง
heroku open

        จะพบว่า heroku เปิดหน้า phpinfo มาถูกต้อง ให้ copy url ไปใส่ใน Webhook URL ในหน้าตั้งค่า Messaging API ให้ถูกต้อง
สั่ง config heroku ให้บันทึก ACCESS_TOKEN และ CHANNEL_SECRET ลงใน ENV ดังนี้
heroku config:set LINEBOT_ACCESS_TOKEN=XXXXXXXXX
heroku config:set LINEBOT_CHANNEL_SECRET=XXXXXXXXX

แก้ไฟล์ index.php เพิ่มโค้ดลงไปดังนี้

require_once '../vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;
$logger = new Logger('LineBot');
$logger->pushHandler(new StreamHandler('php://stderr', Logger::DEBUG));
$httpClient = new \LINE\LINEBot\HTTPClient\CurlHTTPClient($_ENV["LINEBOT_ACCESS_TOKEN"]);
$bot = new \LINE\LINEBot($httpClient, ['channelSecret' => $_ENV["LINEBOT_CHANNEL_SECRET"]]);
$signature = $_SERVER['HTTP_' . \LINE\LINEBot\Constant\HTTPHeader::LINE_SIGNATURE];
try {
$events = $bot->parseEventRequest(file_get_contents('php://input'), $signature);
} catch(\LINE\LINEBot\Exception\InvalidSignatureException $e) {
error_log('parseEventRequest failed. InvalidSignatureException => '.var_export($e, true));
} catch(\LINE\LINEBot\Exception\UnknownEventTypeException $e) {
error_log('parseEventRequest failed. UnknownEventTypeException => '.var_export($e, true));
} catch(\LINE\LINEBot\Exception\UnknownMessageTypeException $e) {
error_log('parseEventRequest failed. UnknownMessageTypeException => '.var_export($e, true));
} catch(\LINE\LINEBot\Exception\InvalidEventRequestException $e) {
error_log('parseEventRequest failed. InvalidEventRequestException => '.var_export($e, true));
}
view rawindex.php hosted with ❤ by GitHub
แล้วสั่ง commit และ push ใหม่ โค้ดข้างต้นจะอ่าน Webhook Events ที่ได้รับมาจาก Line App เราจะอ่าน Events เหล่านี้มาสร้าง Bot กัน Events ที่เราจะเจอบ่อยๆ เช่น MessageEvent คือข้อความที่ส่งมาจากผู้ใช้ และ Postback Event คือ action ที่เกิดจากผู้ใช้ส่งกลับมาให้ Webhook เป็นต้น แนะนำให้เปิด Line Bot SDK กับ API Reference เอาไว้ดูตอนเขียนด้วยนะครับจะได้ไม่งง
เพิ่มโค้ดเพื่อตรวจสอบ Message Events ดังนี้
require_once '../vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;
$logger = new Logger('LineBot');
$logger->pushHandler(new StreamHandler('php://stderr', Logger::DEBUG));
$httpClient = new \LINE\LINEBot\HTTPClient\CurlHTTPClient($_ENV["LINEBOT_ACCESS_TOKEN"]);
$bot = new \LINE\LINEBot($httpClient, ['channelSecret' => $_ENV["LINEBOT_CHANNEL_SECRET"]]);
$signature = $_SERVER['HTTP_' . \LINE\LINEBot\Constant\HTTPHeader::LINE_SIGNATURE];
try {
$events = $bot->parseEventRequest(file_get_contents('php://input'), $signature);
} catch(\LINE\LINEBot\Exception\InvalidSignatureException $e) {
error_log('parseEventRequest failed. InvalidSignatureException => '.var_export($e, true));
} catch(\LINE\LINEBot\Exception\UnknownEventTypeException $e) {
error_log('parseEventRequest failed. UnknownEventTypeException => '.var_export($e, true));
} catch(\LINE\LINEBot\Exception\UnknownMessageTypeException $e) {
error_log('parseEventRequest failed. UnknownMessageTypeException => '.var_export($e, true));
} catch(\LINE\LINEBot\Exception\InvalidEventRequestException $e) {
error_log('parseEventRequest failed. InvalidEventRequestException => '.var_export($e, true));
}
foreach ($events as $event) {
// Postback Event
if (($event instanceof \LINE\LINEBot\Event\PostbackEvent)) {
$logger->info('Postback message has come');
continue;
}
// Location Event
if ($event instanceof LINE\LINEBot\Event\MessageEvent\LocationMessage) {
$logger->info("location -> ".$event->getLatitude().",".$event->getLongitude());
continue;
}
// Message Event = TextMessage
if (($event instanceof \LINE\LINEBot\Event\MessageEvent\TextMessage)) {
// get message text
$messageText=strtolower(trim($event->getText()));
}
}
view rawindex.php hosted with ❤ by GitHub
จากโค้ดจะแยก Postback, Location Message และ TextMessage เพื่อใช้ในการเขียนโค้ดในการตอบโต้กับข้อความที่ผู้ใช้ส่งมา ลองมาดูในส่วนของ TextMessage กันก่อน เราสามารถแยก message ที่ส่งมาจากผู้ใช้โดยใช้ method getText เราจะได้ text message เอาไว้ใช้วิเคราะห์ประโยคหรือส่งไปให้ conversation service หรือ machine learning service ได้ หรือจะเอามาทำ chat bot แบบถามมาตอบไปเป็น rule base ง่ายๆ ได้เช่นกัน เพื่อให้เข้าใจการส่ง message กลับไปยังผู้ใช้ เรามาตั้ง rule base ง่ายๆ กันก่อนดังนี้
  • พิมพ์ text จะส่งข้อความในรูปแบบ text กลับไปให้
  • พิมพ์ image จะส่ง image กลับไปให้
  • พิมพ์ location จะส่ง location message กลับไปให้
  • พิมพ์ confirm จะส่ง confirm dialog กลับไปให้
  • พิมพ์ button จะส่ง dialog พร้อมปุ่ม กลับไปให้
  • พิมพ์ carousel จะส่งข้อความแบบ carousel กลับไปให้
ตัวอย่างการส่ง Text Message
$outputText = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder("text message");
$bot->replyMessage($event->getReplyToken(), $outputText);
view rawindex.php hosted with ❤ by GitHub
ตัวอย่างการส่ง Image
$img_url = "https://cdn.shopify.com/s/files/1/0379/7669/products/sampleset2_1024x1024.JPG?v=1458740363";
$outputText = new LINE\LINEBot\MessageBuilder\ImageMessageBuilder($img_url, $img_url);
$response = $bot->replyMessage($event->getReplyToken(), $outputText);
view rawindex.php hosted with ❤ by GitHub
ตัวอย่างการส่ง Location
$outputText = new \LINE\LINEBot\MessageBuilder\LocationMessageBuilder("Eiffel Tower", "Champ de Mars, 5 Avenue Anatole France, 75007 Paris, France", 48.858328, 2.294750);
$response = $bot->replyMessage($event->getReplyToken(), $outputText);
view rawindex.php hosted with ❤ by GitHub
ตัวอย่างการส่ง Confirm
$actions = array (
New \LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder("yes", "ans=y"),
New \LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder("no", "ans=N")
);
$button = new \LINE\LINEBot\MessageBuilder\TemplateBuilder\ConfirmTemplateBuilder("confim message", $actions);
$outputText = new \LINE\LINEBot\MessageBuilder\TemplateMessageBuilder("confim message", $button);
$response = $bot->replyMessage($event->getReplyToken(), $outputText);
view rawindex.php hosted with ❤ by GitHub
ตัวอย่างการส่ง Button
$actions = array (
// general message action
New \LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder("button 1", "text 1"),
// URL type action
New \LINE\LINEBot\TemplateActionBuilder\UriTemplateActionBuilder("Google", "http://www.google.com"),
// The following two are interactive actions
New \LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder("next page", "page=3"),
New \LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder("Previous page", "page=1")
);
$img_url = "https://cdn.shopify.com/s/files/1/0379/7669/products/sampleset2_1024x1024.JPG?v=1458740363";
$button = new \LINE\LINEBot\MessageBuilder\TemplateBuilder\ButtonTemplateBuilder("button text", "description", $img_url, $actions);
$outputText = new \LINE\LINEBot\MessageBuilder\TemplateMessageBuilder("Button template builder", $button);
$response = $bot->replyMessage($event->getReplyToken(), $outputText);
view rawindex.php hosted with ❤ by GitHub
ตัวอย่างการส่ง Carousel
$columns = array();
$img_url = "https://cdn.shopify.com/s/files/1/0379/7669/products/sampleset2_1024x1024.JPG?v=1458740363";
for($i=0;$i<5;$i++) {
$actions = array(
new \LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder("Add to Cart","action=carousel&button=".$i),
new \LINE\LINEBot\TemplateActionBuilder\UriTemplateActionBuilder("View","http://www.google.com")
);
$column = new \LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselColumnTemplateBuilder("Title", "description", $img_url , $actions);
$columns[] = $column;
}
$carousel = new \LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselTemplateBuilder($columns);
$outputText = new \LINE\LINEBot\MessageBuilder\TemplateMessageBuilder("Carousel Demo", $carousel);
view rawindex.php hosted with ❤ by GitHub
โค้ดทั้งหมด
require_once '../vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;
$logger = new Logger('LineBot');
$logger->pushHandler(new StreamHandler('php://stderr', Logger::DEBUG));
$httpClient = new \LINE\LINEBot\HTTPClient\CurlHTTPClient($_ENV["LINEBOT_ACCESS_TOKEN"]);
$bot = new \LINE\LINEBot($httpClient, ['channelSecret' => $_ENV["LINEBOT_CHANNEL_SECRET"]]);
$signature = $_SERVER['HTTP_' . \LINE\LINEBot\Constant\HTTPHeader::LINE_SIGNATURE];
try {
$events = $bot->parseEventRequest(file_get_contents('php://input'), $signature);
} catch(\LINE\LINEBot\Exception\InvalidSignatureException $e) {
error_log('parseEventRequest failed. InvalidSignatureException => '.var_export($e, true));
} catch(\LINE\LINEBot\Exception\UnknownEventTypeException $e) {
error_log('parseEventRequest failed. UnknownEventTypeException => '.var_export($e, true));
} catch(\LINE\LINEBot\Exception\UnknownMessageTypeException $e) {
error_log('parseEventRequest failed. UnknownMessageTypeException => '.var_export($e, true));
} catch(\LINE\LINEBot\Exception\InvalidEventRequestException $e) {
error_log('parseEventRequest failed. InvalidEventRequestException => '.var_export($e, true));
}
foreach ($events as $event) {
// Postback Event
if (($event instanceof \LINE\LINEBot\Event\PostbackEvent)) {
$logger->info('Postback message has come');
continue;
}
// Location Event
if ($event instanceof LINE\LINEBot\Event\MessageEvent\LocationMessage) {
$logger->info("location -> ".$event->getLatitude().",".$event->getLongitude());
continue;
}
// Message Event = TextMessage
if (($event instanceof \LINE\LINEBot\Event\MessageEvent\TextMessage)) {
$messageText=strtolower(trim($event->getText()));
switch ($messageText) {
case "text" :
$outputText = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder("text message");
break;
case "location" :
$outputText = new \LINE\LINEBot\MessageBuilder\LocationMessageBuilder("Eiffel Tower", "Champ de Mars, 5 Avenue Anatole France, 75007 Paris, France", 48.858328, 2.294750);
break;
case "button" :
$actions = array (
// general message action
New \LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder("button 1", "text 1"),
// URL type action
New \LINE\LINEBot\TemplateActionBuilder\UriTemplateActionBuilder("Google", "http://www.google.com"),
// The following two are interactive actions
New \LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder("next page", "page=3"),
New \LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder("Previous", "page=1")
);
$img_url = "https://cdn.shopify.com/s/files/1/0379/7669/products/sampleset2_1024x1024.JPG?v=1458740363";
$button = new \LINE\LINEBot\MessageBuilder\TemplateBuilder\ButtonTemplateBuilder("button text", "description", $img_url, $actions);
$outputText = new \LINE\LINEBot\MessageBuilder\TemplateMessageBuilder("this message to use the phone to look to the Oh", $button);
break;
case "carousel" :
$columns = array();
$img_url = "https://cdn.shopify.com/s/files/1/0379/7669/products/sampleset2_1024x1024.JPG?v=1458740363";
for($i=0;$i<5;$i++) {
$actions = array(
new \LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder("Add to Card","action=carousel&button=".$i),
new \LINE\LINEBot\TemplateActionBuilder\UriTemplateActionBuilder("View","http://www.google.com")
);
$column = new \LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselColumnTemplateBuilder("Title", "description", $img_url , $actions);
$columns[] = $column;
}
$carousel = new \LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselTemplateBuilder($columns);
$outputText = new \LINE\LINEBot\MessageBuilder\TemplateMessageBuilder("Carousel Demo", $carousel);
break;
case "image" :
$img_url = "https://cdn.shopify.com/s/files/1/0379/7669/products/sampleset2_1024x1024.JPG?v=1458740363";
$outputText = new LINE\LINEBot\MessageBuilder\ImageMessageBuilder($img_url, $img_url);
break;
case "confirm" :
$actions = array (
New \LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder("yes", "ans=y"),
New \LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder("no", "ans=N")
);
$button = new \LINE\LINEBot\MessageBuilder\TemplateBuilder\ConfirmTemplateBuilder("problem", $actions);
$outputText = new \LINE\LINEBot\MessageBuilder\TemplateMessageBuilder("this message to use the phone to look to the Oh", $button);
break;
default :
$outputText = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder("demo command: text, location, button, confirm to test message template");
break;
}
$response = $bot->replyMessage($event->getReplyToken(), $outputText);
}
}
view rawindex.php hosted with ❤ by GitHub
ที่มา Thaiopensource