vADC Docs

Using TrafficScript to add a Twitter feed to your web site

by fmemon on ‎08-07-2012 05:59 PM - edited on ‎06-02-2015 11:50 AM by PaulWallace (2,799 Views)

6a00e5508a3ca78834016769252b72970b-800wi.jpgA great usage of TrafficScipt is for managing and inserting widgets on to your site.  The attached TrafficScript code snippet inserts a Twitter Profile Widget to your site, as described here (sign in required).

 

To use it.

 

  1. In the Stingray web interface navigate to Catalogs -> Rules and scroll down to Create new rule.  Give it a name such as Twitter Feed and select Use TrafficScript Language.  Click Create Rule to create the rule.
  2. Copy and paste the code and save the rule.
  3. Modify the $user and $tag as described in the TrafficScript code snippet.  $user is your Twitter username and $tag specifies where in the web page the feed should go.
  4. Navigate to the Rules page of your Virtual Server (Services -> Virtual Servers -> <your virtual server> -> Rules) and add Twitter Feed as a Response Rule

 

Reload your webpage and you should see the Twitter feed.

 

#  
# This TrafficScript code snippet will insert a Twitter Profile widget  
# for user $user as described here:  
# https://twitter.com/about/resources/widgets/widget_profile  
  
# The widget will be added directly after $tag. The resultant page will  
# look like:  
# ...  
# <tag>  
# <Twitter feed>  
# ...  
#  
  
# Replace 'riverbed' with your Twitter username  
$user = "riverbed";  
  
#  
# You can keep the tag as <!--TWITTER FEED--> and insert that tag into  
# your web page or change $tag to some existing text in your web page, ie  
# $tag = "Our Twitter Feed:"  
$tag = "<!--TWITTER FEED-->";  
  
# Only modify text/html pages  
if( !string.startsWith( http.getResponseHeader( "Content-Type" ), "text/html" )) break;  
  
#  
# The actual code that will be inserted. Various values such as color,  
# height, width, etc. can be changed here.  
#  
$html = "\n\  
<script charset=\"utf-8\" src=\"http://widgets.twimg.com/j/2/widget.js\"></script> \n \  
<script> \n \  
new TWTR.Widget({ \n \  
  version: 2, \n \  
  type: 'profile', \n \  
  rpp: 4, \n \  
  interval: 30000, \n \  
  width: 250, \n \  
  height: 300, \n \  
  theme: { \n \  
  shell: { \n \  
  background: '#333333', \n \  
  color: '#ffffff' \n \  
  }, \n \  
  tweets: { \n \  
  background: '#000000', \n \  
  color: '#ffffff', \n \  
  links: '#eb8507' \n \  
  } \n \  
  }, \n \  
  features: { \n \  
  scrollbar: false, \n \  
  loop: false, \n \  
  live: false, \n \  
  behavior: 'all' \n \  
  } \n \  
}).render().setUser('".$user."').start(); \n \  
</script><br>\n";  
  
# This section inserts $html into the HTTP response after $tag  
$body = http.getResponseBody();  
$body = string.replace( $body, $tag, $tag. $html);  
http.setResponseBody( $body );  

 

Give it a try and let us know how you get on!

 

More Twitter solutions:

 

Contributors