Created
October 5, 2018 15:46
-
-
Save ronnywang/0c9b6836ad148f5bf1e5665ecf47660d to your computer and use it in GitHub Desktop.
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 | |
date_default_timezone_set('Asia/Taipei'); | |
if (array_key_exists('hub_mode', $_GET) and $_GET['hub_mode'] == 'subscribe' and $_GET['hub_verify_token'] == getenv('VERIFY_TOKEN')) { | |
echo $_GET['hub_challenge']; | |
exit; | |
} | |
$access_token = getenv('PAGE_TOKEN'); | |
$reply = function($recipient, $message) use ($access_token) { | |
$curl = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token=' . urlencode($access_token)); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
$json = (array( | |
'recipient' => array('id' => $recipient), | |
'message' => array('text' => $message), | |
)); | |
var_dump($json); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($json)); | |
curl_exec($curl); | |
curl_close($curl); | |
}; | |
if (file_exists('/tmp/db.json')) { | |
$db = json_decode(file_get_contents('/tmp/db.json')); | |
} else { | |
$db = new StdClass; | |
$db->links = new StdClass; | |
} | |
$body = file_get_contents('php://input'); | |
$body = json_decode($body); | |
foreach ($body->entry as $entry) { | |
foreach ($entry->messaging as $message) { | |
$reply_message = array(); | |
foreach ($message->message->attachments as $attachment) { | |
$url = ($attachment->url); | |
while (true) { | |
if (strpos($url, 'https://l.facebook.com/l.php') === 0) { | |
parse_str(explode('?', $url)[1], $ret); | |
$url = $ret['u']; | |
} elseif (strpos($url, 'https://goo.gl/') === 0) { | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_exec($curl); | |
$info = curl_getinfo($curl); | |
curl_close($curl); | |
$url = ($info['redirect_url']); | |
} else { | |
break; | |
} | |
} | |
$first = false; | |
if (!property_exists($db->links, $url)) { | |
$db->links->{$url} = new StdClass; | |
$db->links->{$url}->title = $attachment->title; | |
$db->links->{$url}->followers = new StdClass; | |
$first = true; | |
} | |
if (!property_exists($db->links->{$url}->followers, $message->sender->id)) { | |
$db->links->{$url}->followers->{$message->sender->id} = time(); | |
$count = count(array_values(get_object_vars($db->links->{$url}->followers))); | |
if ($first) { | |
$reply_message[] = "您是第一個回報 $url 的人"; | |
} else { | |
$reply_message[] = "加上您共有 {$count} 人回報 {$url}"; | |
} | |
} else { | |
$reply_message[] = "您已經於 " . date('Y-m-d H:i:s', $db->links->{$url}->followers->{$message->sender->id}) . " 回報過 {$url}"; | |
} | |
file_put_contents('/tmp/db.json', json_encode($db)); | |
} | |
if (!$reply_message) { | |
$reply_message[] = '我不知道您要做什麼'; | |
} | |
$reply($message->sender->id, implode("\n", $reply_message)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment