Site icon Web Niraj

Twitter API: Tweet Button Callbacks

Recently, I’ve been creating an app that rewards users for tweeting messages. However, I wanted to avoid using OAuth authentication and full integration of the Twitter API but still be able to reward users for tweeting. Luckily, I found a way of doing so using the Tweet button and something called Web Intents.

The Twitter JavaScript API needs to be used (which is simple to integrate by copy-and-pasting the plugin code into your page). The functionality works as follows:

  1. User presses tweet button
  2. User logs into twitter (in popup) if they are not already logged in
  3. User previews message and confirms the tweet (in popup)
  4. Tweet is posted and popup closes
  5. JavaScript callback is triggered

The Tweet button handles steps 2-5 above so the only points of integration required are adding the tweet button and the callback implementation.

I integrated the Tweet button as follows:

See the gist on github.

Then, I added some extra JavaScript to the page to listen for the callback and trigger the function to reward the user:

See the gist on github.

Once a tweet has been posted, twitter will trigger the tweet event. The web intent we created will listen for the tweet event and then trigger the reward_user function.

Simples!

UPDATE (28th April 2014):

The code has been updated after Ryan spotted that the previous code was no longer working. You can see the working version of the new code on JSFiddle.

Exit mobile version