2019年12月19日 星期四

webhooks程式碼



ini_set('display_errors', 'On'); if( $_REQUEST['hub_challenge']){ echo $_REQUEST['hub_challenge']; } $raw = file_get_contents('php://input'); rewrite('webhooks.txt', "\n\n\n".$raw ."\n\n"); $data = json_decode($raw, true); if( $data['object'] == 'page'){ foreach($data['entry'] as $entry) { foreach($entry['standby'] as $messaging) { rewrite('webhooks.txt', '[sender][id] ='.$messaging['sender']['id']."\n"); rewrite('webhooks.txt', '[message][text] ='.$messaging['message']['text']."\n"); if(!empty($messaging['message']['text'])){ sendText($messaging['sender']['id'], $messaging['message']['text']); } } } } function sendText($sender,$text){ $token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$token; $data['messaging_type'] = "RESPONSE"; $data['recipient'] = array('id' => $sender); $data['message'] = array('text' => 'Text received, echo: '.$text); echo json_encode($data)."\n"; $options = array( 'http' => array( 'header' => "Content-type: application/json\r\n", 'method' => 'POST', 'content' => json_encode($data) ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; } function rewrite($filename, $data) { $filenum=fopen($filename,'a'); //flock($filenum,LOCK_EX); fwrite($filenum,$data); //flock($filenum,LOCK_UN); fclose($filenum); //chmod($filename,0666); } ?>

沒有留言:

張貼留言

IKEA吊櫃廚櫃

 好不容易裝好IKEA買來的吊櫃,花了三天。 從組裝,鑽牆,上牆調水平,累死我了。