How to Tweet Calendar Events on MyBB
Recently I had to make a plugin for one of my message boards which would send an update to Twitter whenever an user posted a new calendar event. In this tutorial I’ll explain how I’ve done it.
We will see how to shorten the event URL through bit.ly and post a new status on a Twitter account with PHP. Of course the same code can be applied to threads or posts.
Requirements
- MyBB message board
- Twitter account
- bit.ly account
The first step is registering on Twitter and bit.ly. After you’re done with those, take note of your Twitter username and password, and, most importantly, your bit.ly API key, which can be found in your Account page.
Setting Up the Plugin
At this point, we are ready to start with some code. First of all, let’s create a new empty file called mybbtwitter.php or whatever you like. Then insert this code, which is the basic stuff for any MyBB plugin.
{
return array(
"name" => "TwitterCalendar",
"description" => "Tweets calendar events",
"website" => "http://valadilene.org",
"author" => "valadilene",
"authorsite" => "http://valadilene.org",
"version" => "1.0",
"guid" => "",
"compatibility" => "*"
);
}
function valadilene_activate()
{
}
function valadilene_deactivate()
{
}
Shortening URLs
As you may already know, bit.ly is an URL shortening service. Since Twitter only allows 140 characters long statuses, we need to make our URLs short. Example:
before: http://www.example.com/forum/showthread.php?tid=614
after: http://bit.ly/somecode
Here’s a sweet PHP function that does the job (source). Add this at the end of your plugin file.
{
//create the URL
$bitly = ‘http://api.bit.ly/shorten?version=’.$version.‘&longUrl=’.urlencode($url).‘&login=’.$login.‘&apiKey=’.$appkey.‘&format=’.$format;
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == ‘json’)
{
$json = @json_decode($response,true);
return $json[‘results’][$url][’shortUrl’];
}
else //xml
{
$xml = simplexml_load_string($response);
return ‘http://bit.ly/’.$xml->results->nodeKeyVal->hash;
}
}
Now it’s time to get our hands on a PHP Twitter class, so that our forums can communicate with Twitter. This is the one I’m using in my plugin.
Calendar Event Hook
The hook we need to handle is called calendar_do_addevent_end. Let’s add this code to the plugin file.
So, valadilene_addevent is the handler function. Here’s the implementation, which uses the Twitter class we have downloaded earlier.
{
global $details, $mybb, $db;
// gets event data from the db
$q=$db->simple_select(‘events’,‘name, description, uid, starttime’,"eid={$details['eid']}",array(‘limit’=>1));
$event=$db->fetch_array($q);
// include twitter class
require_once(‘class.twitter.php’);
// get bit.ly URL
$short=make_bitly_url($mybb->settings[‘bburl’]."/calendar.php?action=event&eid={$event['eid']}",BITLY_USERNAME,BITLY_API_KEY,‘json’);
// send twitter status
$tw=new twitter();
$tw->username=TWITTER_USERNAME;
$tw->password=TWITTER_PASSWORD;
$tw->update("[".my_date('d M',$event['starttime'])."] {$event['name']} {$short}");
}
Of course you need to replace BITLY_USERNAME, BITLY_API_KEY, TWITTER_USERNAME and TWITTER_PASSWORD with your bit.ly username, the API key you’ve obtained earlier, and your Twitter username and password respectively (or, better, just define those costants at the beginning of the file).
Whenever an user on your message board will post an event in the calendar, your Twitter account will be updated with a new status like the following.
[09 Feb] Event name http://bit.ly/code
You can change this by editing the argument passed to the my_date() function call. Look at this page about date() from the PHP documentation for more information.
NOTE: my_date() is a MyBB function similar to PHP’s date(). It does extra handling such as timezone and language based on your board’s settings.
Conclusion
And that’s it. Of course you can modify the code to do anything you like. For example you could have a look at this list of MyBB hooks and extend tweeting even for threads or posts. Have fun!
Sources:
php-twitter library: http://code.google.com/p/php-twitter/
bit.ly short URLs with PHP: http://davidwalsh.name/bitly-php
Twitter does not have link juice. Merely because of their amazingly high pagerank, your tweets and even your Twitter profile page can get on the first page of Google simply with a good linking strategy