From fb38228a97209f8bf132693feed33e247684e840 Mon Sep 17 00:00:00 2001 From: yago Date: Mon, 12 Jan 2015 22:29:59 +0100 Subject: [PATCH 01/17] GiphyAPI --- plugins/giphy.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 plugins/giphy.lua diff --git a/plugins/giphy.lua b/plugins/giphy.lua new file mode 100644 index 0000000..ccf8697 --- /dev/null +++ b/plugins/giphy.lua @@ -0,0 +1,46 @@ +-- Idea by https://github.com/asdofindia/telegram-bot/ +-- See http://api.giphy.com/ + +function get_random_top() + local api_key = "dc6zaTOxFJmzC" -- public beta key + b = http.request("http://api.giphy.com/v1/gifs/trending?api_key="..api_key) + local images = json:decode(b).data + math.randomseed(os.time()) + local i = math.random(0,#images) + return images[i].images.downsized.url +end + +function search(text) + local api_key = "dc6zaTOxFJmzC" -- public beta key + b = http.request("http://api.giphy.com/v1/gifs/search?q="..text.."&api_key="..api_key) + local images = json:decode(b).data + math.randomseed(os.time()) + local i = math.random(0,#images) + return images[i].images.downsized.url +end + +function run(msg, matches) + -- If no search data, a cat gif will be sended + -- Because everyone loves pussies + if matches[1] == "!gif" or matches[1] == "!giphy" then + gif_url = get_random_top() + else + gif_url = search(matches[1]) + end + + file = download_to_file(gif_url) + send_document(get_receiver(msg), file, ok_cb, false) + return "preparing to make you laugh" +end + +return { + description = "Sends you a laughable gif", + usage = "", + patterns = { + "^!gif$", + "^!gif (.*)", + "^!giphy (.*)", + "^!giphy$" + }, + run = run +} \ No newline at end of file From 81802ccdce0290f5b688dea795925f53c59ace87 Mon Sep 17 00:00:00 2001 From: Nitesh A Jain Date: Thu, 15 Jan 2015 02:16:47 +0530 Subject: [PATCH 02/17] added xkcd plugin --- plugins/xkcd.lua | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 plugins/xkcd.lua diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua new file mode 100644 index 0000000..a6069c0 --- /dev/null +++ b/plugins/xkcd.lua @@ -0,0 +1,38 @@ + +function get_xkcd() + first_url = http.request("http://xkcd.com/info.0.json") + local xcomicinfo = json:decode(first_url) + math.randomseed(os.time()) + i = math.random(1,xcomicinfo.num) + b = http.request("http://xkcd.com/" .. i .. "/info.0.json") + local comicjson = json:decode(b) + local link_image = comicjson.img + c = http.request("link_image") + local title = comicjson.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) + 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) + url, title = get_xkcd() + file_path = download_to_file(url) + send_photo(receiver, file_path, send_title, {receiver, title}) + return false +end + +return { + description = "send random comic image from xkcd", + usage = "!xkcd", + patterns = {"^!xkcd$"}, + run = run +} + From 9a343040c7b317f9a0a0a8b3d4497f2893a45bce Mon Sep 17 00:00:00 2001 From: Nitesh A Jain Date: Thu, 15 Jan 2015 20:52:24 +0530 Subject: [PATCH 03/17] Oops!! Removed quotes around variable --- plugins/xkcd.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua index a6069c0..94abd30 100644 --- a/plugins/xkcd.lua +++ b/plugins/xkcd.lua @@ -7,7 +7,7 @@ function get_xkcd() b = http.request("http://xkcd.com/" .. i .. "/info.0.json") local comicjson = json:decode(b) local link_image = comicjson.img - c = http.request("link_image") + c = http.request(link_image) local title = comicjson.title if link_image:sub(0,2) == '//' then link_image = msg.text:sub(3,-1) From 013b927c83e6f97b4163aac3940ebf49b82a90d6 Mon Sep 17 00:00:00 2001 From: yago Date: Fri, 23 Jan 2015 23:49:51 +0100 Subject: [PATCH 04/17] Help table generator --- plugins/9gag.lua | 4 +-- plugins/giphy.lua | 7 ++++-- plugins/google.lua | 4 +-- plugins/help.lua | 61 ++++++++++++++++++++++++++++++++++++++++----- plugins/invite.lua | 4 ++- plugins/plugins.lua | 11 +++++--- 6 files changed, 74 insertions(+), 17 deletions(-) diff --git a/plugins/9gag.lua b/plugins/9gag.lua index a40354f..0d4a4f3 100644 --- a/plugins/9gag.lua +++ b/plugins/9gag.lua @@ -27,8 +27,8 @@ function run(msg, matches) end return { - description = "send random image from 9gag", - usage = "!9gag", + description = "9GAG for telegram", + usage = "!9gag: Send random image from 9gag", patterns = {"^!9gag$"}, run = run } diff --git a/plugins/giphy.lua b/plugins/giphy.lua index ccf8697..a2a4ea1 100644 --- a/plugins/giphy.lua +++ b/plugins/giphy.lua @@ -34,8 +34,11 @@ function run(msg, matches) end return { - description = "Sends you a laughable gif", - usage = "", + description = "GIFs from telegram with Giphy API", + usage = { + "!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." + }, patterns = { "^!gif$", "^!gif (.*)", diff --git a/plugins/google.lua b/plugins/google.lua index d3b22de..0481962 100644 --- a/plugins/google.lua +++ b/plugins/google.lua @@ -28,8 +28,8 @@ function run(msg, matches) end return { - description = "Searches Google", - usage = "!google terms", + description = "Searches Google and send results", + usage = "!google [terms]", patterns = { "^!google (.*)$", "^%.[g|G]oogle (.*)$" diff --git a/plugins/help.lua b/plugins/help.lua index d02e43a..31b29e8 100644 --- a/plugins/help.lua +++ b/plugins/help.lua @@ -1,17 +1,66 @@ +-- Generate an HTML table for GitHub +function html_help() + local text = [[ + + + + + + + + ]] -function run(msg, matches) + for k,v in pairs(plugins_names()) do + t = loadfile("plugins/"..v)() + text = text.."" + text = text.."" + text = text.."" + text = text.."" + text = text.."" + end + text = text.."
NameDescriptionUsage
"..v..""..t.description.."" + if (type(t.usage) == "table") then + for ku,vu in pairs(t.usage) do + text = text..vu.."
" + end + else + text = text..t.usage + end + text = text.."
" + return text +end + +function telegram_help( ) local ret = "" for k, dict in pairs(plugins) do - if dict.usage ~= "" then - ret = ret .. dict.usage .. " -> " .. dict.description .. "\n" - end + 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 + end + ret = ret .. " -> " .. dict.description .. "\n" + end end return ret end +function run(msg, matches) + if matches[1] == "!help md" then + return html_help() + else + return telegram_help() + end +end + return { description = "Lists all available commands", - usage = "!help", - patterns = {"^!help$"}, + usage = {"!help", "!help md"}, + patterns = { + "^!help$", + "^!help md$" + }, run = run } \ No newline at end of file diff --git a/plugins/invite.lua b/plugins/invite.lua index b47e848..cd5f452 100644 --- a/plugins/invite.lua +++ b/plugins/invite.lua @@ -31,7 +31,9 @@ end return { description = "Invite other user to the chat group", - usage = "!invite name [user_name], !invite id [user_id]", + usage = { + "!invite name [user_name]", + "!invite id [user_id]" }, patterns = { "^!invite (name) (.*)$", "^!invite (id) (%d+)$" diff --git a/plugins/plugins.lua b/plugins/plugins.lua index af729a7..323b6c6 100644 --- a/plugins/plugins.lua +++ b/plugins/plugins.lua @@ -100,14 +100,17 @@ function run(msg, matches) end return { - description = "Enables, disables and reloads plugins", - usage = "!plugins, !plugins enable [plugin], !plugins disable [plugin], !plugins reload", + description = "Enables, disables and reloads plugins. Privileged users only.", + usage = { + "!plugins: list all plugins", + "!plugins enable [plugin]", + "!plugins disable [plugin]", + "!plugins reload" }, patterns = { "^!plugins$", "^!plugins? (enable) (.*)$", "^!plugins? (disable) (.*)$", - "^!plugins? (reload)$" - }, + "^!plugins? (reload)$" }, run = run, privileged = true } \ No newline at end of file From 4023ce731481a70a3ff2bc64015d282533c3223f Mon Sep 17 00:00:00 2001 From: yago Date: Sat, 24 Jan 2015 16:54:59 +0100 Subject: [PATCH 05/17] Enable more plugins by default --- bot/bot.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bot/bot.lua b/bot/bot.lua index cb9dd85..a7d2f99 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -145,6 +145,8 @@ function create_config( ) "9gag", "echo", "get", + "giphy", + "google", "help", "images", "img_google", @@ -155,6 +157,7 @@ function create_config( ) "stats", "time", "version", + "weather", "youtube" }, sudo_users = {our_id} } From f6e6db50c02c000164624d5a21a9e921f0fb70e7 Mon Sep 17 00:00:00 2001 From: Yago Date: Sat, 24 Jan 2015 17:16:35 +0100 Subject: [PATCH 06/17] Update README.md --- README.md | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 9e90998..e363625 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 ------------ From 5b0cbc807277e4349122d31df4b5c4255083d2ce Mon Sep 17 00:00:00 2001 From: Yago Date: Sat, 24 Jan 2015 17:18:13 +0100 Subject: [PATCH 07/17] Update version --- bot/bot.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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() From 6d388060c70a06d99a9cbb6a126f6b006be1de9d Mon Sep 17 00:00:00 2001 From: Yago Date: Sat, 31 Jan 2015 16:22:33 +0100 Subject: [PATCH 08/17] Update twitter_send.lua --- plugins/twitter_send.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/twitter_send.lua b/plugins/twitter_send.lua index cf243f7..28ea04e 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 @@ -47,4 +47,4 @@ return { usage = "!tw [text]", patterns = {"^!tw (.+)"}, run = run -} \ No newline at end of file +} From 737738a330c25dda73db7bc6ee09278a745c731f Mon Sep 17 00:00:00 2001 From: yago Date: Sat, 31 Jan 2015 16:46:30 +0100 Subject: [PATCH 09/17] Trim string function --- bot/utils.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bot/utils.lua b/bot/utils.lua index 506727b..d922a23 100644 --- a/bot/utils.lua +++ b/bot/utils.lua @@ -31,6 +31,11 @@ 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" From f743a706afb8973ff5b77f9b89efc91ccb1785b2 Mon Sep 17 00:00:00 2001 From: Claudio Filho Date: Mon, 2 Feb 2015 14:57:41 -0200 Subject: [PATCH 10/17] Fixing stats to show the result sorted by messages --- plugins/stats.lua | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/plugins/stats.lua b/plugins/stats.lua index e9fcf7e..131f94c 100644 --- a/plugins/stats.lua +++ b/plugins/stats.lua @@ -61,12 +61,23 @@ local function get_stats_status( msg ) -- vardump(stats) local text = "" 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 + print(">> ", id, user.name) if user.last_name == nil then - text = text..user.name.." ["..id.."]: "..user.msg_num.."\n" + text = text..user.name..": "..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.msg_num.."\n" end end print("usuarios: "..text) @@ -75,7 +86,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,7 +97,7 @@ end _stats = read_file_stats() return { - description = "Numer of messages by user", + description = "Number of messages by user", usage = "!stats", patterns = { ".*", @@ -95,4 +106,4 @@ return { run = run } -end \ No newline at end of file +end From 88e094913c8329582f9645775df20c6ecdb7f09f Mon Sep 17 00:00:00 2001 From: PotHix Date: Mon, 2 Feb 2015 15:59:26 -0200 Subject: [PATCH 11/17] Fixes typo on 'eanbled' --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e363625..289f957 100644 --- a/README.md +++ b/README.md @@ -31,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 @@ -53,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. From d012c26fa6e3a95c5ea42f3918f8c553a90969e3 Mon Sep 17 00:00:00 2001 From: yago Date: Mon, 2 Feb 2015 22:22:05 +0100 Subject: [PATCH 12/17] starts function and little change on download_to_file --- bot/utils.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bot/utils.lua b/bot/utils.lua index d922a23..49f5b02 100644 --- a/bot/utils.lua +++ b/bot/utils.lua @@ -42,8 +42,7 @@ function download_to_file( url , noremove ) 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 @@ -190,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 From 2309b9be87a8900ce4d3775763274d814b3f9b6d Mon Sep 17 00:00:00 2001 From: yago Date: Mon, 2 Feb 2015 22:39:51 +0100 Subject: [PATCH 13/17] updated tg --- tg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tg b/tg index d74db18..01f5d9a 160000 --- a/tg +++ b/tg @@ -1 +1 @@ -Subproject commit d74db187ef3ed9a152979274ba2ea2ad8fe8c8d3 +Subproject commit 01f5d9a3b671edc15a5cff285601115845489985 From 944ee5b356f3065d9829a01d2d8605e52e4bb620 Mon Sep 17 00:00:00 2001 From: yago Date: Mon, 2 Feb 2015 23:00:08 +0100 Subject: [PATCH 14/17] saves and print user id --- plugins/stats.lua | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/plugins/stats.lua b/plugins/stats.lua index 131f94c..7ddf803 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,24 +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..": "..user.msg_num.."\n" + text = text..user.name.." ["..user_id.."]: "..user.msg_num.."\n" else - text = text..user.name.." "..user.last_name..": "..user.msg_num.."\n" + text = text..user.name.." "..user.last_name.." ["..user_id.."]: "..user.msg_num.."\n" end end print("usuarios: "..text) From 4305c7b87eeec61332d25a5bc4f2bb1e90f5e9cc Mon Sep 17 00:00:00 2001 From: yago Date: Mon, 2 Feb 2015 23:18:46 +0100 Subject: [PATCH 15/17] Returns HTTP Error if any in XKCD plugin --- plugins/xkcd.lua | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua index 94abd30..3882d2d 100644 --- a/plugins/xkcd.lua +++ b/plugins/xkcd.lua @@ -1,18 +1,19 @@ function get_xkcd() - first_url = http.request("http://xkcd.com/info.0.json") - local xcomicinfo = json:decode(first_url) - math.randomseed(os.time()) - i = math.random(1,xcomicinfo.num) - b = http.request("http://xkcd.com/" .. i .. "/info.0.json") - local comicjson = json:decode(b) - local link_image = comicjson.img - c = http.request(link_image) - local title = comicjson.title - if link_image:sub(0,2) == '//' then - link_image = msg.text:sub(3,-1) - end - return link_image, title + -- Get the latest num + local res,code = https.request("http://xkcd.com/info.0.json") + if code ~= 200 then return "HTTP ERROR" end + local data = json:decode(res) + math.randomseed(os.time()) + i = math.random(1, data.num) -- Latest + local res,code = http.request("http://xkcd.com/"..i.."/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 send_title(cb_extra, success, result) @@ -30,9 +31,8 @@ function run(msg, matches) end return { - description = "send random comic image from xkcd", + description = "Send random comic image from xkcd", usage = "!xkcd", patterns = {"^!xkcd$"}, run = run -} - +} \ No newline at end of file From bd7264f496a13ed9dec5f8c99959f8dff5f64733 Mon Sep 17 00:00:00 2001 From: yago Date: Wed, 4 Feb 2015 22:15:49 +0100 Subject: [PATCH 16/17] XKCD url sussport --- plugins/xkcd.lua | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua index 3882d2d..6757c3b 100644 --- a/plugins/xkcd.lua +++ b/plugins/xkcd.lua @@ -1,12 +1,12 @@ - -function get_xkcd() - -- Get the latest num +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) - math.randomseed(os.time()) - i = math.random(1, data.num) -- Latest - local res,code = http.request("http://xkcd.com/"..i.."/info.0.json") + 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 @@ -16,6 +16,14 @@ function get_xkcd() 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) @@ -24,15 +32,23 @@ end function run(msg, matches) local receiver = get_receiver(msg) - url, title = get_xkcd() + 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 random comic image from xkcd", - usage = "!xkcd", - patterns = {"^!xkcd$"}, + 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 From 9fc9383ab711bf6ea8a289de6804f0ffd0eafad3 Mon Sep 17 00:00:00 2001 From: yago Date: Wed, 4 Feb 2015 23:03:42 +0100 Subject: [PATCH 17/17] Help well formatted --- plugins/echo.lua | 4 ++-- plugins/get.lua | 6 +++--- plugins/google.lua | 2 +- plugins/gps.lua | 2 +- plugins/help.lua | 28 ++++++++++++++++++---------- plugins/img_google.lua | 4 ++-- plugins/location.lua | 2 +- plugins/plugins.lua | 8 ++++---- plugins/rae.lua | 2 +- plugins/set.lua | 4 ++-- plugins/stats.lua | 4 ++-- plugins/time.lua | 4 ++-- plugins/twitter_send.lua | 2 +- plugins/version.lua | 6 ++++-- 14 files changed, 44 insertions(+), 34 deletions(-) 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 7ddf803..0b9e340 100644 --- a/plugins/stats.lua +++ b/plugins/stats.lua @@ -102,8 +102,8 @@ end _stats = read_file_stats() return { - description = "Number 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)" 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 28ea04e..ba9ae84 100644 --- a/plugins/twitter_send.lua +++ b/plugins/twitter_send.lua @@ -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 } 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 }