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/miku/plugins/wordpress_posts.lua

71 lines
2.0 KiB
Lua
Raw Normal View History

local wordpress_recent_post = {}
wordpress_recent_post.triggers = {
"^/([Aa][Kk][Aa])$",
"^/([Pp][Ww])$"
}
local makeOurDate = function(dateString)
local pattern = "(%d+)%-(%d+)%-(%d+)"
local year, month, day = dateString:match(pattern)
return day..'.'..month..'.'..year
end
function wordpress_recent_post:get_full_url(blog)
local url = blog..'/?json=get_recent_posts'
local doer = http
if url:match('^https') then
doer = https
end
local res, code = doer.request(url)
if code ~= 200 then return nil end
local data = json.decode(res).posts[1]
if not data then return nil end
local title = unescape(data.title)
local from = unescape(data.author.name)
local posted_at = makeOurDate(data.date)
local content = data.excerpt:match('<p>(.*)<span')
if not content then
content = data.excerpt:match('<p>(.*)</p>')
end
local content = unescape(content)
local url = data.url
if data.thumbnail then
image_url = data.thumbnail
else
image_url = nil
end
local text = '<b>'..title..'</b>\n<i>'..from..' am '..posted_at..'</i>\n'..content..'...\n<a href="'..url..'">Artikel aufrufen</a>'
if image_url then
local text = title..' ('..from..' am '..posted_at..')\n\n'..content..'...\n'..url -- captions don't support markdown
return text, image_url
else
return text
end
end
function wordpress_recent_post:action(msg, config, matches)
if matches[1]:match('[Aa][Kk][Aa]') then
blog = 'http://akamaru.de'
elseif matches[1]:match('[Pp][Ww]') then
blog = 'https://ponywave.de'
end
local text, image_url = wordpress_recent_post:get_full_url(blog)
if not text then
utilities.send_reply(self, msg, config.errors.connection)
return
end
if image_url then
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local file = download_to_file(image_url)
utilities.send_photo(self, msg.chat.id, file, text, msg.message_id)
else
utilities.send_reply(self, msg, text, 'HTML')
end
end
return wordpress_recent_post