This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/plugins/hackernews.lua
topkecleon 26c1299374 Rewrote help.lua and added markup support to several plugins.
help.lua has been rewritten to support "/help command".
New variable "command" has been added to plugins for the syntax (w/out slash) to be displayed in main
help message. "doc" will be displayed upon "/help command".

Output of >12 plugins has been reformated to utilize markup.
There is a fairly standard style throughout plugins.

get_word() in utilities.lua now has defaults for nil arguments.
2016-01-08 08:44:37 -05:00

61 lines
1.4 KiB
Lua
Executable File

local command = 'hackernews'
local doc = [[```
Returns four (if group) or eight (if private message) top stories from Hacker News.
Alias: /hn
```]]
local triggers = {
'^/hackernews[@'..bot.username..']*',
'^/hn[@'..bot.username..']*'
}
local action = function(msg)
sendChatAction(msg.chat.id, 'typing')
local jstr, res = HTTPS.request('https://hacker-news.firebaseio.com/v0/topstories.json')
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
end
local jdat = JSON.decode(jstr)
local res_count = 4
if msg.chat.id == msg.from.id then
res_count = 8
end
local output = '*Hacker News:*\n'
for i = 1, res_count do
local res_url = 'https://hacker-news.firebaseio.com/v0/item/' .. jdat[i] .. '.json'
jstr, res = HTTPS.request(res_url)
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
end
local res_jdat = JSON.decode(jstr)
local title = res_jdat.title:gsub('%[.+%]', ''):gsub('%(.+%)', ''):gsub('&', '&')
if title:len() > 48 then
title = title:sub(1, 45) .. '...'
end
local url = res_jdat.url
if url:find('%(') then
output = output .. '' .. title .. '\n' .. url:gsub('_', '\\_') .. '\n'
else
output = output .. '• [' .. title .. '](' .. url .. ')\n'
end
end
sendMessage(msg.chat.id, output, true, nil, true)
end
return {
action = action,
triggers = triggers,
doc = doc,
command = command
}