pidor/scripts/upd_status.sh.d/tweet

71 lines
2.1 KiB
PHP
Executable File

#!/usr/bin/php
<?php
/*
* Tezza (@Kaweechelchen | tezza@syn2cat.lu)
* https://github.com/J7mbo/twitter-api-php
* */
// Set the correct timezone
date_default_timezone_set( 'Europe/Luxembourg' );
// Set a statusTag you want to append to all the messages
$statusTag = '#statusUpdate';
// Set the messages to tweet when the status of the hackspace changes
// %s is going to be replaced by the current time to avoid duplicate tweets on Twitter
$messageOpen = "It's %s and we are open \o/ \ncome in and create something awesome =) \nhttps://Level2.lu/openingTimes";
$messageClosed = "We just closed our doors at %s \nSee you very soon... \nhttps://Level2.lu/openingTimes";
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
/*
$settings = array(
'oauth_access_token' => 'xxxx',
'oauth_access_token_secret' => 'yyyy',
'consumer_key' => 'xxxxx',
'consumer_secret' => 'yyyyy'
);
*/
require_once('twittertokens.php'); /* put the tokens inside separate file which is in .gitignore */
/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';
// Get the current and previous status from the parameters handed over to the script
$currentStatus = $argv[1];
$previousStatus = $argv[2];
// Chack if the status has changed
if ( $currentStatus != $previousStatus ) {
// based on the new status, define the message to tweet
switch ( $currentStatus ) {
case 'open':
$status = sprintf( $messageOpen, date( 'H:i' ) );
break;
case 'closed':
$status = sprintf( $messageClosed, date( 'H:i' ) );
break;
}
// POST fields required by the URL above. See relevant docs as above
$postfields = array(
'status' => $status
);
// Perform a POST request and echo the response
$twitter = new TwitterAPIExchange($settings);
// build the tweet and send it out
$twitter
->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
}