From 4b5bf08fe9d3cf1d2c097f23f869154a5988bf71 Mon Sep 17 00:00:00 2001 From: Thomas Menari Date: Mon, 11 Mar 2013 19:30:00 +0000 Subject: [PATCH] Add identica+twitter status fetching --- modules/dent/dent.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/modules/dent/dent.js b/modules/dent/dent.js index 5638ccc..311d9d0 100644 --- a/modules/dent/dent.js +++ b/modules/dent/dent.js @@ -3,6 +3,15 @@ var request = require('request'); var dent = function(dbot) { this.dbot = dbot; + this.StatusRegex = { + identica: /\bhttps?:\/\/identi\.ca\/notice\/(\d+)\b/ig, + twitter: /\bhttps?:\/\/twitter\.com\/\w+\/status\/(\d+)\b/ig + }; + + this.StatusAPI = { + identica: "http://identi.ca/api/statuses/show.json", + twitter: "https://api.twitter.com/1/statuses/show.json" + }; this.api = { 'post': function(content) { @@ -25,6 +34,20 @@ var dent = function(dbot) { } }; + this.lookup = function(event, id, service) { + request({ + url: this.StatusAPI[service], + qs: {"id": id}, + json: true + }, function(error, response, body) { + if (!error && response.statusCode == 200) { + if (_.has(body, 'text')) { + event.reply(service + " [" + body.user.screen_name + '] ' + body.text); + } + } + }); + }; + this.commands = { '~dent': function(event) { this.api.post(event.input[1]); @@ -40,6 +63,19 @@ var dent = function(dbot) { }.bind(this)); } }.bind(this); + + this.listener = function(event) { + for (s in this.StatusRegex) { + if (this.StatusRegex.hasOwnProperty(s)) { + var matches = this.StatusRegex[s].exec(event.message); + if (matches != null) { + this.lookup(event, matches[1], s); + } + } + } + }.bind(this); + + this.on = 'PRIVMSG'; }; exports.fetch = function(dbot) {