From fe63c29c3071353bb53d3a9f665c0b07567f5b77 Mon Sep 17 00:00:00 2001 From: kts Date: Mon, 18 May 2015 16:10:43 -0700 Subject: [PATCH] Added basic tweet component parsing, along with Twit error checking. --- index.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index dc0a9bc..2ef2df0 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,33 @@ Tweet Transaction example: a: You take the painting. b: @TwittMUD south a: You are on a balcony overlooking the ocean. There is a lighthouse. There are exits: north + +-------------------------------------------------------------------------------- +How does the game world structure look? + +Room-oriented: + + start { + things: { + 0: 'candle', + 1: 'thing' + }, + exits: { + 'e': 'eastern_plaza' + }, + locations: { + 'lighthouse': { + distance: 0, + direction: 'w', + location: 'plateu' + } + 'beach' + }, + users: { + 0: '123435388', + 1: '123444441' + } + } */ var Twit = require('twit'); var fs = require('fs'); @@ -31,12 +58,17 @@ var gogogo = function() { console.log(e); return 1; } - client = new Twit({ - consumer_key: config.consumer_key, - consumer_secret: config.consumer_secret, - access_token: config.access_token, - access_token_secret: config.access_token_secret - }); + try { + client = new Twit({ + consumer_key: config.consumer_key, + consumer_secret: config.consumer_secret, + access_token: config.access_token, + access_token_secret: config.access_token_secret + }); + } catch(e) { + console.log(e); + return 2; + } stream = client.stream('user', {with: 'user'}); stream.on('follow', handleFollow); stream.on('unfollow', handleUnfollow); @@ -45,13 +77,32 @@ var gogogo = function() { }; /* ================ Handler Functions (events, tweets, etc.) ================ */ /* handleTweet -Called on each tweet to this Twitter account. +Called on each tweet to this Twitter account. After some checks to ensure the tweet is intended for TwittMUD and is a command, it calls handleCommand, passing the contents of the message, who it was from, the desired lang, and the reply_id if it exists. */ var handleTweet = function(tweet) { - vol.last_tweet_id = tweet.id; + var from = tweet.user.id; + var id = tweet.id; + var id_str = tweet.id_str; + var reply_id = tweet.in_reply_to_status_id; + var text = tweet.text; + var lang = tweet.lang; + //vol.last_tweet_id = tweet.id; // does the response match - var user = tweet.text.split - console.log(tweet); + var exp = /^@(.*)\ (.*)/ + var res = exp.exec(text); + var to = res[1]; + var msg = res[2]; + // Conditions for handling messages as commands: + // 2. does the user follow us? (is in followers) + // * is the user active? (is in active users list) + // 3. is it a reply? + // * + // 4. Is the message parsable as a command? + // 1. is the message to us? (@TwittMUD) + var user = 'TwittMUD'; // TODO: replace with config.user_name + if (to.toLowerCase().indexOf(user.toLowerCase()) != -1) { + } + console.log(from + '=>' + to + ': ' + msg); return true; }; /* handleFollow