|
1 function riddim.plugins.urltitle(bot) |
|
2 require "net.httpclient_listener"; |
|
3 local http = require "net.http"; |
|
4 |
|
5 local function handler(message) |
|
6 local url = message.body and message.body:match("https?://%S+"); |
|
7 if url then |
|
8 http.request(url, nil, function (data, code) |
|
9 if code ~= 200 then return end |
|
10 local title = data:match("<title[^>]*>([^<]+)"); |
|
11 |
|
12 if title then |
|
13 title = title:gsub("\n", ""); |
|
14 if message.room then |
|
15 message.room:send(title); |
|
16 else |
|
17 message:reply(title); |
|
18 end |
|
19 end |
|
20 end); |
|
21 end |
|
22 end |
|
23 bot:hook("message", handler); |
|
24 bot:hook("groupchat/joined", function(room) |
|
25 room:hook("message", handler) |
|
26 end); |
|
27 end |