Twitter just announced a new API called @Anywhere. This new API allows integrating Twitter into your site dead simple. There are several different features you can integrate with your site. These include automatic linking to names on the page or in a specific area, hover cards, which are popups that show user info, follow button for users, an inline iFrame tweet box and finally twitter connect, which is the same basic idea as Facebook connect. This tutorial is going to show you how to integrate some of these feature into your own site.
Below you will find an example of some of the ways you could use this in the real world. Imagine you have a post on your site where you tag the user's twitter name. You can then display a twitter follow button for the author or have a tweet box so readers can post comments on twitter or tell others about the work.
My Sweet Twitter Post
Written by Charlie Key (@zwigby)
Lorem ipsum dolor sit amet, congue vitae, etiam sed consequat volutpat ac volutpat. Lorem dui arcu vitae, velit sit. Aliquam bibendum scelerisque ultricies nulla tellus cursus, in orci. Ante placerat donec et aptent, feugiat quam. Turpis vel eu vitae, wisi rutrum semper quos dui vitae dui, soluta natoque tempor, et dolor. A lectus sit per vestibulum in etiam, massa vel, mi ac.
Dictum mi. Sit vestibulum fusce faucibus eget, interdum enim neque ullamcorper amet, quisque maecenas nibh integer lacus faucibus a, in ipsum id, lacus sollicitudin. Ac suscipit diam justo turpis, aliquam etiam pretium vulputate leo viverra, laoreet dui libero, odio aenean lectus fusce sollicitudin tincidunt. Dapibus tortor ac penatibus ipsum elit, tellus lacus duis dis odio in blandit, magnis lectus nunc turpis. Lorem pellentesque molestie sodales ac molestie eget, est ac, arcu lectus posuere diam tincidunt justo, risus vel mi mauris elit at sodales. Vel mi urna augue sed lorem. Sit curabitur. Proin mauris tortor fusce nulla, nonummy in ultrices magna in, in nunc tincidunt feugiat potenti quis. Eros purus senectus et vitae erat vivamus, hendrerit euismod dolor. Nulla vel aenean quasi adipiscing tellus.
Nulla integer sit, fusce vehicula ante a et ut metus, in non congue turpis purus praesent, libero metus sed ac in fringilla nam. Adipiscing justo diam semper velit parturient, nam wisi lorem massa integer. Quisque quis cras odio phasellus integer nec, id tincidunt inceptos, quam dapibus quis. Pede nascetur hymenaeos, eu sociosqu eu placerat lorem, donec sagittis id. Et lectus malesuada nunc felis eu a, cras fringilla posuere. Wisi mattis sapien ultricies mauris eu, blandit elit tellus mauris et netus rutrum, ullamcorper lectus scelerisque neque, donec quis leo sed donec vestibulum quisque. Praesent pede nunc ante vestibulum mauris, eleifend ut quis eget dui tellus. A congue, imperdiet sed cursus nunc integer. Ligula enim ut. Lectus vel venenatis ultrices lectus ligula odio, eleifend sociis malesuada. Nonummy libero ligula semper, ligula ut mauris eu. Cras suspendisse tellus, placerat neque velit, urna ornare consectetuer ac aliquet.
To get started with @anywhere you first need to register an application over at the Twitter Dev Site. I have had problems with changing the callback url and application url after creating the application, so double check to make sure they are correct. Once created you should be sitting at the manage application page.
Once your application has been registered, actually getting something working is really easy. All we need to do is make sure we include the correct JavaScript file and then ask the API to link all twitter usernames on the page. One item to make sure is that the id variable passed to the file is your API Key, which can be found on your manage application page.
</script>
<script type="text/javascript">
twttr.anywhere(function(twitter) {
// enable links for @names
twitter().linkifyUsers();
});
</script>
You can easily take this one step further by changing the simple links to hover cards. Hover cards can be found in the example above, but basically they look like the following. They popup on hovering over a twitter @name. The code is simple to update for hover cards.
// enable links for @names
twitter().hovercards();
});
The hover cards are one of the cooler and useful options available. The next item we are going to take a look at is the follow button. As with everything else it is really easy to add a follow button. However, this time we have to tell the button where to appear, this is done by passing a selector to the twitter object. The selectors are the same as jQuery Selectors. Once you identify where you want the button, you call followButton with a single string parameter, the user name for the button. The code for a basic follow button is below. First we have some html that would be where we want to place the button and second is the JavaScript to do it.
// enable links for @names
twitter("#author-follow-button").followButton("zwigby");
});
This leads to something like the following.
The final item we are going to add today to the page is going to be an inline tweet box, you can, of course, customize the details of the box including the size, the default content, and the title. The configuration options are passed using a JavaScript object. You tell the twitter object where to place the box and then call tweetBox with the options object. The full list of options is available over at the @Anywhere documentation. We are really only going to worry about two for this example, label and defaultContent. The label, which controls the typical "What's Happening?", has been changed in our example above to "Love this article? Tell others!" and I also added some default content. The code to add the box is below. Again, first with a div to place it in and second the JavaScript.
label: "Love this article? Tell others!",
defaultContent: "I learned how to use Twitter @anywhere over at @switchonthecode http://sotc.me/32291"
});
To finish off with good measure, here is the exact code used for the example above. You'll notice that you can use selectors for all of the items to narrow down the area where you want to linkify or create hover cards.
</script>
<script type="text/javascript">
twttr.anywhere(function(twitter) {
// enable hovercards on @names
twitter('#sweet-post-content').hovercards();
// add a follow button
twitter('#follow-author').followButton("zwigby");
//
twitter('#twitter-shoutout').tweetBox({
label: "Love this article? Tell others!",
defaultContent: "I learned how to use Twitter @anywhere over at @switchonthecode http://sotc.me/32291"
});
});
</script>
<div id="sweet-post-content">
<h3>My Sweet Twitter Post</h3>
<p>Written by Charlie Key (@zwigby)</p>
<p>Lorem ipsum dolor sit amet, congue vitae, etiam sed consequat volutpat ac volutpat. Lorem dui arcu vitae, velit sit. Aliquam bibendum scelerisque ultricies nulla tellus cursus, in orci. Ante placerat donec et aptent, feugiat quam. Turpis vel eu vitae, wisi rutrum semper quos dui vitae dui, soluta natoque tempor, et dolor. A lectus sit per vestibulum in etiam, massa vel, mi ac.</p>
<p>Dictum mi. Sit vestibulum fusce faucibus eget, interdum enim neque ullamcorper amet, quisque maecenas nibh integer lacus faucibus a, in ipsum id, lacus sollicitudin. Ac suscipit diam justo turpis, aliquam etiam pretium vulputate leo viverra, laoreet dui libero, odio aenean lectus fusce sollicitudin tincidunt. Dapibus tortor ac penatibus ipsum elit, tellus lacus duis dis odio in blandit, magnis lectus nunc turpis. Lorem pellentesque molestie sodales ac molestie eget, est ac, arcu lectus posuere diam tincidunt justo, risus vel mi mauris elit at sodales. Vel mi urna augue sed lorem. Sit curabitur. Proin mauris tortor fusce nulla, nonummy in ultrices magna in, in nunc tincidunt feugiat potenti quis. Eros purus senectus et vitae erat vivamus, hendrerit euismod dolor. Nulla vel aenean quasi adipiscing tellus.</p>
<div id="follow-author"></div>
<br>
<div id="twitter-shoutout"></div>
</div>
Now there is much more you can do with the @Anywhere api including connecting to twitter's user backend. That, however, is for another tutorial. I hope this tutorial and example have shown you a few of the many possibilities available with the @Anywhere platform. If you have any questions feel free to leave a comment.
03/08/2011 - 15:31
Fantastic Post!
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.