Skip to content

Instantly share code, notes, and snippets.

@rickymoorhouse
Created January 19, 2010 15:04
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rickymoorhouse/280986 to your computer and use it in GitHub Desktop.
<?php
require_once('twitter/twitteroauth.php');
define("CONSUMER_KEY","");
define("CONSUMER_SECRET","");
define("SOURCE","");
define("OAUTH_TOKEN","");
define("OAUTH_TOKEN_SECRET","");
function getConnectionWithAccessToken($oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
return $connection;
}
function sendToTwitter($message) {
$connection = getConnectionWithAccessToken(OAUTH_TOKEN,OAUTH_TOKEN_SECRET);
$content = $connection->post("statuses/update", array(
"status" => $message,
"source" => SOURCE
));
if (empty($content)) {
return "Failed sending to twitter: '".$message;
} else {
return "Sent message to twitter: '".$message;
}
}
function relative_date($d) {
// Format time
$the_time = date("g:ia",$d);
// Start of today
$s_today = strtotime(date("Y-m-d"));
// Start of tomorrow
$s_tomorrow = $s_today + 86400;
// Start of tomorrow+1
$s_dayafter = $s_tomorrow + 86400;
$diff = time() - $d;
if ($d > $s_dayafter) { // This is after tomorrow
return date("D jS F",$d) . " at $the_time";
} elseif ($d > $s_tomorrow) { // This is tomorrow
return "tomorrow at $the_time";
} elseif ($d > $s_today) { // This is today
if (date("G",$d) > 17) {
return "this evening at $the_time";
} elseif (date("G",$d) > 12) {
return "this afternoon at $the_time";
} else {
return "today at $the_time";
}
}
}
function decode_message($mail) {
// Get the text encoded version
$mail = str_replace("\r","",$mail);
print "\n\n";
preg_match("/base64\n+([^\\=]+=)/",$mail, $a);
//var_dump($a);
$text = base64_decode($a[1]);
//var_dump($text);
//var_dump($text);
// Find the title
preg_match("/Title: ([^\\r\\n]+)/",$text, $b);
$title = $b[1];
// Find the location
preg_match("/Where: ([^\\r\\n]+)/",$text, $b);
$where = $b[1];
//Get the time
//Time: Mon 14 Sep 12:15 – 13:15
preg_match("/When: \w+ (\d+) (\w+) (\d+):(\d+)/",$text, $b);
$time = $b[1] . " " . $b[2] . " " . date("Y") . " " . $b[3] . ":" . $b[4] . ":00";
$t = strtotime($time);
$message = $title . " " . relative_date($t) . ", " . $where;
return $message;
}
// Load e-mail content
//$mail = $HTTP_RAW_POST_DATA;
// Additional code to receive posts from postmarkapp
$mail = file_get_contents('php://input');
$m = json_decode($mail);
$text = $m->TextBody;
/*
"TextBody": "This is a reminder for:\r\n\r\nTitle: test twitter account\r\nWhen: Thu 26 Jan 19:00 – 20:00 London\r\nCalendar: Momentum\r\nWho:\r\n * paulsgrovemen@gmail.com- organiser\r\n\r\nEvent details: \r\nhttps://www.google.com/calendar/event?action=VIEW&eid=MTQ5aTFwNXUycGJ2aHJwdTBjZnFtdGRhdTQgcGF1bHNncm92ZW1lbkBt&tok=MjMjcGF1bHNncm92ZW1lbkBnbWFpbC5jb20yMWFkYzdiODIxNTUxMDA5ZTdhYTQ1NDZhYjU0YWVjODk2ZGZjMTZh&ctz=Europe%2FLondon&hl=en_GB\r\n\r\nInvitation from Google Calendar: https://www.google.com/calendar/\r\n\r\nYou are receiving this email at the account paulsgrovemen@gmail.com because \r\nyou set a reminder for this event on the calendar Momentum.\r\n\r\nYou can change your reminders for specific events in the event details page \r\nin https://www.google.com/calendar/.\r\n"
*/
// Find the title
preg_match("/Title: ([^\\r\\n]+)/",$text, $b);
$title = $b[1];
// Find the location
preg_match("/Where: ([^\\r\\n]+)/",$text, $b);
$where = $b[1];
//Get the time
//Time: Mon 14 Sep 12:15 – 13:15
preg_match("/When: \w+ (\d+) (\w+) (\d+):(\d+)/",$text, $b);
$time = $b[1] . " " . $b[2] . " " . date("Y") . " " . $b[3] . ":" . $b[4] . ":00";
$t = strtotime($time);
$message = $title . " " . relative_date($t) . ", " . $where;
// Send to twitter
print sendToTwitter($message);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment