diff --git a/README.md b/README.md index 9e90998..289f957 100644 --- a/README.md +++ b/README.md @@ -12,21 +12,17 @@ Multimedia ![http://i.imgur.com/0FGUvU0.png](http://i.imgur.com/0FGUvU0.png) ![http://i.imgur.com/zW7WWWt.png](http://i.imgur.com/zW7WWWt.png) ![http://i.imgur.com/zW7WWWt.png](http://i.imgur.com/kPK7paz.png) -Default enabled commands +Bot Commands ------------ -``` -!9gag -> send random image from 9gag -!echo [whatever] -> echoes the msg -!get (value_name) -> retrieves variables saved with !set -!set [value_name] [data] -> Set value -!img [topic] -> search image with Google API and sends it -!loc (location) -> Gets information about a location, maplink and overview -!stats -> Numer of messages by user -!time [area] -> Displays the local time in an area -!version -> Shows bot version -!google terms -> Searches Google -!help -> Lists all available commands -``` + + + + + + + + +
NameDescriptionUsage
9gag.lua9GAG for telegram!9gag: Send random image from 9gag
btc.luaBitcoin global average market value (in EUR or USD)!btc [EUR|USD] [amount]
echo.luaechoes the msg!echo [whatever]
eur.luaEURUSD market value!eur [USD]
fortunes_uc3m.luaFortunes from Universidad Carlos III!uc3m
get.luaretrieves variables saved with !set!get (value_name)
giphy.luaGIFs from telegram with Giphy API!gif (term): Search and sends GIF from Giphy. If no param, sends a trending GIF.
!giphy (term): Search and sends GIF from Giphy. If no param, sends a trending GIF.
google.luaSearches Google and send results!google [terms]
gps.luagenerates a map showing the given GPS coordinates!gps latitude,longitude
hello.luaSays hello to someonesay hello to [name]
help.luaLists all available commands!help
!help md
images.luaWhen user sends image URL (ends with png, jpg, jpeg) download and send it to origin.
img_google.luasearch image with Google API and sends it!img [topic]
invite.luaInvite other user to the chat group!invite name [user_name]
!invite id [user_id]
location.luaGets information about a location, maplink and overview!loc (location)
media.luaWhen user sends media URL (ends with gif, mp4, pdf, etc.) download and send it to origin.
ping.luaIf domain is offline, send msg to peer
plugins.luaEnables, disables and reloads plugins. Privileged users only.!plugins: list all plugins
!plugins enable [plugin]
!plugins disable [plugin]
!plugins reload
rae.luaSpanish dictionary!rae [word]
set.luaSet value!set [value_name] [data]
stats.luaNumer of messages by user!stats
time.luaDisplays the local time in an area!time [area]
twitter.luaWhen user sends twitter URL, send text and images to origin. Requieres OAuth Key.
twitter_send.luaSends a tweet!tw [text]
version.luaShows bot version!version
weather.luaweather in that city (Madrid is default)!weather (city)
youtube.luasends YouTube image
Installation ------------ @@ -35,9 +31,9 @@ Installation $ sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev unzip git $ cd /tmp $ wget http://luarocks.org/releases/luarocks-2.2.0.tar.gz -$ tar -xzvf luarocks-2.2.0.tar.gz +$ tar -xzvf luarocks-2.2.0.tar.gz $ cd luarocks-2.2.0/ -$ ./configure +$ ./configure $ make && sudo make install $ sudo luarocks install oauth $ sudo luarocks install luasocket @@ -57,7 +53,7 @@ See the plugins list with `!plugins` command. Enable a disabled plugin by `!plugins enable [name]`. -Disable an eanbled plugin by `!plugins disable [name]`. +Disable an enabled plugin by `!plugins disable [name]`. Those commands require a privileged user, privileged users are defined inside `data/config.lua` (generated by the bot), stop de bot and edit if necessary. diff --git a/bot/bot.lua b/bot/bot.lua index a7d2f99..8dd97bd 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -5,7 +5,7 @@ json = (loadfile "./libs/JSON.lua")() serpent = (loadfile "./libs/serpent.lua")() require("./bot/utils") -VERSION = '0.8.3' +VERSION = '0.8.4' function on_msg_receive (msg) vardump(msg) @@ -209,4 +209,4 @@ end -- Start and load values our_id = 0 -now = os.time() \ No newline at end of file +now = os.time() diff --git a/bot/utils.lua b/bot/utils.lua index 506727b..49f5b02 100644 --- a/bot/utils.lua +++ b/bot/utils.lua @@ -31,14 +31,18 @@ function string:split(sep) return fields end +-- Removes spaces +function string.trim(s) + return s:gsub("^%s*(.-)%s*$", "%1") +end + function download_to_file( url , noremove ) print("url to download: "..url) local ltn12 = require "ltn12" local respbody = {} one, c, h = http.request{url=url, sink=ltn12.sink.table(respbody), redirect=true} htype = h["content-type"] - vardump(c) - print("content-type: "..htype) + if htype == "image/jpeg" then file_name = string.random(5)..".jpg" file_path = "/tmp/"..file_name @@ -185,4 +189,9 @@ end -- Retruns true if the string is empty function string:isempty() return self == nil or self == '' +end + + +function string.starts(String, Start) + return Start == string.sub(String,1,string.len(Start)) end \ No newline at end of file diff --git a/plugins/echo.lua b/plugins/echo.lua index 1e758f9..58f77b3 100644 --- a/plugins/echo.lua +++ b/plugins/echo.lua @@ -4,8 +4,8 @@ function run(msg, matches) end return { - description = "echoes the msg", - usage = "!echo [whatever]", + description = "Simplest plugin ever!", + usage = "!echo [whatever]: echoes the msg", patterns = {"^!echo (.*)$"}, run = run } diff --git a/plugins/get.lua b/plugins/get.lua index c254dc5..4dc953b 100644 --- a/plugins/get.lua +++ b/plugins/get.lua @@ -72,11 +72,11 @@ function lex(msg, text) end return { - description = "retrieves variables saved with !set", - usage = "!get (value_name)", + description = "Retrieves variables saved with !set", + usage = "!get (value_name): Returns the value_name value.", patterns = { "^!get (%a+)$", - "^!get$"}, + "^!get$"}, run = run, lex = lex } diff --git a/plugins/google.lua b/plugins/google.lua index 0481962..61f6b2e 100644 --- a/plugins/google.lua +++ b/plugins/google.lua @@ -29,7 +29,7 @@ end return { description = "Searches Google and send results", - usage = "!google [terms]", + usage = "!google [terms]: Searches Google and send results", patterns = { "^!google (.*)$", "^%.[g|G]oogle (.*)$" diff --git a/plugins/gps.lua b/plugins/gps.lua index f26a211..7393e86 100644 --- a/plugins/gps.lua +++ b/plugins/gps.lua @@ -25,7 +25,7 @@ end return { description = "generates a map showing the given GPS coordinates", - usage = "!gps latitude,longitude", + usage = "!gps latitude,longitude: generates a map showing the given GPS coordinates", patterns = {"^!gps ([^,]*)[,%s]([^,]*)$"}, run = run } diff --git a/plugins/help.lua b/plugins/help.lua index 31b29e8..c671ff1 100644 --- a/plugins/help.lua +++ b/plugins/help.lua @@ -30,18 +30,23 @@ function html_help() return text end +function has_usage_data(dict) + if (dict.usage == nil or dict.usage == '') then + return false + end + return true +end + function telegram_help( ) local ret = "" for k, dict in pairs(plugins) do - if dict.usage ~= "" then - if (type(dict.usage) == "table") then - for ku,vu in pairs(dict.usage) do - ret = ret..vu.." " - end - else - ret = ret..dict.usage + if (type(dict.usage) == "table") then + for ku,usage in pairs(dict.usage) do + ret = ret..usage..'\n' end - ret = ret .. " -> " .. dict.description .. "\n" + ret = ret..'\n' + elseif has_usage_data(dict) then -- Is not empty + ret = ret..dict.usage..'\n\n' end end return ret @@ -56,8 +61,11 @@ function run(msg, matches) end return { - description = "Lists all available commands", - usage = {"!help", "!help md"}, + description = "Help plugin. Get info from other plugins. ", + usage = { + "!help: Show all the help", + "!help md: Generate a GitHub Markdown table" + }, patterns = { "^!help$", "^!help md$" diff --git a/plugins/img_google.lua b/plugins/img_google.lua index 5624edf..456a247 100644 --- a/plugins/img_google.lua +++ b/plugins/img_google.lua @@ -25,8 +25,8 @@ function run(msg, matches) end return { - description = "search image with Google API and sends it", - usage = "!img [topic]", + description = "Search image with Google API and sends it.", + usage = "!img [term]: Random search an image with Google API.", patterns = {"^!img (.*)$"}, run = run } diff --git a/plugins/location.lua b/plugins/location.lua index f6d876b..3d06d92 100644 --- a/plugins/location.lua +++ b/plugins/location.lua @@ -64,7 +64,7 @@ end return { description = "Gets information about a location, maplink and overview", - usage = "!loc (location)", + usage = "!loc (location): Gets information about a location, maplink and overview", patterns = {"^!loc (.*)$"}, run = run } diff --git a/plugins/plugins.lua b/plugins/plugins.lua index 323b6c6..021c41b 100644 --- a/plugins/plugins.lua +++ b/plugins/plugins.lua @@ -100,12 +100,12 @@ function run(msg, matches) end return { - description = "Enables, disables and reloads plugins. Privileged users only.", + description = "Plugin to manage other plugins. Enable, disable or reload.", usage = { "!plugins: list all plugins", - "!plugins enable [plugin]", - "!plugins disable [plugin]", - "!plugins reload" }, + "!plugins enable [plugin]: enable plugin", + "!plugins disable [plugin]: disable plugin", + "!plugins reload: reloads all plugins" }, patterns = { "^!plugins$", "^!plugins? (enable) (.*)$", diff --git a/plugins/rae.lua b/plugins/rae.lua index 787fc18..3c8c538 100644 --- a/plugins/rae.lua +++ b/plugins/rae.lua @@ -41,7 +41,7 @@ end return { description = "Spanish dictionary", - usage = "!rae [word]", + usage = "!rae [word]: Search that word in Spanish dictionary. Powered by https://github.com/javierhonduco/dulcinea", patterns = {"^!rae (.*)$"}, run = run } diff --git a/plugins/set.lua b/plugins/set.lua index d971b9c..8f0b1a6 100644 --- a/plugins/set.lua +++ b/plugins/set.lua @@ -23,8 +23,8 @@ function run(msg, matches) end return { - description = "Set value", - usage = "!set [value_name] [data]", + description = "Plugin for saving values. get.lua plugin is necesary to retrieve them.", + usage = "!set [value_name] [data]: Saves the data with the value_name name.", patterns = {"^!set (%a+) (.+)$"}, run = run } diff --git a/plugins/stats.lua b/plugins/stats.lua index e9fcf7e..0b9e340 100644 --- a/plugins/stats.lua +++ b/plugins/stats.lua @@ -23,6 +23,7 @@ function update_user_stats(msg) if _stats[to_id][from_id] == nil then print ('New stats key from_id: '..to_id) _stats[to_id][from_id] = { + user_id = from_id, name = user_name, last_name = user_last_name, print_name = user_print_name, @@ -32,7 +33,7 @@ function update_user_stats(msg) print ('Updated '..to_id..' '..from_id) local actual_num = _stats[to_id][from_id].msg_num _stats[to_id][from_id].msg_num = actual_num + 1 - -- And update last_name + _stats[to_id][from_id].user_id = from_id _stats[to_id][from_id].last_name = user_last_name end end @@ -60,13 +61,28 @@ end local function get_stats_status( msg ) -- vardump(stats) local text = "" - local to_id = tostring(msg.to.id) + local to_id = tostring(msg.to.id) + local rank = {} for id, user in pairs(_stats[to_id]) do + table.insert(rank, user) + end + + table.sort(rank, function(a, b) + if a.msg_num and b.msg_num then + return a.msg_num > b.msg_num + end + end + ) + + for id, user in pairs(rank) do + -- Previous versions didn't save that + user_id = user.user_id or '' + print(">> ", id, user.name) if user.last_name == nil then - text = text..user.name.." ["..id.."]: "..user.msg_num.."\n" + text = text..user.name.." ["..user_id.."]: "..user.msg_num.."\n" else - text = text..user.name.." "..user.last_name.." ["..id.."]: "..user.msg_num.."\n" + text = text..user.name.." "..user.last_name.." ["..user_id.."]: "..user.msg_num.."\n" end end print("usuarios: "..text) @@ -75,7 +91,7 @@ end local function run(msg, matches) if matches[1] == "stats" then -- Hack - return get_stats_status(msg) + return get_stats_status(msg) else print ("update stats") update_user_stats(msg) @@ -86,8 +102,8 @@ end _stats = read_file_stats() return { - description = "Numer of messages by user", - usage = "!stats", + description = "Plugin to update user stats.", + usage = "!stats: Returns a list of Username [telegram_id]: msg_num", patterns = { ".*", "^!(stats)" @@ -95,4 +111,4 @@ return { run = run } -end \ No newline at end of file +end diff --git a/plugins/time.lua b/plugins/time.lua index 0bf0ed9..289691e 100644 --- a/plugins/time.lua +++ b/plugins/time.lua @@ -94,7 +94,7 @@ end return { description = "Displays the local time in an area", - usage = "!time [area]", + usage = "!time [area]: Displays the local time in that area", patterns = {"^!time (.*)$"}, - run = run + run = run } diff --git a/plugins/twitter_send.lua b/plugins/twitter_send.lua index cf243f7..ba9ae84 100644 --- a/plugins/twitter_send.lua +++ b/plugins/twitter_send.lua @@ -16,16 +16,16 @@ local client = OAuth.new(consumer_key, consumer_secret, { function run(msg, matches) if consumer_key:isempty() then - return "Twitter Consumer Key is empty, write it in plugins/twitter.lua" + 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.lua" + 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.lua" + 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.lua" + return "Twitter Access Token Secret is empty, write it in plugins/twitter_send.lua" end if not is_sudo(msg) then @@ -44,7 +44,7 @@ end return { description = "Sends a tweet", - usage = "!tw [text]", + usage = "!tw [text]: Sends the Tweet with the configured accout.", patterns = {"^!tw (.+)"}, run = run -} \ No newline at end of file +} diff --git a/plugins/version.lua b/plugins/version.lua index ec0bc02..9e42111 100644 --- a/plugins/version.lua +++ b/plugins/version.lua @@ -7,8 +7,10 @@ end return { description = "Shows bot version", - usage = "!version", - patterns = {"^!version$"}, + usage = "!version: Shows bot version", + patterns = { + "^!version$" + }, run = run } diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua new file mode 100644 index 0000000..6757c3b --- /dev/null +++ b/plugins/xkcd.lua @@ -0,0 +1,54 @@ +function get_last_id() + local res,code = https.request("http://xkcd.com/info.0.json") + if code ~= 200 then return "HTTP ERROR" end + local data = json:decode(res) + return data.num +end + +function get_xkcd(id) + local res,code = http.request("http://xkcd.com/"..id.."/info.0.json") + if code ~= 200 then return "HTTP ERROR" end + local data = json:decode(res) + local link_image = data.img + if link_image:sub(0,2) == '//' then + link_image = msg.text:sub(3,-1) + end + return link_image, data.title +end + + +function get_xkcd_random() + local last = get_last_id() + math.randomseed(os.time()) + i = math.random(1, data.num) + return get_xkcd(i) +end + +function send_title(cb_extra, success, result) + if success then + send_msg(cb_extra[1], cb_extra[2], ok_cb, false) + end +end + +function run(msg, matches) + local receiver = get_receiver(msg) + if matches[1] == "!xkcd" then + url, title = get_xkcd_random() + else + url, title = get_xkcd(matches[1]) + end + file_path = download_to_file(url) + send_photo(receiver, file_path, send_title, {receiver, title}) + return false +end + +return { + description = "Send comic images from xkcd", + usage = {"!xkcd (id): Send an xkcd image and tigle. If not id, send a random one"}, + patterns = { + "^!xkcd$", + "^!xkcd (%d+)", + "xkcd.com/(%d+)" + }, + run = run +} \ No newline at end of file diff --git a/tg b/tg index d74db18..01f5d9a 160000 --- a/tg +++ b/tg @@ -1 +1 @@ -Subproject commit d74db187ef3ed9a152979274ba2ea2ad8fe8c8d3 +Subproject commit 01f5d9a3b671edc15a5cff285601115845489985