From 94568cf25cb205b7b7c00c3e47c77301e2a72881 Mon Sep 17 00:00:00 2001 From: PotHix Date: Thu, 12 Feb 2015 20:50:53 -0200 Subject: [PATCH 1/3] Added the ability to quote users texts by chat. quotes_set does the job of saving users quotes by using the following command: !addquote text to be quoted quotes_get retrieve quotes stored by using `!quote` command. --- plugins/quotes_get.lua | 37 ++++++++++++++++++++++++++++ plugins/quotes_set.lua | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 plugins/quotes_get.lua create mode 100644 plugins/quotes_set.lua diff --git a/plugins/quotes_get.lua b/plugins/quotes_get.lua new file mode 100644 index 0000000..9663ac3 --- /dev/null +++ b/plugins/quotes_get.lua @@ -0,0 +1,37 @@ +do + local quotes_file = './data/quotes.lua' + local quotes_table + + function read_quotes_file() + local f = io.open(quotes_file, "r+") + + if f == nil then + serialize_to_file({}, quotes_file) + else + f:close() + end + return loadfile (quotes_file)() + end + + function get_quote(msg) + local to_id = tostring(msg.to.id) + local quotes_phrases + + quotes_table = read_quotes_file() + quotes_phrases = quotes_table[to_id] + + return quotes_phrases[math.random(1,#quotes_phrases)] + end + + function run(msg, matches) + return get_quote(msg) + end + + return { + description = "retrieves random quotes", + usage = "!quote", + patterns = { + "^!quote$"}, + run = run + } +end diff --git a/plugins/quotes_set.lua b/plugins/quotes_set.lua new file mode 100644 index 0000000..f568693 --- /dev/null +++ b/plugins/quotes_set.lua @@ -0,0 +1,56 @@ +do + local quotes_file = './data/quotes.lua' + local quotes_table + + function read_quotes_file() + local f = io.open(quotes_file, "r+") + + if f == nil then + print ('Created a new quotes file on '..quotes_file) + serialize_to_file({}, quotes_file) + else + print ('Quotes loaded: '..quotes_file) + f:close() + end + return loadfile (quotes_file)() + end + + function save_quote(msg) + local to_id = tostring(msg.to.id) + local quotes + local phrase + + phrase = string.match(msg.text, "!addquote (.+)") + if (phrase == nil) then + return "Usage: !addquote quote" + end + + if quotes_table == nil then + quotes_table = {} + end + + if quotes_table[to_id] == nil then + print ('New quote key to_id: '..to_id) + quotes_table[to_id] = {} + end + + quotes = quotes_table[to_id] + quotes[#quotes+1] = phrase + + serialize_to_file(quotes_table, quotes_file) + + return "done!" + end + + function run(msg, matches) + quotes_table = read_quotes_file() + return save_quote(msg) + end + + return { + description = "Save quote", + usage = "!addquote (quote)", + patterns = {"^!addquote (.+)$"}, + run = run + } +end From bdb9639485e265e0391bdb629c5bf3d8edd57353 Mon Sep 17 00:00:00 2001 From: Claudio Filho Date: Fri, 13 Feb 2015 13:38:28 -0200 Subject: [PATCH 2/3] Merge quote files into one --- plugins/quotes.lua | 72 ++++++++++++++++++++++++++++++++++++++++++ plugins/quotes_get.lua | 37 ---------------------- plugins/quotes_set.lua | 56 -------------------------------- 3 files changed, 72 insertions(+), 93 deletions(-) create mode 100644 plugins/quotes.lua delete mode 100644 plugins/quotes_get.lua delete mode 100644 plugins/quotes_set.lua diff --git a/plugins/quotes.lua b/plugins/quotes.lua new file mode 100644 index 0000000..93ab554 --- /dev/null +++ b/plugins/quotes.lua @@ -0,0 +1,72 @@ +local quotes_file = './data/quotes.lua' +local quotes_table + +function read_quotes_file() + local f = io.open(quotes_file, "r+") + + if f == nil then + print ('Created a new quotes file on '..quotes_file) + serialize_to_file({}, quotes_file) + else + print ('Quotes loaded: '..quotes_file) + f:close() + end + return loadfile (quotes_file)() +end + +function save_quote(msg) + local to_id = tostring(msg.to.id) + + if msg.text:sub(11):isempty() then + return "Usage: !addquote quote" + end + + if quotes_table == nil then + quotes_table = {} + end + + if quotes_table[to_id] == nil then + print ('New quote key to_id: '..to_id) + quotes_table[to_id] = {} + end + + local quotes = quotes_table[to_id] + quotes[#quotes+1] = msg.text:sub(11) + + serialize_to_file(quotes_table, quotes_file) + + return "done!" +end + +function get_quote(msg) + local to_id = tostring(msg.to.id) + local quotes_phrases + + quotes_table = read_quotes_file() + quotes_phrases = quotes_table[to_id] + + return quotes_phrases[math.random(1,#quotes_phrases)] +end + +function run(msg, matches) + if string.match(msg.text, "!quote$") then + return get_quote(msg) + elseif string.match(msg.text, "!addquote (.+)$") then + quotes_table = read_quotes_file() + return save_quote(msg) + end +end + +return { + description = "Save quote", + description = "Quote plugin, you can create and retrieves random quotes", + usage = { + "!addquote [msg]", + "!quote", + }, + patterns = { + "^!addquote (.+)$", + "^!quote$", + }, + run = run +} diff --git a/plugins/quotes_get.lua b/plugins/quotes_get.lua deleted file mode 100644 index 9663ac3..0000000 --- a/plugins/quotes_get.lua +++ /dev/null @@ -1,37 +0,0 @@ -do - local quotes_file = './data/quotes.lua' - local quotes_table - - function read_quotes_file() - local f = io.open(quotes_file, "r+") - - if f == nil then - serialize_to_file({}, quotes_file) - else - f:close() - end - return loadfile (quotes_file)() - end - - function get_quote(msg) - local to_id = tostring(msg.to.id) - local quotes_phrases - - quotes_table = read_quotes_file() - quotes_phrases = quotes_table[to_id] - - return quotes_phrases[math.random(1,#quotes_phrases)] - end - - function run(msg, matches) - return get_quote(msg) - end - - return { - description = "retrieves random quotes", - usage = "!quote", - patterns = { - "^!quote$"}, - run = run - } -end diff --git a/plugins/quotes_set.lua b/plugins/quotes_set.lua deleted file mode 100644 index f568693..0000000 --- a/plugins/quotes_set.lua +++ /dev/null @@ -1,56 +0,0 @@ -do - local quotes_file = './data/quotes.lua' - local quotes_table - - function read_quotes_file() - local f = io.open(quotes_file, "r+") - - if f == nil then - print ('Created a new quotes file on '..quotes_file) - serialize_to_file({}, quotes_file) - else - print ('Quotes loaded: '..quotes_file) - f:close() - end - return loadfile (quotes_file)() - end - - function save_quote(msg) - local to_id = tostring(msg.to.id) - local quotes - local phrase - - phrase = string.match(msg.text, "!addquote (.+)") - if (phrase == nil) then - return "Usage: !addquote quote" - end - - if quotes_table == nil then - quotes_table = {} - end - - if quotes_table[to_id] == nil then - print ('New quote key to_id: '..to_id) - quotes_table[to_id] = {} - end - - quotes = quotes_table[to_id] - quotes[#quotes+1] = phrase - - serialize_to_file(quotes_table, quotes_file) - - return "done!" - end - - function run(msg, matches) - quotes_table = read_quotes_file() - return save_quote(msg) - end - - return { - description = "Save quote", - usage = "!addquote (quote)", - patterns = {"^!addquote (.+)$"}, - run = run - } -end From c2a5380507533f8eef419a2ebf5d710be76ef935 Mon Sep 17 00:00:00 2001 From: Claudio Filho Date: Fri, 13 Feb 2015 13:45:27 -0200 Subject: [PATCH 3/3] Add imdb plugin --- plugins/imdb.lua | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 plugins/imdb.lua diff --git a/plugins/imdb.lua b/plugins/imdb.lua new file mode 100644 index 0000000..5f6c92b --- /dev/null +++ b/plugins/imdb.lua @@ -0,0 +1,33 @@ + +function imdb(movie) + local http = require("socket.http") + http.TIMEOUT = 5 + + movie = movie:gsub(' ', '+') + url = "http://www.imdbapi.com/?t=" .. movie + response, code, headers = http.request(url) + + if code ~= 200 then + return "Error: " .. code + end + + if #response > 0 then + r = json:decode(response) + r['Url'] = "http://imdb.com/title/" .. r.imdbID + t = "" + for k, v in pairs(r) do t = t .. k .. ": " .. v .. ", " end + return t:sub(1, -3) + end + return nil +end + +function run(msg, matches) + return imdb(matches[1]) +end + +return { + description = "Imdb plugin for telegram", + usage = "!imdb [movie]", + patterns = {"^!imdb (.+)"}, + run = run +}