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 # otouto
The plugin-wielding, multipurpose Telegram bot. 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) [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. 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 fortunef = io.popen('fortune')
local output = fortunef:read('*all') 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() fortunef:close()
end end

View File

@ -15,6 +15,16 @@ function patterns:action(msg)
local output = msg.reply_to_message.text or '' local output = msg.reply_to_message.text or ''
local m1, m2 = msg.text:match('^/?s/(.-)/(.-)/?$') local m1, m2 = msg.text:match('^/?s/(.-)/(.-)/?$')
if not m2 then return true end 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 = output:gsub(m1, m2)
output = 'Did you mean:\n"' .. output:sub(1, 4000) .. '"' output = 'Did you mean:\n"' .. output:sub(1, 4000) .. '"'
bindings.sendReply(self, msg.reply_to_message, output) bindings.sendReply(self, msg.reply_to_message, output)

View File

@ -7,10 +7,10 @@ local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')
xkcd.command = 'xkcd [query]' xkcd.command = 'xkcd [i]'
xkcd.doc = [[``` xkcd.doc = [[```
/xkcd [query] /xkcd [i]
Returns an xkcd strip and its alt text. If there is no query, it will be randomized. 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() function xkcd:init()
@ -19,33 +19,28 @@ end
function xkcd:action(msg) function xkcd:action(msg)
local input = utilities.input(msg.text)
local jstr, res = HTTP.request('http://xkcd.com/info.0.json') local jstr, res = HTTP.request('http://xkcd.com/info.0.json')
if res ~= 200 then if res ~= 200 then
bindings.sendReply(self, msg, self.config.errors.connection) bindings.sendReply(self, msg, self.config.errors.connection)
return return
end end
local latest = JSON.decode(jstr).num local latest = JSON.decode(jstr).num
local res_url local strip_num = latest
local input = utilities.input(msg.text)
if input then 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) if tonumber(input) then
jstr, res = HTTPS.request(url) if tonumber(input) > latest then
if res ~= 200 then strip_num = latest
bindings.sendReply(self, msg, self.config.errors.connection)
return
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 else
res_url = 'http://xkcd.com/' .. math.random(latest) .. '/info.0.json' strip_num = input
end end
elseif input == 'r' then
strip_num = math.random(latest)
end
end
local res_url = 'http://xkcd.com/' .. strip_num .. '/info.0.json'
jstr, res = HTTP.request(res_url) jstr, res = HTTP.request(res_url)
if res ~= 200 then if res ~= 200 then