Twitter and Flash, twitter in your FLASH movie, getting the tweets, catching some significant words and provoke a FLASH movie reaction to your tweets....
Getting your twitter timeline is just using the rssfeed of your twitter account, for example contrechoc tweets:
http://www.contrechoc.com/crosslab/twitterRSS/twitter.php
a PHP file which consists of:
$twitterFeed = 'http://twitter.com/statuses/user_timeline/111370674.rss';
$outputFeed = @file_get_contents($twitterFeed);
print $outputFeed;
so the most important line is
http://twitter.com/statuses/user_timeline/111370674.rss
everyone can do this, you can get your twitter number here:
http://api.twitter.com/1/users/show.xml?screen_name=contrechoc
instead of contrechoc, put your own twittername.
or use:
http://idfromuser.com to get your twitter unique key.
Then you see the number above the name line. (This page is also a nice example of XML.)
This page gives simple examples how to use twitter data on your site using php:
http://www.problogdesign.com/wordpress/how-to-get-your-twitter-follower-number-in-plain-text/
More difficult is making a tweet.
Some special twitter vocabulary:
Sending a tweet is called "Updating status". Getting the tweets is called: "Getting Timeline". Then there is: "Sending and receiving direct messages".
For these acctions to be programmed we need to log in into our twitter account, which was rather easy some time ago. The script for this, often done by a programmer, needed to incorporate the login and password. But this was giving your credentials away, letting your password lying around on a desk of somebody else.
Nowadays the script can be made without giving away your credentials using OAUTH. But this security makes life much more complex.
We have to know a little bit about OAUTH, which cannot be done using FLASH. We need a PHP library as an intermediate between the "database" Twitter and our FLASH movie.
What is OAUTH?
http://dev.twitter.com/pages/oauth_faq
On this page there is an example illustrating the steps to get an app functioning with OAUTH:
http://blogs.sitepoint.com/2010/08/17/oauth-explained-with-foursquar/
Go to
http://foursquare.com/ and experience how this looks.
This is a nice graphical representation of this OAUTH authentication process:
http://developers.new.digg.com/authentication
The PHP library we use is the lib of Abraham Williams, which you can get here:
https://github.com/abraham/twitteroauth
Then you need your sercret key and token as described in the documentation at
http://dev.twitter.com/pages/auth
and change the files using your own secret key.
At this lib I added a simple PHP file, which after logging in just writes an update Status:
echo $connection->post('statuses/update', array('status' => $statusText.' at '.date(DATE_RFC822)));
http://www.contrechoc.com/crosslab/abrahamTwitter/writeStatusShort.php?statusText=hello world
From this last line Hello World can be twittered.... (if you are logged in...see OAUTH description).
So putting the things together gives a movie, getting the last tweets, and being able to submit a tweet:
http://www.contrechoc.com/crosslab/abrahamTwitter/twitterFlashWithInput.html
of course as an example this is giving contrechoc's tweets (rssfeed)- submitting tweets is only possible when logged in...the redirection page has to be altered.
FLASH
Then we return to FLASH scripting, to get a media response to a tweet, we will search for a string in a bit of text (tweet = text):
for this we need a RegExp object:
var myPattern:RegExp = /bestseller/gi;
then we can search a String for this RegExp : (
var bitOfText: String = "when the sea of inspiration is empty, all fishing for new ideas is in vain, unless you are a bestseller";
//coming from a tweet for example
if ( bitOfText.search(myPattern1) != -1 )
{
trace ("found ", myPattern );
//do something with it!
}
When we have found "bestseller", then we can use the media template to show an image, or start a video, sound etc.
We need the FLASH movie also to get updates itself. For this we use a timer:
var myTimer: Timer;
myTimer = new Timer(500);
myTimer.addEventListener("timer", timerHandler);
myTimer.start();
function timerHandler(event:TimerEvent):void {
//function to get rssfeed from twitter and go to search for certain words
}
The file for this last example can be found here:
http://www.contrechoc.com/crosslab/twitter_media_Example.zip
When the FLASH movie is not the right version, you can take the script from the textfile in the folder, take a new FLASH file and copy this script inside the actions window. To get your own tweets inside a flash, you should alter the rssFeedURL inside the script.
To write yourself tweets from your FLASH, as told, you need the whole bunch of PHP files and get yourself secret keys etc from twitter.
This is an example of FLASH with OAUTH, which won't really work for you, because it is works only when I log in....
http://www.contrechoc.com/crosslab/abrahamTwitter/twitterFlashWithInput.html
Extra:
search a word in all tweets:
http://search.twitter.com/search.atom?q=hello