local hackernews = {}
local HTTPS = require('ssl.https')
local JSON = require('cjson')
local bindings = require('bindings')
local utilities = require('utilities')
hackernews.command = 'hackernews'
hackernews.doc = [[```
Returns four (if group) or eight (if private message) top stories from Hacker News.
Alias: /hn
```]]
hackernews.triggers = utilities.triggers():t('hackernews', true):t('hn', true).table
function hackernews:action(msg)
bindings.sendChatAction(self, msg.chat.id, 'typing')
local jstr, res = HTTPS.request('https://hacker-news.firebaseio.com/v0/topstories.json')
if res ~= 200 then
bindings.sendReply(self, msg, self.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
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)
local res_jdat = JSON.decode(jstr)
local title = res_jdat.title:gsub('%[.+%]', ''):gsub('%(.+%)', ''):gsub('&', '&')
if title:len() > 48 then
title = title:sub(1, 45) .. '...'
local url = res_jdat.url
if not url then
if url:find('%(') then
output = output .. '• ' .. title .. '\n' .. url:gsub('_', '\\_') .. '\n'
else
output = output .. '• [' .. title .. '](' .. url .. ')\n'
bindings.sendMessage(self, msg.chat.id, output, true, nil, true)
return hackernews