<?php
$accessToken = 'ここだけ置き換えて!';
$jsonString = file_get_contents('php://input');
error_log($jsonString);
$jsonObj = json_decode($jsonString);
$message = $jsonObj->{"events"}[0]->{"message"};
$replyToken = $jsonObj->{"events"}[0]->{"replyToken"};
//受信メッセージを解析//
if ($message->{"type"} == 'text') {
$textmessage = $message->{"text"};
$replymessage = 'Receiving Message : '.$message->{"text"};
} else {
$textmessage = 'Messege Receiving Error! invalid message type';
$replymessage = 'Messege Receiving Error! invalid message type';
}
$messageData = [
'type' => 'text',
'text' => $replymessage
];
//Salesforceに書き込み//
$url = parse_url(getenv('DATABASE_URL'));
$dsn = sprintf('pgsql:host=%s;dbname=%s', $url['host'], substr($url['path'], 1));
$dbh = new PDO($dsn, $url['user'], $url['pass']);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO salesforce.linemessage__c (message__c) VALUES (?)";
$stmt = $dbh -> prepare($sql);
$stmt->bindvalue(1, $textmessage, PDO::PARAM_STR);
$stmt->execute();
$dbh = null;
//LINEにリプライ//
$response = [
'replyToken' => $replyToken,
'messages' => [$messageData]
];
error_log(json_encode($response));
$ch = curl_init('https://api.line.me/v2/bot/message/reply');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charser=UTF-8',
'Authorization: Bearer ' . $accessToken
));
$result = curl_exec($ch);
error_log($result);
curl_close($ch);