Created
August 26, 2014 02:58
-
-
Save EricTendian/b03dbeb99d274fa99bc6 to your computer and use it in GitHub Desktop.
A webhook for safety alert emails, built to work with the Illinois Institute of Technology's alert system. This script posts alert messages to a Facebook group as soon as the POST request is made.
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 | |
/** | |
* IIT Alert notification app | |
* Created by: Eric Tendian | |
* This script is meant to be kicked off by cloudmailin.com upon a new email. | |
* | |
* Access token last generated: 25 August 2014 | |
* | |
* To generate a new token, first visit the URL: | |
* https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=[YOUR APP ID]&redirect_uri=[YOUR REDIRECT URL]&scope=user_groups,publish_actions | |
* Then take the token from that response and go to the URL: | |
* https://graph.facebook.com/oauth/access_token?client_id=[YOUR APP ID]&client_secret=[YOUR APP SECRET]&grant_type=fb_exchange_token&fb_exchange_token=EXISTING_ACCESS_TOKEN | |
* | |
* You will then have an access token that lasts 60 days. | |
* | |
* For reference: | |
* app id: [YOUR APP ID] | |
* app secret: [YOUR APP SECRET] | |
*/ | |
if (strpos($_POST['headers']['Subject'], '[IIT Alert]')===false) die; | |
$access_token = ''; // 60-day token goes here | |
$groupid = ''; // group ID to post to | |
$message = "--- IIT ALERT NEWS FLASH --- | |
Date: ".$_POST['headers']['Date']." | |
Subject: ".$_POST['headers']['Subject']." | |
".$_POST['plain']." | |
--- END FLASH --- | |
As always, register for IIT Alert here: http://web.iit.edu/iitalert"; | |
$graph_url = "https://graph.facebook.com/v2.1/$groupid/feed"; | |
$postData = "message=".urlencode($message)."&access_token=".$access_token."&format=json&method=post&pretty=0&suppress_http_code=1"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $graph_url); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
echo $output; | |
error_log($output); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment