diff --git a/plugins/9gag.lua b/plugins/9gag.lua index 8544806..f2a7d70 100644 --- a/plugins/9gag.lua +++ b/plugins/9gag.lua @@ -1,15 +1,15 @@ +do function get_9GAG() - b = http.request("http://api-9gag.herokuapp.com/") - local gag = json:decode(b) - math.randomseed(os.time()) - i = math.random(#gag) -- random max json table size (# is an operator o.O) - local link_image = gag[i].src - local title = gag[i].title - if link_image:sub(0,2) == '//' then - link_image = msg.text:sub(3,-1) - end - return link_image, title + local b = http.request("http://api-9gag.herokuapp.com/") + local gag = json:decode(b) + local i = math.random(#gag) -- random max json table size (# is an operator o.O) + local link_image = gag[i].src + local title = gag[i].title + if link_image:sub(0,2) == '//' then + link_image = msg.text:sub(3,-1) + end + return link_image, title end function send_title(cb_extra, success, result) @@ -27,9 +27,10 @@ function run(msg, matches) end return { - description = "9GAG for telegram", - usage = "!9gag: Send random image from 9gag", - patterns = {"^!9gag$"}, - run = run + description = "9GAG for telegram", + usage = "!9gag: Send random image from 9gag", + patterns = {"^!9gag$"}, + run = run } +end \ No newline at end of file diff --git a/plugins/bugzilla.lua b/plugins/bugzilla.lua index fe023b0..8ed22de 100644 --- a/plugins/bugzilla.lua +++ b/plugins/bugzilla.lua @@ -1,95 +1,82 @@ +do + +local BASE_URL = "https://bugzilla.mozilla.org/rest/" + function bugzilla_login() - url = "https://bugzilla.mozilla.org/rest/login?login=" .. _config.bugzilla.username .. "&password=" .. _config.bugzilla.password - - print("accessing " .. url) - - local res,code = https.request( url ) - - data = json:decode(res) - - return data + local url = BASE_URL.."login?login=" .. _config.bugzilla.username .. "&password=" .. _config.bugzilla.password + print("accessing " .. url) + local res,code = https.request( url ) + data = json:decode(res) + return data end + function bugzilla_check(id) - -- data = bugzilla_login() - - vardump(data) - url = "https://bugzilla.mozilla.org/rest/bug/" .. id .. "?api_key=" .. _config.bugzilla.apikey - -- print(url) - local res,code = https.request( url ) - - data = json:decode(res) - - return data - + -- data = bugzilla_login() + vardump(data) + local url = BASE_URL.."bug/" .. id .. "?api_key=" .. _config.bugzilla.apikey + -- print(url) + local res,code = https.request( url ) + data = json:decode(res) + return data end function bugzilla_listopened(email) - - url = "https://bugzilla.mozilla.org/rest/bug?include_fields=id,summary,status,whiteboard,resolution&email1=" .. email .. "&email2=" .. email .. "&emailassigned_to2=1&emailreporter1=1&emailtype1=substring&emailtype2=substring&f1=bug_status&f2=bug_status&n1=1&n2=1&o1=equals&o2=equals&resolution=---&v1=closed&v2=resolved&api_key=" .. _config.bugzilla.apikey - - local res,code = https.request( url ) - - print(res) - - data = json:decode(res) - - return data + local url = BASE_URL.."bug?include_fields=id,summary,status,whiteboard,resolution&email1=" .. email .. "&email2=" .. email .. "&emailassigned_to2=1&emailreporter1=1&emailtype1=substring&emailtype2=substring&f1=bug_status&f2=bug_status&n1=1&n2=1&o1=equals&o2=equals&resolution=---&v1=closed&v2=resolved&api_key=" .. _config.bugzilla.apikey + local res,code = https.request( url ) + print(res) + local data = json:decode(res) + return data end function run(msg, matches) - local response = "" + local response = "" - if matches[1] == "status" then - data = bugzilla_check(matches[2]) + if matches[1] == "status" then + data = bugzilla_check(matches[2]) + vardump(data) + if data.error == true then + return "Sorry, API failed with message: " .. data.message + else + response = "Bug #"..matches[1]..":\nReporter: "..data.bugs[1].creator + response = response .. "\n Last update: "..data.bugs[1].last_change_time + response = response .. "\n Status: "..data.bugs[1].status.." "..data.bugs[1].resolution + response = response .. "\n Whiteboard: "..data.bugs[1].whiteboard + response = response .. "\n Access: https://bugzilla.mozilla.org/show_bug.cgi?id=" .. matches[1] + print(response) + end + elseif matches[1] == "list" then + data = bugzilla_listopened(matches[2]) - vardump(data) + vardump(data) + if data.error == true then + return "Sorry, API failed with message: " .. data.message + else - if data.error == true then - return "Sorry, API failed with message: " .. data.message - else - response = "Bug #"..matches[1]..":\nReporter: "..data.bugs[1].creator - response = response .. "\n Last update: "..data.bugs[1].last_change_time - response = response .. "\n Status: "..data.bugs[1].status.." "..data.bugs[1].resolution - response = response .. "\n Whiteboard: "..data.bugs[1].whiteboard - response = response .. "\n Access: https://bugzilla.mozilla.org/show_bug.cgi?id=" .. matches[1] - print(response) - end - elseif matches[1] == "list" then - data = bugzilla_listopened(matches[2]) + -- response = "Bug #"..matches[1]..":\nReporter: "..data.bugs[1].creator + -- response = response .. "\n Last update: "..data.bugs[1].last_change_time + -- response = response .. "\n Status: "..data.bugs[1].status.." "..data.bugs[1].resolution + -- response = response .. "\n Whiteboard: "..data.bugs[1].whiteboard + -- response = response .. "\n Access: https://bugzilla.mozilla.org/show_bug.cgi?id=" .. matches[1] + local total = table.map_length(data.bugs) + + print("total bugs: " .. total) + response = "There are " .. total .. " number of bug(s) assigned/reported by " .. matches[2] - vardump(data) + if total > 0 then + response = response .. ": " - if data.error == true then - return "Sorry, API failed with message: " .. data.message - else + for tableKey, bug in pairs(data.bugs) do + response = response .. "\n #" .. bug.id + response = response .. "\n Status: " .. bug.status .. " " .. bug.resolution + response = response .. "\n Whiteboard: " .. bug.whiteboard + response = response .. "\n Summary: " .. bug.summary + end + end + end - -- response = "Bug #"..matches[1]..":\nReporter: "..data.bugs[1].creator - -- response = response .. "\n Last update: "..data.bugs[1].last_change_time - -- response = response .. "\n Status: "..data.bugs[1].status.." "..data.bugs[1].resolution - -- response = response .. "\n Whiteboard: "..data.bugs[1].whiteboard - -- response = response .. "\n Access: https://bugzilla.mozilla.org/show_bug.cgi?id=" .. matches[1] - local total = table.map_length(data.bugs) - - print("total bugs: " .. total) - - response = "There are " .. total .. " number of bug(s) assigned/reported by " .. matches[2] - - if total > 0 then - response = response .. ": " - - for tableKey, bug in pairs(data.bugs) do - response = response .. "\n #" .. bug.id - response = response .. "\n Status: " .. bug.status .. " " .. bug.resolution - response = response .. "\n Whiteboard: " .. bug.whiteboard - response = response .. "\n Summary: " .. bug.summary - end - end - end - - end - - return response + end + return response end -- (table) @@ -107,11 +94,13 @@ end -- total bugs: 2 return { - description = "Lookup bugzilla status update", - usage = "/bot bugzilla [bug number]", - patterns = { - "^/bugzilla (status) (.*)$", - "^/bugzilla (list) (.*)$" - }, - run = run -} \ No newline at end of file + description = "Lookup bugzilla status update", + usage = "/bot bugzilla [bug number]", + patterns = { + "^/bugzilla (status) (.*)$", + "^/bugzilla (list) (.*)$" + }, + run = run +} + +end \ No newline at end of file diff --git a/plugins/echo.lua b/plugins/echo.lua index 58f77b3..eba3aff 100644 --- a/plugins/echo.lua +++ b/plugins/echo.lua @@ -4,9 +4,10 @@ function run(msg, matches) end return { - description = "Simplest plugin ever!", - usage = "!echo [whatever]: echoes the msg", - patterns = {"^!echo (.*)$"}, - run = run + description = "Simplest plugin ever!", + usage = "!echo [whatever]: echoes the msg", + patterns = { + "^!echo (.*)$" + }, + run = run } - diff --git a/plugins/eur.lua b/plugins/eur.lua index 39ded90..fe8250b 100644 --- a/plugins/eur.lua +++ b/plugins/eur.lua @@ -1,6 +1,7 @@ +do function getEURUSD(usd) - b = http.request("http://webrates.truefx.com/rates/connect.html?c=EUR/USD&f=csv&s=n") + local b = http.request("http://webrates.truefx.com/rates/connect.html?c=EUR/USD&f=csv&s=n") local rates = b:split(", ") local symbol = rates[1] local timestamp = rates[2] @@ -31,3 +32,4 @@ return { run = run } +end \ No newline at end of file diff --git a/plugins/fortunes_uc3m.lua b/plugins/fortunes_uc3m.lua index c178dc7..55e7a1b 100644 --- a/plugins/fortunes_uc3m.lua +++ b/plugins/fortunes_uc3m.lua @@ -1,10 +1,10 @@ +do function get_fortunes_uc3m() - math.randomseed(os.time()) - local i = math.random(0,178) -- max 178 - local web = "http://www.gul.es/fortunes/f"..i - b, c, h = http.request(web) - return b + local i = math.random(0,178) -- max 178 + local web = "http://www.gul.es/fortunes/f"..i + local b, c, h = http.request(web) + return b end @@ -13,9 +13,12 @@ function run(msg, matches) end return { - description = "Fortunes from Universidad Carlos III", - usage = "!uc3m", - patterns = {"^!uc3m$"}, - run = run + description = "Fortunes from Universidad Carlos III", + usage = "!uc3m", + patterns = { + "^!uc3m$" + }, + run = run } +end \ No newline at end of file diff --git a/plugins/gps.lua b/plugins/gps.lua index 2f37d78..1cc4347 100644 --- a/plugins/gps.lua +++ b/plugins/gps.lua @@ -1,3 +1,5 @@ +do + function run(msg, matches) local lat = matches[1] local lon = matches[2] @@ -17,8 +19,10 @@ function run(msg, matches) end return { - description = "generates a map showing the given GPS coordinates", - usage = "!gps latitude,longitude: generates a map showing the given GPS coordinates", - patterns = {"^!gps ([^,]*)[,%s]([^,]*)$"}, - run = run -} \ No newline at end of file + description = "generates a map showing the given GPS coordinates", + usage = "!gps latitude,longitude: generates a map showing the given GPS coordinates", + patterns = {"^!gps ([^,]*)[,%s]([^,]*)$"}, + run = run +} + +end \ No newline at end of file diff --git a/plugins/imdb.lua b/plugins/imdb.lua index 5f6c92b..8de189e 100644 --- a/plugins/imdb.lua +++ b/plugins/imdb.lua @@ -1,33 +1,36 @@ +do function imdb(movie) - local http = require("socket.http") - http.TIMEOUT = 5 + local http = require("socket.http") + http.TIMEOUT = 5 - movie = movie:gsub(' ', '+') - url = "http://www.imdbapi.com/?t=" .. movie - response, code, headers = http.request(url) + local movie = movie:gsub(' ', '+') + local url = "http://www.imdbapi.com/?t=" .. movie + local response, code, headers = http.request(url) - if code ~= 200 then - return "Error: " .. code - end + 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 + if #response > 0 then + local r = json:decode(response) + r['Url'] = "http://imdb.com/title/" .. r.imdbID + local 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]) + return imdb(matches[1]) end return { - description = "Imdb plugin for telegram", - usage = "!imdb [movie]", - patterns = {"^!imdb (.+)"}, - run = run + description = "Imdb plugin for telegram", + usage = "!imdb [movie]", + patterns = {"^!imdb (.+)"}, + run = run } + +end \ No newline at end of file diff --git a/plugins/invite.lua b/plugins/invite.lua index cd5f452..e4a22cb 100644 --- a/plugins/invite.lua +++ b/plugins/invite.lua @@ -5,40 +5,40 @@ do local function run(msg, matches) - -- User submitted a user name - if matches[1] == "name" then - user = matches[2] - user = string.gsub(user," ","_") - end - -- User submitted an id - if matches[1] == "id" then - user = matches[2] - user = 'user#id'..user - end - -- The message must come from a chat group - if msg.to.type == 'chat' then - chat = 'chat#id'..msg.to.id - else - return 'This isnt a chat group!' - end - print ("Trying to add: "..user.." to "..chat) - status = chat_add_user (chat, user, ok_cb, false) - if not status then - return "An error happened" - end - return "Added user: "..user.." to "..chat + -- User submitted a user name + if matches[1] == "name" then + user = matches[2] + user = string.gsub(user," ","_") + end + -- User submitted an id + if matches[1] == "id" then + user = matches[2] + user = 'user#id'..user + end + -- The message must come from a chat group + if msg.to.type == 'chat' then + chat = 'chat#id'..msg.to.id + else + return 'This isnt a chat group!' + end + print ("Trying to add: "..user.." to "..chat) + status = chat_add_user (chat, user, ok_cb, false) + if not status then + return "An error happened" + end + return "Added user: "..user.." to "..chat end return { - description = "Invite other user to the chat group", - usage = { - "!invite name [user_name]", - "!invite id [user_id]" }, - patterns = { - "^!invite (name) (.*)$", - "^!invite (id) (%d+)$" - }, - run = run + description = "Invite other user to the chat group", + usage = { + "!invite name [user_name]", + "!invite id [user_id]" }, + patterns = { + "^!invite (name) (.*)$", + "^!invite (id) (%d+)$" + }, + run = run } end \ No newline at end of file diff --git a/plugins/location.lua b/plugins/location.lua index b68b8f1..23663db 100644 --- a/plugins/location.lua +++ b/plugins/location.lua @@ -7,53 +7,55 @@ -- Globals -- If you have a google api key for the geocoding/timezone api +do -api_key = nil +local api_key = nil -base_api = "https://maps.googleapis.com/maps/api" +local base_api = "https://maps.googleapis.com/maps/api" function get_staticmap(area) - local api = base_api .. "/staticmap?" + local api = base_api .. "/staticmap?" - -- Get a sense of scale - lat,lng,acc,types = get_latlong(area) - - local scale=types[1] - if scale=="locality" then zoom=8 - elseif scale=="country" then zoom=4 - else zoom=13 end - - local parameters = - "size=600x300" .. - "&zoom=" .. zoom .. - "¢er=" .. URL.escape(area) .. - "&markers=color:red"..URL.escape("|"..area) - - if api_key ~=nil and api_key ~= "" then - parameters = parameters .. "&key="..api_key - end - return lat, lng, api..parameters + -- Get a sense of scale + local lat,lng,acc,types = get_latlong(area) + + local scale = types[1] + if scale=="locality" then zoom=8 + elseif scale=="country" then zoom=4 + else zoom = 13 end + + local parameters = + "size=600x300" .. + "&zoom=" .. zoom .. + "¢er=" .. URL.escape(area) .. + "&markers=color:red"..URL.escape("|"..area) + + if api_key ~=nil and api_key ~= "" then + parameters = parameters .. "&key="..api_key + end + return lat, lng, api..parameters end function run(msg, matches) - local receiver = get_receiver(msg) - local lat,lng,url = get_staticmap(matches[1]) + local receiver = get_receiver(msg) + local lat,lng,url = get_staticmap(matches[1]) - -- Send the actual location, is a google maps link - send_location(receiver, lat, lng, ok_cb, false) - - -- Send a picture of the map, which takes scale into account - send_photo_from_url(receiver, url) + -- Send the actual location, is a google maps link + send_location(receiver, lat, lng, ok_cb, false) - -- Return a link to the google maps stuff is now not needed anymore - return nil + -- Send a picture of the map, which takes scale into account + send_photo_from_url(receiver, url) + + -- Return a link to the google maps stuff is now not needed anymore + return nil end return { - description = "Gets information about a location, maplink and overview", - usage = "!loc (location): Gets information about a location, maplink and overview", - patterns = {"^!loc (.*)$"}, - run = run + description = "Gets information about a location, maplink and overview", + usage = "!loc (location): Gets information about a location, maplink and overview", + patterns = {"^!loc (.*)$"}, + run = run } +end \ No newline at end of file diff --git a/plugins/media.lua b/plugins/media.lua index ce2c3fb..3e9dbc7 100644 --- a/plugins/media.lua +++ b/plugins/media.lua @@ -1,8 +1,8 @@ do function run(msg, matches) - file = download_to_file(matches[1]) - send_document(get_receiver(msg), file, ok_cb, false) + local file = download_to_file(matches[1]) + local send_document(get_receiver(msg), file, ok_cb, false) end return { diff --git a/plugins/ping.lua b/plugins/ping.lua index 938abbf..55b7b27 100644 --- a/plugins/ping.lua +++ b/plugins/ping.lua @@ -1,24 +1,28 @@ -socket = require("socket") +do + +local socket = require("socket") function cron() - -- Use yours desired web and id - local addr = "www.google.com" - local dest = "user#id"..our_id - -- Checks a TCP connexion - local connexion = socket.connect(addr, 80) - if not connexion then - local text = "ALERT: "..addr.." is offline" - print (text) - send_msg(dest, text, ok_cb, false) - else - connexion:close() - end + -- Use yours desired web and id + local addr = "www.google.com" + local dest = "user#id"..our_id + -- Checks a TCP connexion + local connexion = socket.connect(addr, 80) + if not connexion then + local text = "ALERT: "..addr.." is offline" + print (text) + send_msg(dest, text, ok_cb, false) + else + connexion:close() + end end return { - description = "If domain is offline, send msg to peer", - usage = "", - patterns = {}, - run = nil, - cron = cron + description = "If domain is offline, send msg to peer", + usage = "", + patterns = {}, + run = nil, + cron = cron } + +end \ No newline at end of file diff --git a/plugins/rae.lua b/plugins/rae.lua index 3c8c538..3c2c7f5 100644 --- a/plugins/rae.lua +++ b/plugins/rae.lua @@ -1,25 +1,41 @@ +do + function getDulcinea( text ) -- Powered by https://github.com/javierhonduco/dulcinea + local api = "http://dulcinea.herokuapp.com/api/?query=" local query_url = api..text - b = http.request(query_url) + + local b, code = http.request(query_url) + + if code ~= 200 then + return "Error: HTTP Connexion" + end + dulcinea = json:decode(b) + if dulcinea.status == "error" then return "Error: " .. dulcinea.message end + while dulcinea.type == "multiple" do text = dulcinea.response[1].id b = http.request(api..text) dulcinea = json:decode(b) end + local text = "" + local responses = #dulcinea.response + if responses == 0 then return "Error: 404 word not found" end + if (responses > 5) then responses = 5 end + for i = 1, responses, 1 do text = text .. dulcinea.response[i].word .. "\n" local meanings = #dulcinea.response[i].meanings @@ -31,7 +47,7 @@ function getDulcinea( text ) text = text .. meaning .. "\n\n" end end - print (text) + return text end @@ -41,8 +57,9 @@ end return { description = "Spanish dictionary", - usage = "!rae [word]: Search that word in Spanish dictionary. Powered by https://github.com/javierhonduco/dulcinea", + usage = "!rae [word]: Search that word in Spanish dictionary.", patterns = {"^!rae (.*)$"}, run = run } +end \ No newline at end of file diff --git a/plugins/time.lua b/plugins/time.lua index 289691e..3ff6db6 100644 --- a/plugins/time.lua +++ b/plugins/time.lua @@ -1,7 +1,7 @@ -- Implement a command !time [area] which uses -- 2 Google APIs to get the desired result: --- 1. Geocoding to get from area to a lat/long pair --- 2. Timezone to get the local time in that lat/long location +-- 1. Geocoding to get from area to a lat/long pair +-- 2. Timezone to get the local time in that lat/long location -- Globals -- If you have a google api key for the geocoding/timezone api @@ -12,89 +12,89 @@ dateFormat = "%A %d %B - %H:%M:%S" -- Need the utc time for the google api function utctime() - return os.time(os.date("!*t")) + return os.time(os.date("!*t")) end -- Use the geocoding api to get the lattitude and longitude with accuracy specifier -- CHECKME: this seems to work without a key?? function get_latlong(area) - local api = base_api .. "/geocode/json?" - local parameters = "address=".. (URL.escape(area) or "") - if api_key ~= nil then - parameters = parameters .. "&key="..api_key - end + local api = base_api .. "/geocode/json?" + local parameters = "address=".. (URL.escape(area) or "") + if api_key ~= nil then + parameters = parameters .. "&key="..api_key + end - -- Do the request - local res, code = https.request(api..parameters) - if code ~=200 then return nil end - local data = json:decode(res) + -- Do the request + local res, code = https.request(api..parameters) + if code ~=200 then return nil end + local data = json:decode(res) - if (data.status == "ZERO_RESULTS") then - return nil - end - if (data.status == "OK") then - -- Get the data - lat = data.results[1].geometry.location.lat - lng = data.results[1].geometry.location.lng - acc = data.results[1].geometry.location_type - types= data.results[1].types - return lat,lng,acc,types - end + if (data.status == "ZERO_RESULTS") then + return nil + end + if (data.status == "OK") then + -- Get the data + lat = data.results[1].geometry.location.lat + lng = data.results[1].geometry.location.lng + acc = data.results[1].geometry.location_type + types= data.results[1].types + return lat,lng,acc,types + end end -- Use timezone api to get the time in the lat, -- Note: this needs an API key function get_time(lat,lng) - local api = base_api .. "/timezone/json?" + local api = base_api .. "/timezone/json?" - -- Get a timestamp (server time is relevant here) - local timestamp = utctime() - local parameters = "location=" .. - URL.escape(lat) .. "," .. - URL.escape(lng) .. - "×tamp="..URL.escape(timestamp) - if api_key ~=nil then - parameters = parameters .. "&key="..api_key - end + -- Get a timestamp (server time is relevant here) + local timestamp = utctime() + local parameters = "location=" .. + URL.escape(lat) .. "," .. + URL.escape(lng) .. + "×tamp="..URL.escape(timestamp) + if api_key ~=nil then + parameters = parameters .. "&key="..api_key + end - local res,code = https.request(api..parameters) - if code ~= 200 then return nil end - local data = json:decode(res) - - if (data.status == "ZERO_RESULTS") then - return nil - end - if (data.status == "OK") then - -- Construct what we want - -- The local time in the location is: - -- timestamp + rawOffset + dstOffset - local localTime = timestamp + data.rawOffset + data.dstOffset - return localTime, data.timeZoneId - end - return localTime + local res,code = https.request(api..parameters) + if code ~= 200 then return nil end + local data = json:decode(res) + + if (data.status == "ZERO_RESULTS") then + return nil + end + if (data.status == "OK") then + -- Construct what we want + -- The local time in the location is: + -- timestamp + rawOffset + dstOffset + local localTime = timestamp + data.rawOffset + data.dstOffset + return localTime, data.timeZoneId + end + return localTime end function getformattedLocalTime(area) - if area == nil then - return "The time in nowhere is never" - end + if area == nil then + return "The time in nowhere is never" + end - lat,lng,acc = get_latlong(area) - if lat == nil and lng == nil then - return 'It seems that in "'..area..'" they do not have a concept of time.' - end - local localTime, timeZoneId = get_time(lat,lng) + lat,lng,acc = get_latlong(area) + if lat == nil and lng == nil then + return 'It seems that in "'..area..'" they do not have a concept of time.' + end + local localTime, timeZoneId = get_time(lat,lng) - return "The local time in "..timeZoneId.." is: ".. os.date(dateFormat,localTime) + return "The local time in "..timeZoneId.." is: ".. os.date(dateFormat,localTime) end function run(msg, matches) - return getformattedLocalTime(matches[1]) + return getformattedLocalTime(matches[1]) end return { - description = "Displays the local time in an area", - usage = "!time [area]: Displays the local time in that area", - patterns = {"^!time (.*)$"}, - run = run + description = "Displays the local time in an area", + usage = "!time [area]: Displays the local time in that area", + patterns = {"^!time (.*)$"}, + run = run } diff --git a/plugins/twitter.lua b/plugins/twitter.lua index 822b843..36cebf1 100644 --- a/plugins/twitter.lua +++ b/plugins/twitter.lua @@ -39,9 +39,9 @@ function run(msg, matches) -- 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) + local short = v.url + local long = v.expanded_url + text = text:gsub(short, long) end end @@ -49,10 +49,10 @@ function run(msg, matches) local images = {} if response.extended_entities and response.extended_entities.media then for k, v in pairs(response.extended_entities.media) do - local url = v.url - local pic = v.media_url - text = text:gsub(url, "") - table.insert(images, pic) + local url = v.url + local pic = v.media_url + text = text:gsub(url, "") + table.insert(images, pic) end end @@ -65,8 +65,10 @@ end return { - description = "When user sends twitter URL, send text and images to origin. Requieres OAuth Key.", - usage = "", - patterns = {"https://twitter.com/[^/]+/status/([0-9]+)"}, - run = run + description = "When user sends twitter URL, send text and images to origin. Requieres OAuth Key.", + usage = "", + patterns = { + "https://twitter.com/[^/]+/status/([0-9]+)" + }, + run = run } diff --git a/plugins/twitter_send.lua b/plugins/twitter_send.lua index ba9ae84..5ae4f7c 100644 --- a/plugins/twitter_send.lua +++ b/plugins/twitter_send.lua @@ -1,3 +1,5 @@ +do + local OAuth = require "OAuth" local consumer_key = "" @@ -6,45 +8,47 @@ local access_token = "" local 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 + 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) - if consumer_key:isempty() then - return "Twitter Consumer Key is empty, write it in plugins/twitter_send.lua" - end - if consumer_secret:isempty() then - return "Twitter Consumer Secret is empty, write it in plugins/twitter_send.lua" - end - if access_token:isempty() then - return "Twitter Access Token is empty, write it in plugins/twitter_send.lua" - end - if access_token_secret:isempty() then - return "Twitter Access Token Secret is empty, write it in plugins/twitter_send.lua" - end + if consumer_key:isempty() then + return "Twitter Consumer Key is empty, write it in plugins/twitter_send.lua" + end + if consumer_secret:isempty() then + return "Twitter Consumer Secret is empty, write it in plugins/twitter_send.lua" + end + if access_token:isempty() then + return "Twitter Access Token is empty, write it in plugins/twitter_send.lua" + end + if access_token_secret:isempty() then + return "Twitter Access Token Secret is empty, write it in plugins/twitter_send.lua" + end - if not is_sudo(msg) then - return "You aren't allowed to send tweets" - end - - local response_code, response_headers, response_status_line, response_body = - client:PerformRequest("POST", "https://api.twitter.com/1.1/statuses/update.json", { - status = matches[1] - }) - if response_code ~= 200 then - return "Error: "..response_code - end - return "Tweet sended" + if not is_sudo(msg) then + return "You aren't allowed to send tweets" + end + + local response_code, response_headers, response_status_line, response_body = + client:PerformRequest("POST", "https://api.twitter.com/1.1/statuses/update.json", { + status = matches[1] + }) + if response_code ~= 200 then + return "Error: "..response_code + end + return "Tweet sended" end return { - description = "Sends a tweet", - usage = "!tw [text]: Sends the Tweet with the configured accout.", - patterns = {"^!tw (.+)"}, - run = run + description = "Sends a tweet", + usage = "!tw [text]: Sends the Tweet with the configured accout.", + patterns = {"^!tw (.+)"}, + run = run } + +end \ No newline at end of file diff --git a/plugins/version.lua b/plugins/version.lua index 9e42111..b283074 100644 --- a/plugins/version.lua +++ b/plugins/version.lua @@ -1,3 +1,4 @@ +do function run(msg, matches) return 'Telegram Bot '.. VERSION .. [[ @@ -6,11 +7,12 @@ function run(msg, matches) end return { - description = "Shows bot version", - usage = "!version: Shows bot version", - patterns = { - "^!version$" - }, - run = run + description = "Shows bot version", + usage = "!version: Shows bot version", + patterns = { + "^!version$" + }, + run = run } +end \ No newline at end of file diff --git a/plugins/vote.lua b/plugins/vote.lua index 6ec5946..bd5ffe3 100644 --- a/plugins/vote.lua +++ b/plugins/vote.lua @@ -1,85 +1,88 @@ +do + local _file_votes = './data/votes.lua' function read_file_votes () - local f = io.open(_file_votes, "r+") - if f == nil then - print ('Created voting file '.._file_votes) - serialize_to_file({}, _file_votes) - else - print ('Values loaded: '.._file_votes) - f:close() - end - return loadfile (_file_votes)() + local f = io.open(_file_votes, "r+") + if f == nil then + print ('Created voting file '.._file_votes) + serialize_to_file({}, _file_votes) + else + print ('Values loaded: '.._file_votes) + f:close() + end + return loadfile (_file_votes)() end function clear_votes (chat) - local _votes = read_file_votes () - _votes [chat] = {} - serialize_to_file(_votes, _file_votes) + local _votes = read_file_votes () + _votes [chat] = {} + serialize_to_file(_votes, _file_votes) end function votes_result (chat) - local _votes = read_file_votes () - local results = {} - local result_string = "" - if _votes [chat] == nil then - _votes[chat] = {} - end - for user,vote in pairs (_votes[chat]) do - if (results [vote] == nil) then - results [vote] = user - else - results [vote] = results [vote] .. ", " .. user - end - end - for vote,users in pairs (results) do - result_string = result_string .. vote .. " : " .. users .. "\n" - end - return result_string + local _votes = read_file_votes () + local results = {} + local result_string = "" + if _votes [chat] == nil then + _votes[chat] = {} + end + for user,vote in pairs (_votes[chat]) do + if (results [vote] == nil) then + results [vote] = user + else + results [vote] = results [vote] .. ", " .. user + end + end + for vote,users in pairs (results) do + result_string = result_string .. vote .. " : " .. users .. "\n" + end + return result_string end function save_vote(chat, user, vote) - local _votes = read_file_votes () - if _votes[chat] == nil then - _votes[chat] = {} - end - _votes[chat][user] = vote + local _votes = read_file_votes () + if _votes[chat] == nil then + _votes[chat] = {} + end + _votes[chat][user] = vote - serialize_to_file(_votes, _file_votes) - + serialize_to_file(_votes, _file_votes) + end function run(msg, matches) - if (matches[1] == "ing") then - if (matches [2] == "reset") then - clear_votes (tostring(msg.to.id)) - return "Voting statistics reset.." - elseif (matches [2] == "stats") then - local votes_result = votes_result (tostring(msg.to.id)) - if (votes_result == "") then - votes_result = "[No votes registered]\n" - end - return "Voting statistics :\n" .. votes_result - end - else - save_vote(tostring(msg.to.id), msg.from.print_name, tostring(tonumber(matches[2]))) - return "Vote registered : " .. msg.from.print_name .. " " .. tostring(tonumber(matches [2])) - end + if (matches[1] == "ing") then + if (matches [2] == "reset") then + clear_votes (tostring(msg.to.id)) + return "Voting statistics reset.." + elseif (matches [2] == "stats") then + local votes_result = votes_result (tostring(msg.to.id)) + if (votes_result == "") then + votes_result = "[No votes registered]\n" + end + return "Voting statistics :\n" .. votes_result + end + else + save_vote(tostring(msg.to.id), msg.from.print_name, tostring(tonumber(matches[2]))) + return "Vote registered : " .. msg.from.print_name .. " " .. tostring(tonumber(matches [2])) + end end return { - description = "Plugin for voting in groups.", - usage = { - "!voting reset: Reset all the votes.", - "!vote [number]: Cast the vote.", - "!voting stats: Shows the statistics of voting." - }, - patterns = { - "^!vot(ing) (reset)", - "^!vot(ing) (stats)", - "^!vot(e) ([0-9]+)$" - }, - run = run + description = "Plugin for voting in groups.", + usage = { + "!voting reset: Reset all the votes.", + "!vote [number]: Cast the vote.", + "!voting stats: Shows the statistics of voting." + }, + patterns = { + "^!vot(ing) (reset)", + "^!vot(ing) (stats)", + "^!vot(e) ([0-9]+)$" + }, + run = run } +end \ No newline at end of file diff --git a/plugins/weather.lua b/plugins/weather.lua index 0d07f8a..50b526b 100644 --- a/plugins/weather.lua +++ b/plugins/weather.lua @@ -1,39 +1,45 @@ +do + +local BASE_URL = "http://api.openweathermap.org/data/2.5" function get_weather(location) - print("Finding weather in ", location) - b, c, h = http.request("http://api.openweathermap.org/data/2.5/weather?q=" .. location .. "&units=metric") - weather = json:decode(b) - print("Weather returns", weather) - local city = weather.name - local country = weather.sys.country - temp = 'The temperature in ' .. city .. ' (' .. country .. ')' - temp = temp .. ' is ' .. weather.main.temp .. '°C' - conditions = 'Current conditions are: ' .. weather.weather[1].description - if weather.weather[1].main == 'Clear' then - conditions = conditions .. ' ☀' - elseif weather.weather[1].main == 'Clouds' then - conditions = conditions .. ' ☁☁' - elseif weather.weather[1].main == 'Rain' then - conditions = conditions .. ' ☔' - elseif weather.weather[1].main == 'Thunderstorm' then - conditions = conditions .. ' ☔☔☔☔' - end - return temp .. '\n' .. conditions + print("Finding weather in ", location) + local b, c, h = http.request(BASE_URL.."/weather?q=" .. location .. "&units=metric") + local weather = json:decode(b) + print("Weather returns", weather) + local city = weather.name + local country = weather.sys.country + local temp = 'The temperature in ' .. city .. ' (' .. country .. ')' + temp = temp .. ' is ' .. weather.main.temp .. '°C' + conditions = 'Current conditions are: ' .. weather.weather[1].description + + if weather.weather[1].main == 'Clear' then + conditions = conditions .. ' ☀' + elseif weather.weather[1].main == 'Clouds' then + conditions = conditions .. ' ☁☁' + elseif weather.weather[1].main == 'Rain' then + conditions = conditions .. ' ☔' + elseif weather.weather[1].main == 'Thunderstorm' then + conditions = conditions .. ' ☔☔☔☔' + end + + return temp .. '\n' .. conditions end function run(msg, matches) - if string.len(matches[1]) > 2 then - city = matches[1] - else - city = "Madrid,ES" - end - return get_weather(city) + if string.len(matches[1]) > 2 then + city = matches[1] + else + city = "Madrid,ES" + end + return get_weather(city) end return { - description = "weather in that city (Madrid is default)", - usage = "!weather (city)", - patterns = {"^!weather(.*)$"}, - run = run + description = "weather in that city (Madrid is default)", + usage = "!weather (city)", + patterns = {"^!weather(.*)$"}, + run = run } +end \ No newline at end of file diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua index 0354b62..4c8b6f7 100644 --- a/plugins/xkcd.lua +++ b/plugins/xkcd.lua @@ -1,3 +1,5 @@ +do + function get_last_id() local res,code = https.request("http://xkcd.com/info.0.json") if code ~= 200 then return "HTTP ERROR" end @@ -19,8 +21,7 @@ end function get_xkcd_random() local last = get_last_id() - math.randomseed(os.time()) - i = math.random(1, last) + local i = math.random(1, last) return get_xkcd(i) end @@ -43,12 +44,14 @@ function run(msg, matches) end return { - description = "Send comic images from xkcd", - usage = {"!xkcd (id): Send an xkcd image and title. If not id, send a random one"}, - patterns = { - "^!xkcd$", - "^!xkcd (%d+)", - "xkcd.com/(%d+)" - }, - run = run + description = "Send comic images from xkcd", + usage = {"!xkcd (id): Send an xkcd image and title. If not id, send a random one"}, + patterns = { + "^!xkcd$", + "^!xkcd (%d+)", + "xkcd.com/(%d+)" + }, + run = run } + +end \ No newline at end of file diff --git a/plugins/youtube.lua b/plugins/youtube.lua index 3c56513..47f7a9d 100644 --- a/plugins/youtube.lua +++ b/plugins/youtube.lua @@ -16,7 +16,10 @@ function send_youtube_data(data, receiver) local uploader = data.uploader local text = title..' ('..uploader..')\n'..description local image_url = data.thumbnail.hqDefault - local cb_extra = {receiver=receiver, url=image_url} + local cb_extra = { + receiver = receiver, + url = image_url + } send_msg(receiver, text, send_photo_from_url_callback, cb_extra) end