plugins/github.lua

changeset 119
e09157903d93
child 120
91a29c964e97
equal deleted inserted replaced
118:b3fa3397da0b 119:e09157903d93
1 local url = require"socket.url";
2 local json = require"util.json";
3 local http = require"net.http";
4
5 function riddim.plugins.github(bot)
6 local conf = bot.config.github;
7 local base_url = url.parse("https://api.github.com/repos/x/y/issues/123");
8 local base_path = url.parse_path(base_url.path);
9 base_path[2], base_path[3] = conf.user, conf.project;
10
11 local ex = {
12 headers = {
13 Accept = "application/vnd.github.v3+json";
14 };
15 };
16
17 local function issue_url(number)
18 base_path[5] = number;
19 base_url.path = url.build_path(base_path);
20 local url = url.build(base_url);
21 print("url:", url);
22 return url;
23 end
24
25 bot:hook("commands/issue", function (command)
26 local issue_id = tonumber(command.param);
27 if not issue_id then return; end
28 print(issue_id);
29 assert(http.request(issue_url(issue_id), ex, function (issue, code)
30 if code > 400 then
31 return command:reply("HTTP Error "..code.." :(");
32 end
33 issue = issue and json.decode(issue);
34 if not issue then
35 return command:reply("Got invalid JSON back :(");
36 end
37 command:reply(("%s #%d\n%s"):format(issue.title, issue.number issue.html_url));
38 end));
39 return true;
40 end);
41
42 local function check_for_issue_id(message)
43 local issue_id = message.body and message.body:match"#(%d+)";
44 if issue_id then
45 return bot:event("commands/issue", { param = issue_id, reply = message.reply, });
46 end
47 end
48
49 bot:hook("message", check_for_issue_id);
50
51 bot:hook("groupchat/joining", function (room)
52 room:hook("message", check_for_issue_id);
53 end);
54 end

mercurial