2016-08-16 00:53:23 +02:00
|
|
|
local wordpress_recent_post = {}
|
|
|
|
|
2016-08-16 00:58:42 +02:00
|
|
|
wordpress_recent_post.triggers = {
|
2016-08-16 13:03:12 +02:00
|
|
|
"^/([Aa][Kk][Aa]) (.+)$",
|
2016-08-16 00:58:42 +02:00
|
|
|
"^/([Aa][Kk][Aa])$",
|
|
|
|
}
|
2016-08-16 00:53:23 +02:00
|
|
|
|
|
|
|
local makeOurDate = function(dateString)
|
|
|
|
local pattern = "(%d+)%-(%d+)%-(%d+)"
|
|
|
|
local year, month, day = dateString:match(pattern)
|
|
|
|
return day..'.'..month..'.'..year
|
|
|
|
end
|
|
|
|
|
2016-08-16 13:03:12 +02:00
|
|
|
function wordpress_recent_post:get_full_url(blog, tag)
|
|
|
|
if tag then
|
|
|
|
url = blog..'/?json=get_search_results&search='..tag
|
|
|
|
else
|
|
|
|
url = blog..'/?json=get_recent_posts'
|
|
|
|
end
|
2017-01-24 21:05:40 +01:00
|
|
|
local res, code = https.request(url)
|
2016-08-16 00:53:23 +02:00
|
|
|
if code ~= 200 then return nil end
|
|
|
|
local data = json.decode(res).posts[1]
|
2016-08-16 13:03:12 +02:00
|
|
|
if not data then return 'NOTFOUND' end
|
2016-08-16 00:53:23 +02:00
|
|
|
|
2016-08-16 13:05:26 +02:00
|
|
|
local title = unescape(data.title)
|
|
|
|
local from = unescape(data.author.name)
|
2016-08-16 00:53:23 +02:00
|
|
|
local posted_at = makeOurDate(data.date)
|
|
|
|
local content = data.excerpt:match('<p>(.*)<span')
|
|
|
|
if not content then
|
|
|
|
content = data.excerpt:match('<p>(.*)</p>')
|
|
|
|
end
|
2016-08-16 13:03:12 +02:00
|
|
|
if not content then
|
|
|
|
content = ""
|
|
|
|
else
|
2016-08-16 13:05:26 +02:00
|
|
|
content = '\n'..unescape(content)..'...'
|
2016-08-16 13:03:12 +02:00
|
|
|
end
|
2016-08-16 00:53:23 +02:00
|
|
|
local url = data.url
|
|
|
|
if data.thumbnail then
|
|
|
|
image_url = data.thumbnail
|
|
|
|
else
|
|
|
|
image_url = nil
|
|
|
|
end
|
|
|
|
|
2017-01-24 21:05:40 +01:00
|
|
|
local text = '<b>'..title..'</b>\n<i>'..from..' am '..posted_at..'</i>'..content..'\n<a href="'..url..'">Beitrag aufrufen.</a>'
|
2016-08-16 01:02:24 +02:00
|
|
|
return text, image_url
|
2016-08-16 00:53:23 +02:00
|
|
|
end
|
|
|
|
|
2016-08-16 00:58:42 +02:00
|
|
|
function wordpress_recent_post:action(msg, config, matches)
|
2017-01-24 21:05:40 +01:00
|
|
|
local blog = 'https://akamaru.de'
|
2016-08-16 13:03:12 +02:00
|
|
|
local text, image_url = wordpress_recent_post:get_full_url(blog, matches[2])
|
2016-08-16 00:53:23 +02:00
|
|
|
if not text then
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_reply(msg, config.errors.connection)
|
2016-08-16 00:53:23 +02:00
|
|
|
return
|
2016-08-16 13:03:12 +02:00
|
|
|
elseif text == 'NOTFOUND' then
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_reply(msg, config.errors.results)
|
2016-08-16 13:03:12 +02:00
|
|
|
return
|
2016-08-16 00:53:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if image_url then
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_typing(msg.chat.id, 'upload_photo')
|
2016-11-29 18:14:26 +01:00
|
|
|
utilities.send_photo(msg.chat.id, image_url, nil, msg.message_id)
|
2016-08-16 00:53:23 +02:00
|
|
|
end
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_reply(msg, text, 'HTML')
|
2016-08-16 00:53:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return wordpress_recent_post
|