From 57ad52893fc6c89d16fbc70680398fc143eba4c3 Mon Sep 17 00:00:00 2001 From: yago Date: Sat, 4 Oct 2014 17:01:29 +0200 Subject: [PATCH 1/3] updated tg --- tg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tg b/tg index ddcd8ad..7a9764f 160000 --- a/tg +++ b/tg @@ -1 +1 @@ -Subproject commit ddcd8ad44eab32ac977601be0a8e9c8be9420777 +Subproject commit 7a9764f24ac745eb03a72e2ddb69c8574f6a8c2f From 86c80966310a72f8de9c2e1d311bd7fc34af8168 Mon Sep 17 00:00:00 2001 From: yago Date: Sat, 4 Oct 2014 17:02:29 +0200 Subject: [PATCH 2/3] @javierhonduco Dulcinea --- bot/bot.lua | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/bot/bot.lua b/bot/bot.lua index eccd4d6..bcd3a36 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -59,7 +59,6 @@ function send_image_from_url (msg) end function is_image_url(text) - print ('IS image ' .. text ..'?') last = string.get_last_word(text) extension = string.get_extension_from_filename(last) if extension == 'jpg' or extension == 'png' then @@ -102,6 +101,12 @@ function do_action(msg) return end + if string.starts(msg.text, '!rae') then + text = msg.text:sub(6,-1) + meaning = getDulcinea(text) + send_msg(receiver, meaning, ok_cb, false) + end + if string.starts(msg.text, '!9gag') then url, title = get_9GAG() file_path = download_to_file(url) @@ -271,14 +276,39 @@ function get_fortunes_uc3m() return b end -function getGoogleImage(text) - local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" - text = URL.escape(text) +function getDulcinea( text ) + local api = "http://dulcinea.herokuapp.com/api/?query=" b = http.request(api..text) - local google = json:decode(b) - math.randomseed(os.time()) - i = math.random(#google.responseData.results) - return google.responseData.results[i].url + dulcinea = json:decode(b) + while dulcinea.type == "multiple" do + text = dulcinea.response[1].id + b = http.request(api..text) + dulcinea = json:decode(b) + end + local text = "" + vardump(dulcinea) + for i = 1, 5, 1 do + text = text .. dulcinea.response[i].word .."\n" + text = text .. dulcinea.response[i].meanings[1].meaning .."\n" + text = text .. "\n" + end + return text + +end + +function getGoogleImage(text) + text = URL.escape(text) + for i = 1, 5, 1 do -- Try 5 times + local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q=" + b = http.request(api..text) + local google = json:decode(b) + + if (google.responseStatus == 200) then -- OK + math.randomseed(os.time()) + i = math.random(#google.responseData.results) -- Random image from results + return google.responseData.results[i].url + end + end end function get_9GAG() From 5d984b1dc12c943cc12c0076aeedd45176736f9a Mon Sep 17 00:00:00 2001 From: yago Date: Sat, 4 Oct 2014 17:36:33 +0200 Subject: [PATCH 3/3] Loop for meanings and responses --- bot/bot.lua | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/bot/bot.lua b/bot/bot.lua index bcd3a36..ba01f78 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -280,17 +280,30 @@ function getDulcinea( text ) local api = "http://dulcinea.herokuapp.com/api/?query=" b = http.request(api..text) 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 = "" vardump(dulcinea) - for i = 1, 5, 1 do - text = text .. dulcinea.response[i].word .."\n" - text = text .. dulcinea.response[i].meanings[1].meaning .."\n" - text = text .. "\n" + local text = "" + local responses = #dulcinea.response + 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 + if (meanings > 5) then + meanings = 5 + end + for j = 1, meanings, 1 do + local meaning = dulcinea.response[i].meanings[j].meaning + text = text .. meaning .. "\n\n" + end end return text