pidor/scripts/upd_status.sh.d/tweet

71 lines
2.0 KiB
PHP
Executable File
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

#!/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/, come in and create something awesome =) $statusTag";
$messageClosed = "We just closed our doors at %s. See you very soon... $statusTag";
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();
}