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

47 lines
974 B
Lua
Raw Normal View History

local doc = [[
2015-07-08 09:38:04 +02:00
/hackernews
Returns four (if group) or eight (if private message) top stories from Hacker News.
2015-07-03 00:15:52 +02:00
]]
local triggers = {
'^/hackernews[@'..bot.username..']*',
'^/hn[@'..bot.username..']*'
2015-07-03 00:15:52 +02:00
}
local action = function(msg)
2015-07-03 00:15:52 +02:00
local jstr, res = HTTPS.request('https://hacker-news.firebaseio.com/v0/topstories.json')
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
end
2015-07-03 00:15:52 +02:00
local jdat = JSON.decode(jstr)
local res_count = 4
2015-07-03 00:15:52 +02:00
if msg.chat.id == msg.from.id then
res_count = 8
2015-07-03 00:15:52 +02:00
end
local message = ''
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)
message = message .. res_jdat.title .. '\n ' .. res_jdat.url .. '\n'
2015-07-03 00:15:52 +02:00
end
sendReply(msg, message)
2015-07-03 00:15:52 +02:00
end
return {
action = action,
triggers = triggers,
doc = doc
}