plugins/urltitle.lua

Tue, 22 Sep 2020 14:59:25 +0200

author
Seve Ferrer <seve@delape.net>
date
Tue, 22 Sep 2020 14:59:25 +0200
changeset 159
7ca948b157df
parent 115
6498ca5ed831
permissions
-rw-r--r--

plugins/commands.lua: The default command_prefix value is set on config.docker.lua.

function riddim.plugins.urltitle(bot)
	local http = require "net.http";

	local function handler(message)
		local url = message.body and message.body:match("https?://%S+");
		if url then
			http.request(url, nil, function (data, code)
				if code ~= 200 then return end
				local title = data:match("<title[^>]*>([^<]+)");

				if title then
					title = title:gsub("\n", "");
					if message.room then
						message.room:send_message(title)
					else
						message:reply(title);
					end
				end
			end);
		end
	end
	bot:hook("message", handler);
	bot:hook("groupchat/joined", function(room)
		room:hook("message", handler)
	end);
end

mercurial