Comparison

mod_slack_webhooks/mod_slack_webhooks.lua @ 3000:02fc3b64cbb7

Initial commit of mod_slack_webhooks. This provides an HTTP-based interface to and from Prosody-hosted MUCs equivalent to Slack's incoming and outgoing webhook interfaces, allowing a variety of Slack integrations to be used with a Prosody MUC.
author Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
date Sun, 15 Apr 2018 08:45:43 -0700
child 3071:b6cac9d72255
comparison
equal deleted inserted replaced
2999:d631fd9a3300 3000:02fc3b64cbb7
1 -- Allow Slack-style incoming and outgoing hooks to MUC rooms
2 -- Based on mod_muc_intercom and mod_post_msg
3 -- Copyright 2016-2017 Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
4 --
5 -- This file is MIT/X11 licensed.
6
7 module:depends"http"
8
9 local host_session = prosody.hosts[module.host];
10 local msg = require "util.stanza".message;
11 local jid = require "util.jid";
12 local now = require "util.datetime".datetime;
13 local b64_decode = require "util.encodings".base64.decode;
14 local json = require "util.json"
15 local formdecode = require "net.http".formdecode;
16 local xml = require "util.xml";
17 local http = require "net.http";
18
19 local function get_room_by_jid(mod_muc, jid)
20 if mod_muc.get_room_by_jid then
21 return mod_muc.get_room_by_jid(jid);
22 elseif mod_muc.rooms then
23 return mod_muc.rooms[jid]; -- COMPAT 0.9, 0.10
24 end
25 end
26
27 local routing = module:get_option("outgoing_webhook_routing") or {};
28 local listen_path = module:get_option("incoming_webhook_path") or "/webhook";
29 local default_from_nick = module:get_option("incoming_webhook_default_nick") or "Bot";
30
31 function postcallback(content, code)
32 module:log("debug", "HTTP result %d", code)
33 end
34
35 function check_message(data)
36 local origin, stanza = data.origin, data.stanza;
37 local mod_muc = host_session.muc;
38 if not mod_muc then return; end
39
40 local this_room = get_room_by_jid(mod_muc, stanza.attr.to);
41 if not this_room then return; end -- no such room
42
43 local from_room_jid = this_room._jid_nick[stanza.attr.from];
44 if not from_room_jid then return; end -- no such nick
45
46 local from_room, from_host, from_nick = jid.split(from_room_jid);
47
48 local body = stanza:get_child("body");
49 if not body then return; end -- No body, like topic changes
50 body = body and body:get_text(); -- I feel like I want to do `or ""` there :/
51
52 if not routing[from_room] then
53 return;
54 end
55
56 local json_out = {channel_name = from_room, timestamp = now(), text = body, team_domain = from_host, user_name = from_nick};
57 local stanzaid = stanza:get_child("id");
58 if stanzaid and string.sub(stanzaid,1,string.len("webhookbot"))=="webhookbot" then
59 json_out["bot_id"] = "webhookbot";
60 end
61
62 json_out = json.encode(json_out)
63 local url = routing[from_room];
64 module:log("debug", "message from %s in %s to %s", from_nick, from_room, url);
65 local headers = {
66 ["Content-Type"] = "application/json",
67 };
68 http.request(url, { method = "POST", body = json_out, headers = headers }, postcallback)
69 end
70
71 module:hook("message/bare", check_message, 10);
72
73 local function route_post(f)
74 return function(event, path)
75 local request = event.request;
76 local headers = request.headers;
77 local bare_room = jid.join(path, module.host);
78 local mod_muc = host_session.muc;
79 if not get_room_by_jid(mod_muc, bare_room) then
80 module:log("warn", "mod_slack_webhook: invalid JID: %s", bare_room);
81 return 404;
82 end
83 -- Check secret?
84 return f(event, path)
85 end
86 end
87
88 local function handle_post(event, path)
89 local mod_muc = host_session.muc;
90 local request = event.request;
91 local response = event.response;
92 local headers = request.headers;
93
94 local body_type = headers.content_type;
95 local message;
96 local post_body;
97 if body_type == "application/x-www-form-urlencoded" then
98 post_body = formdecode(request.body)["payload"];
99 elseif body_type == "application/json" then
100 if not pcall(function() post_body = json.decode(request.body) end) then
101 return 420;
102 end
103 else
104 return 422;
105 end
106 local bare_room = jid.join(path, module.host);
107 local dest_room = get_room_by_jid(mod_muc, bare_room);
108 local from_nick = default_from_nick;
109 if post_body["username"] then
110 from_nick = post_body["username"];
111 end
112 local sender = jid.join(path, module.host, from_nick);
113 module:log("debug", "message to %s from %s", bare_room, sender);
114 module:log("debug", "body: %s", post_body["text"]);
115 message = msg({ to = bare_room, from = sender, type = "groupchat", id="webhookbot" .. now()},post_body["text"]);
116 dest_room:broadcast_message(message, true);
117 return 201;
118 end
119
120 module:provides("http", {
121 default_path = listen_path;
122 route = {
123 ["POST /*"] = route_post(handle_post);
124 OPTIONS = function(e)
125 local headers = e.response.headers;
126 headers.allow = "POST";
127 headers.accept = "application/x-www-form-urlencoded, application/json";
128 return 200;
129 end;
130 }
131 });
132