WordPress FB2WP外掛:自動回覆粉絲頁留言事件範例
<?php | |
function mxp_fb_comment_callback($item) { | |
$parent_id = isset($item['parent_id']) ? $item['parent_id'] : ""; | |
$comment_id = isset($item['comment_id']) ? $item['comment_id'] : ""; | |
$message = isset($item['message']) ? $item['message'] : ""; | |
$post_id = isset($item['post_id']) ? $item['post_id'] : ""; | |
$sender_name = isset($item['sender_name']) ? $item['sender_name'] : ""; | |
$verb = isset($item['verb']) ? $item['verb'] : ""; //must be "add" | |
if ($parent_id != $post_id || $verb != "add" || $message == "" || $comment_id == "") { | |
//不是第一則留言 或 不是新增留言 或 留言不是文字 就不回覆了! | |
return; | |
} | |
$api_url = "https://graph.facebook.com/v2.10/{$comment_id}/comments?access_token=這裡是一個有效的偷啃,權限要包含 publish_pages, manage_pages 這兩個哦,別說我沒提醒你QQ"; | |
$msg = ""; | |
if (preg_match("/阿竣/i", $message)) { | |
$msg = "找阿竣是不是?等等哦!粉絲頁小幫手馬上幫你 ping 他一下~ by 自動回覆機器人"; | |
} | |
if ($msg != "") { | |
$response = wp_remote_post($api_url, array( | |
'method' => 'POST', | |
'timeout' => 5, | |
'redirection' => 5, | |
'httpversion' => '1.1', | |
'blocking' => true, | |
'headers' => array('Content-Type' => 'application/json; charset=utf-8'), | |
'cookies' => array(), | |
'body' => array('message' => $msg), | |
) | |
); | |
} | |
//射後不理拉~ | |
} | |
add_filter('fb2wp_comment_event', 'mxp_fb_comment_callback', 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment