Last active
April 13, 2017 14:21
-
-
Save anoochit/d75546c0b2d03f1cd012c3d89aad91b6 to your computer and use it in GitHub Desktop.
demo linebot step 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
} | |
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())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment