2016-04-11 06:04:47 +02:00
|
|
|
|
local preview = {}
|
|
|
|
|
|
|
|
|
|
local HTTP = require('socket.http')
|
2016-07-17 13:22:27 +02:00
|
|
|
|
local utilities = require('miku.utilities')
|
2016-04-11 06:04:47 +02:00
|
|
|
|
|
|
|
|
|
preview.command = 'preview <link>'
|
2016-05-27 05:28:44 +02:00
|
|
|
|
|
|
|
|
|
function preview:init(config)
|
|
|
|
|
preview.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('preview', true).table
|
|
|
|
|
preview.doc = [[```
|
|
|
|
|
]]..config.cmd_pat..[[preview <link>
|
2016-01-13 19:00:17 +01:00
|
|
|
|
Returns a full-message, "unlinked" preview.
|
|
|
|
|
```]]
|
2016-04-11 06:04:47 +02:00
|
|
|
|
end
|
2016-01-13 19:00:17 +01:00
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
|
function preview:action(msg)
|
2016-01-13 19:00:17 +01:00
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
|
local input = utilities.input(msg.text)
|
2016-01-13 19:00:17 +01:00
|
|
|
|
|
|
|
|
|
if not input then
|
2016-05-29 19:08:39 +02:00
|
|
|
|
utilities.send_message(self, msg.chat.id, preview.doc, true, nil, true)
|
2016-01-13 19:00:17 +01:00
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
|
input = utilities.get_word(input, 1)
|
2016-01-13 19:00:17 +01:00
|
|
|
|
if not input:match('^https?://.+') then
|
|
|
|
|
input = 'http://' .. input
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local res = HTTP.request(input)
|
|
|
|
|
if not res then
|
2016-05-29 19:08:39 +02:00
|
|
|
|
utilities.send_reply(self, msg, 'Please provide a valid link.')
|
2016-01-13 19:00:17 +01:00
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if res:len() == 0 then
|
2016-05-29 19:08:39 +02:00
|
|
|
|
utilities.send_reply(self, msg, 'Sorry, the link you provided is not letting us make a preview.')
|
2016-01-13 19:00:17 +01:00
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Invisible zero-width, non-joiner.
|
|
|
|
|
local output = '[](' .. input .. ')'
|
2016-05-29 19:08:39 +02:00
|
|
|
|
utilities.send_message(self, msg.chat.id, output, false, nil, true)
|
2016-01-13 19:00:17 +01:00
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
|
return preview
|