Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Now we can post messages to our own wall.

Now we can use `./0xfb post 'Hello from 0xf8c3b00k!'` to post messages
to our own wall.

Known issues:
  * We can not post multi-line messages

TODO:
  * Post messages to others' wall
  * Use `Optimist` to simplify command line argument parsing
  • Loading branch information...
commit 3843e82e8f4cedfdfc54aa472c4ea5867ac29d11 1 parent 79fa771
@PoshHsu PoshHsu authored
Showing with 68 additions and 2 deletions.
  1. +1 −1  0xfb/config.js
  2. +7 −1 0xfb/index.js
  3. +60 −0 0xfb/post.js
View
2  0xfb/config.js
@@ -76,4 +76,4 @@ exports.app_config = function() {
exports.user_config = function(filename) {
user_cfg = user_cfg || loadUserConfig(filename);
return user_cfg;
-}
+}
View
8 0xfb/index.js
@@ -12,6 +12,8 @@ var CONFIG = require('./config.js').app_config();
// function would be program option, while the second one would be
// command argument. Move to ./config.js ?
var progCommand = {
+ // Post message
+ 'post' : './post.js',
// Update photo
'uploadphoto' : './upload_photo.js',
'album' : './album.js'
@@ -25,6 +27,10 @@ var handleSetting = function(progOpt, command, cmdArg) {
// Parse command line, collect program option, command, command argrment.
var initRun = function() {
+ // [TODO] we can use Optimist to simplify command line arguments
+ // parse. Refer to:
+ // * https://github.com/substack/node-optimist
+ // * http://docs.nodejitsu.com/articles/command-line/how-to-parse-command-line-arguments
var program_option = [];
var command = '';
@@ -54,4 +60,4 @@ fbOauthClient.getAuthToken(function(access_token, expires) {
// console.log(" Expire Time: %d, now: %d", expires, parseInt(Date.now()/1000));
initRun();
-});
+});
View
60 0xfb/post.js
@@ -0,0 +1,60 @@
+var https = require('https');
+var USERCONFIG = require('./config.js').user_config();
+
+var post = function(program_option, command_argument) {
+
+ if(command_argument.join(' ').trim() === '') {
+ console.log('Oops! Even you are a hacker, you should say something!');
+ process.exit(0);
+ } else {
+ var message = command_argument.join(' ');
+ }
+
+ var request_OK = 200;
+ var profile_id = 'me'; // FIXME: why can't we post to others' wall?
+
+ var options = {
+ host: 'graph.facebook.com',
+ port: 443,
+ path: profile_id + '/feed' + '?access_token=' + USERCONFIG['fb_auth_token'],
+ method: 'POST'
+ };
+
+ var req = https.request(options, function(res) {
+ // debug message
+ // console.log('STATUS: ' + res.statusCode);
+ // console.log('HEADERS: ' + JSON.stringify(res.headers));
+
+ // set ending to utf8 in advance, or we will get a `buffer` object
+ res.setEncoding('utf8');
+
+ res.on('data', function(d) {
+ // debug message
+ // console.log(d);
+
+ if (res.statusCode === request_OK) {
+ console.log('Post message successfully with id = ' + JSON.parse(d).id);
+ } else {
+ console.log('Fail to post message, status code = ' + res.statusCode);
+ // [TODO] Handle error here, for now, just dump raw messages
+ console.log(d);
+ }
+ });
+
+ res.on('end', function() {
+ process.exit(0);
+ });
+ });
+
+ // Handle errors encountered during the request
+ req.on('error', function(e) {
+ console.log('Problem with request: ' + e.message);
+ });
+
+ req.write("message=" + message);
+ req.end();
+};
+
+exports.run = function(program_option, command_argument) {
+ post(program_option, command_argument);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.