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
This commit is contained in:
topkecleon 2016-05-07 19:30:48 -04:00
parent d00403eb2e
commit c8e8be144f
4 changed files with 29 additions and 21 deletions

View File

@ -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.

View File

@ -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

View File

@ -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)

View File

@ -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)