Writing your own module
| Introduction This guide assumes you have intermediate knowledge of PHP + Javascript. Structure Each module is stored in the modules directory. Modules are used to add additional functionality to the chat bar. For example, you may want to add a module which allows the user to view your twitter updates. Let us call the name of our module- twitterupdates Create a folder called twitterupdates in the modules folder Create a file called index.php. This file will be called by CometChat. <?php echo "Hello world!"; ?> Add an icon called twitterupdates.png in the themes/default/images/icons folder and your basic module structure is now ready. Add the module to your configuration file and refresh your site. You will now see your module (on the bottom left)! Adding functionality Create a file called twitter.php with the following contents:
<?PHP
/* tweetPHP, PHP twitter Class By Tim Davies
Requires cURL and SimpleXML (both standard) */
class twitter {
function __construct($user) {
$this->user = $user;
}
public function fetch_tweets($tweetlimit = 5, $charlimit = False, $limit= 42) {
$ch = curl_init();
$login = $this->login;
$target = 'http://twitter.com/statuses/user_timeline.xml';
curl_setopt($ch, CURLOPT_URL, $target);
curl_setopt($ch, CURLOPT_USERPWD, $login);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$getweet = curl_exec($ch);
$twitters = new SimpleXMLElement($getweet);
//error reporting
if(array_key_exists('@attributes', $twitters)){
//die("<b>Fatal Error</b> Twitter is currently unavaliable");
}
$counter = 0;
foreach ($twitters->status as $twit) {
$twiturl = 'http://twitter.com/'. $this->user .'/statuses/'. $twit->id;
$created = substr($twit->created_at,0,16);
if(++$counter > $tweetlimit) {
break;
}else{
if($charlimit){
if(strlen($twit->text) > $charlimit) {
$tweet = substr($twit->text, 0 , $limit)."...";
}else{
$tweet = substr($twit->text, 0 , $limit);
}
}else{
$tweet = $twit->text;
}
echo '<li class="tweet">'. $tweet .'</li>';
}
}
}
}
?>
Edit the existing index.php file and add the following contents:
[code]
<?php
include dirname(__FILE__)."/twitter.php";
$twitter = new twitter('USERNAME');
$tweets = $twitter->fetch_tweets(10);
echo $tweets;
?>
|



