From c6b99bec64ee76b2e8e86ea949e4b831db675f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mun=CC=83oz?= Date: Wed, 5 Nov 2014 13:21:56 +0100 Subject: [PATCH 1/2] Loading plugins after configuration --- bot/bot.lua | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/bot/bot.lua b/bot/bot.lua index 032c486..d088eac 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -4,8 +4,6 @@ -- lrexlib = require("rex_pcre") VERSION = 'v0.6' - - plugins = {} -- taken from http://stackoverflow.com/a/11130774/3163199 function scandir(directory) @@ -16,16 +14,7 @@ end return t end - - -- load all plugins in the plugins/ directory - for k, v in pairs(scandir("plugins")) do - if not (v:sub(0, 1) == ".") then - print("Loading plugin", v) - t = loadfile("plugins/" .. v)() - table.insert(plugins, t) - end - end - + function on_msg_receive (msg) if msg_valid(msg) == false then return @@ -336,11 +325,6 @@ end end - -- Start and load values - config = load_config() - our_id = 0 - now = os.time() - function get_receiver(msg) if msg.to.type == 'user' then return 'user#id'..msg.from.id @@ -350,7 +334,6 @@ end end - function on_our_id (id) our_id = id end @@ -373,3 +356,23 @@ function on_binlog_replay_end () started = 1 end + + + + -- Start and load values + config = load_config() + our_id = 0 + now = os.time() + + -- load plugins + plugins = {} + + -- load all plugins in the plugins/ directory + for k, v in pairs(scandir("plugins")) do + if not (v:sub(0, 1) == ".") then + print("Loading plugin", v) + t = loadfile("plugins/" .. v)() + table.insert(plugins, t) + end + end + From ef12c413a74655212a085cc0119a5cb0ee88fa15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mun=CC=83oz?= Date: Wed, 5 Nov 2014 13:22:07 +0100 Subject: [PATCH 2/2] Adding twitter plugin --- bot/config.json | 10 +++++-- plugins/twitter.lua | 70 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 plugins/twitter.lua diff --git a/bot/config.json b/bot/config.json index 8bdb0a2..6dcdc89 100644 --- a/bot/config.json +++ b/bot/config.json @@ -2,5 +2,11 @@ "log_file": "/var/www/html/log.txt", "sh_enabled": false, "sudo_users": [ 0, 1 ], - "values": { } -} \ No newline at end of file + "values": { }, + "twitter": { + "consumer_key" : "", + "consumer_secret" : "", + "access_token" : "", + "access_token_secret" : "" + } +} diff --git a/plugins/twitter.lua b/plugins/twitter.lua new file mode 100644 index 0000000..b40899c --- /dev/null +++ b/plugins/twitter.lua @@ -0,0 +1,70 @@ +local OAuth = require "OAuth" + +local consumer_key = config.twitter.consumer_key +local consumer_secret = config.twitter.consumer_secret +local access_token = config.twitter.access_token +local access_token_secret = config.twitter.access_token_secret + +local client = OAuth.new(consumer_key, consumer_secret, { + RequestToken = "https://api.twitter.com/oauth/request_token", + AuthorizeUser = {"https://api.twitter.com/oauth/authorize", method = "GET"}, + AccessToken = "https://api.twitter.com/oauth/access_token" +}, { + OAuthToken = access_token, + OAuthTokenSecret = access_token_secret +}) + +function run(msg, matches) + + local twitter_url = "https://api.twitter.com/1.1/statuses/show/" .. matches[1] .. ".json" + + print(twitter_url) + + local response_code, response_headers, response_status_line, response_body = client:PerformRequest("GET", twitter_url) + print(response_body) + local response = json:decode(response_body) + + print("response = ", response) + + local header = "Tweet from " .. response.user.name .. " (@" .. response.user.screen_name .. ")\n" + local text = response.text + + -- replace short URLs + if response.entities.url then + for k, v in pairs(response.entities.urls) do + local short = v.url + local long = v.expanded_url + text = text:gsub(short, long) + end + end + + -- remove images + local images = {} + if response.entities.media then + for k, v in pairs(response.entities.media) do + local url = v.url + local pic = v.media_url + text = text:gsub(url, "") + table.insert(images, pic) + end + end + + -- send the parts + local receiver = get_receiver(msg) + send_msg(receiver, header .. "\n" .. text, ok_cb, false) + for k, v in pairs(images) do + local file = download_to_file(v) + send_photo(receiver, file, ok_cb, false) + end + + return nil +end + +return { + description = "Shows a tweet", + usage = "", + patterns = {"https://twitter.com/[^/]+/status/([0-9]+)"}, + run = run +} + +