Skip to content

Instantly share code, notes, and snippets.

@CodeNinja89
Last active December 24, 2015 03:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save CodeNinja89/6741419 to your computer and use it in GitHub Desktop.
Save CodeNinja89/6741419 to your computer and use it in GitHub Desktop.
Twitter Android app using Cordova/PhoneGap inAppBrowser plugin. Uses Codebird-js library to log in (using OAuth authorization) and use Twitter API. Uses callback URL instead of PIN-based authorization.
<!DOCTYPE html>
<html>
<head>
<title>MooTweet</title>
<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="js/jquery-min.js"></script>
<script type="text/javascript" charset="utf-8" src="js/sha1.js"></script>
<script type="text/javascript" charset="utf-8" src="js/codebird.js"></script>
<script type="text/javascript" charset="utf-8" src="js/some.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body onload="onLoad()">
<div class="bar">
<div id="links">
<a href="home.html" id="1">Home</a>
<a href="me.html" id="2">Me</a>
<a href="compose.html" id="3">Compose</a>
</div>
</div>
<div id="LoadMe">
<ul id="homeTimeline">
</ul>
</div>
<script>
$('a').click(function() {
loadOption($(this).attr('href'), $(this).attr('id'));
return false;
});
</script>
</body>
</html>
<!-- the LoadMe div can be updated with the following snippets.
You should make different html files for each snippet. -->
<ul id="userTimeline"></ul> <!-- this is where user's timeline will go (me.html) -->
<ul id="homeTimeline"></ul> <!-- this is where user's home timeline will go (home.html) -->
/* Some Globals */
var cb = new Codebird; // we will require this everywhere
var interval; // for setting intervals.
/* thanks to amwelles / widget.html (https://gist.github.com/amwelles/5776750)
* for the following function.
* simple and elegant.
*/
function parseText(text) {
var link = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var user = /@(\w+)/ig;
var hashTags = /#(\w+)/ig;
var desc = "";
if(link) {
desc = text.replace(link,'<a href="$1" target="_blank">$1</a>');
} if(user) {
desc = desc.replace(user,'<a href="https://twitter.com/$1" target="_blank">@$1</a>');
} if(hashTags) {
desc = desc.replace(hashTags,'<a href="https://twitter.com/search?q=%23$1" target="_blank">#$1</a>');
}
return desc;
}
function showHomeTimeline(count) {
cb.__call(
"statuses_homeTimeline", {"count": count},
function(reply) {
for (var key in reply) {
$('#homeTimeline').prepend('<li><p>' + reply[key].user["screen_name"] +': ' + parseText(reply[key].text) +'</p></li>');
}
}
);
}
function showUserTimeline() {
cb.__call(
"statuses_userTimeline", {"count": "5"},
function(reply) {
for (var key in reply) {
$('#userTimeline').prepend('<li><p>' + reply[key].user["screen_name"] +': ' + parseText(reply[key].text) +'</p></li>');
}
}
);
}
function autoReply(id, name) {
var params = {
"in_reply_to_status_id": id,
"status": "@" + name + " Hi, I'm driving, I'll get back to you ASAP!"
};
cb.__call(
"statuses_update", params,
function(reply) {
// something will go here
}
);
}
function fetchTweets(id) {
$.ajaxSetup({
cache: false
});
var params = {
"include_rts": "1",
"count": "5",
"since_id": id
};
var home = setInterval(function() {showHomeTimeline(5)}, 300000);
// get at most five most recent mentions.
var ref = setInterval(
function() {
cb.__call(
"statuses_mentionsTimeline", params,
function (reply) {
if(reply){
for(var key in reply){
autoReply(reply[key].id, reply[key].user["screen_name"]); // auto reply to the tweet where I'm mentioned.
}
}
}
);
}, 300000);
}
function loadOption(href, id) {
// load the page irrespective of what it is.
$('#LoadMe').load(href).hide().fadeIn("slow");
// then check what it is and load the content accordingly.
if(Number(id) == 1) {
clearInterval(interval);
showHomeTimeline(5);
interval = setInterval(function() {showHomeTimeline()}, 300000);
} else if(Number(id) == 2) {
clearInterval(interval);
showUserTimeline();
interval = setInterval(function() {showUserTimeline()}, 300000);
} else {
// this is a placeholder for now.
}
}
function sendTweet() {
var param = {"status": $('#tweet').val()};
cb.__call(
"statuses_update", param,
function(reply) {
// somethimg here
}
);
$('#LoadMe').load("me.html").hide().fadeIn("slow");
showUserTimeline();
}
/**************************** Let the coding begin ****************************/
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
cb.setConsumerKey("THE KEY TO", "YOUR SECRET");
var id;
// check if we already have access tokens
if(localStorage.accessToken && localStorage.tokenSecret) {
// then directly setToken() and read the timeline
cb.setToken(localStorage.accessToken, localStorage.tokenSecret);
showHomeTimeline(20);
cb.__call(
"statuses_mentionsTimeline", {"count": "1"},
function (reply) {
for(var key in reply){
autoReply(reply[key].id, reply[key].user["screen_name"]); // auto reply to the tweet where I'm mentioned.
id = reply[key].id;
}
}
);
// now poll periodically and send an auto-reply when we are mentioned.
fetchTweets(id);
} else { // authorize the user and ask her to get the pin.
cb.__call(
"oauth_requestToken",
{oauth_callback: "http://localhost/"},
function (reply) {
// nailed it!
cb.setToken(reply.oauth_token, reply.oauth_token_secret);
cb.__call(
"oauth_authorize", {},
function (auth_url) {
var ref = window.open(auth_url, '_blank', 'location=no'); // redirection.
// check if the location the phonegap changes to matches our callback url or not
ref.addEventListener("loadstart", function(iABObject) {
if(iABObject.url.match(/localhost/)) {
ref.close();
authorize(iABObject);
}
});
}
);
}
);
}
}
function authorize(o) {
var currentUrl = o.url;
var query = currentUrl.match(/oauth_verifier(.+)/);
for (var i = 0; i < query.length; i++) {
parameter = query[i].split("=");
if (parameter.length === 1) {
parameter[1] = "";
}
}
cb.__call(
"oauth_accessToken", {oauth_verifier: parameter[1]},
function (reply) {
cb.setToken(reply.oauth_token, reply.oauth_token_secret);
localStorage.accessToken = reply.oauth_token;
localStorage.tokenSecret = reply.oauth_token_secret;
cb.__call(
"statuses_homeTimeline", {},
function (reply) {
for (var key in reply) {
$('#homeTimeline').append('<li><p>' + reply[key].user["screen_name"] +': ' + parseText(reply[key].text) +'</p></li>');
}
}
);
}
);
fetchTweets(null);
}
@VipulSolanki90
Copy link

Nice plugin. (Y) :) But i am not able to logout from the twitter account i used to authentication. Could you please provide the code to logout from the twitter account so that another user can do the login and authentication process.

Thanks in advance :)

@mayurloved
Copy link

Nice but not support in ios :(

@sourcebits-gokul
Copy link

Hey how to do retweet using codebird.js ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment