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/otouto/plugins/preview.lua

44 lines
1.2 KiB
Lua
Raw Normal View History

local preview = {}
local HTTP = require('socket.http')
2016-06-07 06:31:34 +02:00
local utilities = require('otouto.utilities')
preview.command = 'preview <link>'
function preview:init(config)
2016-08-14 04:46:18 +02:00
preview.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('preview', true).table
preview.doc = config.cmd_pat .. 'preview <link> \nReturns a full-message, "unlinked" preview.'
end
function preview:action(msg)
2016-08-14 04:46:18 +02:00
local input = utilities.input_from_msg(msg)
if not input then
utilities.send_reply(msg, preview.doc, true)
2016-08-14 04:46:18 +02:00
return
end
input = utilities.get_word(input, 1)
if not input:match('^https?://.+') then
input = 'http://' .. input
end
local res = HTTP.request(input)
if not res then
utilities.send_reply(msg, 'Please provide a valid link.')
2016-08-14 04:46:18 +02:00
return
end
if res:len() == 0 then
utilities.send_reply(msg, 'Sorry, the link you provided is not letting us make a preview.')
2016-08-14 04:46:18 +02:00
return
end
-- Invisible zero-width, non-joiner.
local output = '<a href="' .. input .. '">' .. utilities.char.zwnj .. '</a>'
utilities.send_message(msg.chat.id, output, false, nil, 'html')
end
return preview