From c8e8be144fbf160a45413868f1f9bf66b847fb79 Mon Sep 17 00:00:00 2001 From: topkecleon Date: Sat, 7 May 2016 19:30:48 -0400 Subject: [PATCH] xkcd.lua: Working again, but no more queries (for now). fortune.lua: The bad spacing annoyed me to no end. Monospaced now. patterns.lua: Error message upon malformed patterns. readme.md: outdated, notice --- README.md | 2 ++ plugins/fortune.lua | 3 ++- plugins/patterns.lua | 10 ++++++++++ plugins/xkcd.lua | 35 +++++++++++++++-------------------- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 3509d9d..2ae0797 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # otouto The plugin-wielding, multipurpose Telegram bot. +**This readme is outdated as of 3.7. Proceed with caution.** + [Public Bot](http://telegram.me/mokubot) | [Official Channel](http://telegram.me/otouto) | [Development Group](http://telegram.me/BotDevelopment) otouto is an independently-developed Telegram API bot written in Lua. Originally conceived as a CLI script in February of 2015, otouto has since been open-sourced and migrated to the API, and is being developed to this day. diff --git a/plugins/fortune.lua b/plugins/fortune.lua index 38fd008..edd5f1b 100755 --- a/plugins/fortune.lua +++ b/plugins/fortune.lua @@ -23,7 +23,8 @@ function fortune:action(msg) local fortunef = io.popen('fortune') local output = fortunef:read('*all') - bindings.sendMessage(self, msg.chat.id, output) + output = '```\n' .. output .. '\n```' + bindings.sendMessage(self, msg.chat.id, output, true, nil, true) fortunef:close() end diff --git a/plugins/patterns.lua b/plugins/patterns.lua index 4000c60..1f57eda 100644 --- a/plugins/patterns.lua +++ b/plugins/patterns.lua @@ -15,6 +15,16 @@ function patterns:action(msg) local output = msg.reply_to_message.text or '' local m1, m2 = msg.text:match('^/?s/(.-)/(.-)/?$') if not m2 then return true end + local res, output = pcall( + function() + return output:gsub(m1, m2) + end + ) + if res == false then + output = 'Malformed pattern!' + bindings.sendReply(self, msg, output) + return + end output = output:gsub(m1, m2) output = 'Did you mean:\n"' .. output:sub(1, 4000) .. '"' bindings.sendReply(self, msg.reply_to_message, output) diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua index 4838744..26685c6 100755 --- a/plugins/xkcd.lua +++ b/plugins/xkcd.lua @@ -7,10 +7,10 @@ local JSON = require('dkjson') local bindings = require('bindings') local utilities = require('utilities') -xkcd.command = 'xkcd [query]' +xkcd.command = 'xkcd [i]' xkcd.doc = [[``` -/xkcd [query] -Returns an xkcd strip and its alt text. If there is no query, it will be randomized. +/xkcd [i] +Returns the latest xkcd strip and its alt text. If a number is given, returns that number strip. If "r" is passed in place of a number, returns a random strip. ```]] function xkcd:init() @@ -19,34 +19,29 @@ end function xkcd:action(msg) - local input = utilities.input(msg.text) - local jstr, res = HTTP.request('http://xkcd.com/info.0.json') if res ~= 200 then bindings.sendReply(self, msg, self.config.errors.connection) return end - local latest = JSON.decode(jstr).num - local res_url + local strip_num = latest + local input = utilities.input(msg.text) if input then - local url = 'https://ajax.googleapis.com/ajax/services/search/web?v=1.0&safe=active&q=site%3axkcd%2ecom%20' .. URL.escape(input) - jstr, res = HTTPS.request(url) - if res ~= 200 then - bindings.sendReply(self, msg, self.config.errors.connection) - return + if tonumber(input) then + if tonumber(input) > latest then + strip_num = latest + else + strip_num = input + end + elseif input == 'r' then + strip_num = math.random(latest) end - local jdat = JSON.decode(jstr) - if #jdat.responseData.results == 0 then - bindings.sendReply(self, msg, self.config.errors.results) - return - end - res_url = jdat.responseData.results[1].url .. 'info.0.json' - else - res_url = 'http://xkcd.com/' .. math.random(latest) .. '/info.0.json' end + local res_url = 'http://xkcd.com/' .. strip_num .. '/info.0.json' + jstr, res = HTTP.request(res_url) if res ~= 200 then bindings.sendReply(self, msg, self.config.errors.connection)