annotate mod_slack_webhooks/mod_slack_webhooks.lua @ 3902:341850e8866f

mod_muc_moderation: Broadcast retraction via method that saves it Otherwise the retraction is not saved to history, so that those who join after it was sent are unaware of the retraction
author Kim Alvefur <zash@zash.se>
date Sat, 22 Feb 2020 21:41:31 +0100
parents 284d8c18060e
children 724003f24308
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
1 -- Allow Slack-style incoming and outgoing hooks to MUC rooms
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
2 -- Based on mod_muc_intercom and mod_post_msg
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
3 -- Copyright 2016-2017 Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
4 --
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
5 -- This file is MIT/X11 licensed.
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
6
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
7 module:depends"http"
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
8
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
9 local msg = require "util.stanza".message;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
10 local jid = require "util.jid";
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
11 local now = require "util.datetime".datetime;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
12 local json = require "util.json"
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
13 local formdecode = require "net.http".formdecode;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
14 local http = require "net.http";
3146
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
15 local dataform = require "util.dataforms";
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
16
3443
284d8c18060e mod_slack_webhooks: Fix use with 0.11+ MUC API
Kim Alvefur <zash@zash.se>
parents: 3148
diff changeset
17 local mod_muc = module:depends"muc";
284d8c18060e mod_slack_webhooks: Fix use with 0.11+ MUC API
Kim Alvefur <zash@zash.se>
parents: 3148
diff changeset
18 local rooms = rawget(mod_muc, "rooms");
284d8c18060e mod_slack_webhooks: Fix use with 0.11+ MUC API
Kim Alvefur <zash@zash.se>
parents: 3148
diff changeset
19 local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
284d8c18060e mod_slack_webhooks: Fix use with 0.11+ MUC API
Kim Alvefur <zash@zash.se>
parents: 3148
diff changeset
20 function (room_jid)
284d8c18060e mod_slack_webhooks: Fix use with 0.11+ MUC API
Kim Alvefur <zash@zash.se>
parents: 3148
diff changeset
21 return rooms[room_jid];
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
22 end
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
23
3147
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
24 local button_ns = "xmpp:prosody.im/community/mod_slack_webhooks#buttons";
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
25 local routing = module:get_option("outgoing_webhook_routing") or {};
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
26 local listen_path = module:get_option("incoming_webhook_path") or "/webhook";
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
27 local default_from_nick = module:get_option("incoming_webhook_default_nick") or "Bot";
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
28
3092
0f63e999d0e4 mod_slack_webhooks: Remove unused locals [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3071
diff changeset
29 function postcallback(_, code)
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
30 module:log("debug", "HTTP result %d", code)
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
31 end
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
32
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
33 function check_message(data)
3092
0f63e999d0e4 mod_slack_webhooks: Remove unused locals [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3071
diff changeset
34 local stanza = data.stanza;
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
35
3443
284d8c18060e mod_slack_webhooks: Fix use with 0.11+ MUC API
Kim Alvefur <zash@zash.se>
parents: 3148
diff changeset
36 local this_room = get_room_from_jid(stanza.attr.to);
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
37 if not this_room then return; end -- no such room
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
38
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
39 local from_room_jid = this_room._jid_nick[stanza.attr.from];
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
40 if not from_room_jid then return; end -- no such nick
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
41
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
42 local from_room, from_host, from_nick = jid.split(from_room_jid);
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
43
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
44 local body = stanza:get_child("body");
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
45 if not body then return; end -- No body, like topic changes
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
46 body = body and body:get_text(); -- I feel like I want to do `or ""` there :/
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
47
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
48 if not routing[from_room] then
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
49 return;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
50 end
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
51
3094
39d714abbc2b mod_slack_webhooks: Split long line to improve readability [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3093
diff changeset
52 local json_out = {
39d714abbc2b mod_slack_webhooks: Split long line to improve readability [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3093
diff changeset
53 channel_name = from_room,
39d714abbc2b mod_slack_webhooks: Split long line to improve readability [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3093
diff changeset
54 timestamp = now(),
39d714abbc2b mod_slack_webhooks: Split long line to improve readability [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3093
diff changeset
55 text = body,
39d714abbc2b mod_slack_webhooks: Split long line to improve readability [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3093
diff changeset
56 team_domain = from_host,
39d714abbc2b mod_slack_webhooks: Split long line to improve readability [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3093
diff changeset
57 user_name = from_nick,
39d714abbc2b mod_slack_webhooks: Split long line to improve readability [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3093
diff changeset
58 };
3147
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
59
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
60 local form = stanza:get_child("x", "jabber:x:form");
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
61 if form and form.attr.type == "submit" then
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
62 local callback_id, button_name, button_value;
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
63 for field in form:childtags("field") do
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
64 if field.attr.var == "callback_id" then
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
65 button_name = field:get_child_text("text");
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
66 elseif field.attr.var == "button_name" then
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
67 button_name = field:get_child_text("text");
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
68 elseif field.attr.var ~= "FORM_TYPE" or field:get_child_text("text") ~= button_ns then
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
69 callback_id, button_name, button_value = nil, nil, nil;
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
70 break;
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
71 end
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
72 end
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
73 if callback_id and button_name and button_value then
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
74 json_out.callback_id = callback_id;
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
75 json_out.actions = {
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
76 { type = "button", name = button_name, value = button_value }
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
77 };
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
78 end
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
79 end
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
80
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
81 local stanzaid = stanza:get_child("id");
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
82 if stanzaid and string.sub(stanzaid,1,string.len("webhookbot"))=="webhookbot" then
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
83 json_out["bot_id"] = "webhookbot";
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
84 end
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
85
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
86 json_out = json.encode(json_out)
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
87 local url = routing[from_room];
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
88 module:log("debug", "message from %s in %s to %s", from_nick, from_room, url);
3148
8c7b8b2c3237 mod_slack_webhooks: Add a special webhook target for debugging
Kim Alvefur <zash@zash.se>
parents: 3147
diff changeset
89 if url == "DEBUG" then
8c7b8b2c3237 mod_slack_webhooks: Add a special webhook target for debugging
Kim Alvefur <zash@zash.se>
parents: 3147
diff changeset
90 module:log("debug", "json_out = %s", json_out);
8c7b8b2c3237 mod_slack_webhooks: Add a special webhook target for debugging
Kim Alvefur <zash@zash.se>
parents: 3147
diff changeset
91 return;
8c7b8b2c3237 mod_slack_webhooks: Add a special webhook target for debugging
Kim Alvefur <zash@zash.se>
parents: 3147
diff changeset
92 end
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
93 local headers = {
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
94 ["Content-Type"] = "application/json",
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
95 };
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
96 http.request(url, { method = "POST", body = json_out, headers = headers }, postcallback)
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
97 end
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
98
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
99 module:hook("message/bare", check_message, 10);
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
100
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
101 local function route_post(f)
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
102 return function(event, path)
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
103 local bare_room = jid.join(path, module.host);
3443
284d8c18060e mod_slack_webhooks: Fix use with 0.11+ MUC API
Kim Alvefur <zash@zash.se>
parents: 3148
diff changeset
104 if not get_room_from_jid(bare_room) then
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
105 module:log("warn", "mod_slack_webhook: invalid JID: %s", bare_room);
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
106 return 404;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
107 end
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
108 -- Check secret?
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
109 return f(event, path)
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
110 end
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
111 end
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
112
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
113 local function handle_post(event, path)
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
114 local request = event.request;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
115 local headers = request.headers;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
116
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
117 local body_type = headers.content_type;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
118 local post_body;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
119 if body_type == "application/x-www-form-urlencoded" then
3096
420391fd56e5 mod_slack_webhooks: Fix handling of form-urlencoded
Kim Alvefur <zash@zash.se>
parents: 3095
diff changeset
120 post_body = formdecode(request.body);
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
121 elseif body_type == "application/json" then
3097
c7f4e3987ed0 mod_slack_webhooks: Fix error handling for JSON parsing
Kim Alvefur <zash@zash.se>
parents: 3096
diff changeset
122 post_body = json.decode(request.body)
c7f4e3987ed0 mod_slack_webhooks: Fix error handling for JSON parsing
Kim Alvefur <zash@zash.se>
parents: 3096
diff changeset
123 if not post_body then
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
124 return 420;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
125 end
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
126 else
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
127 return 422;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
128 end
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
129 local bare_room = jid.join(path, module.host);
3443
284d8c18060e mod_slack_webhooks: Fix use with 0.11+ MUC API
Kim Alvefur <zash@zash.se>
parents: 3148
diff changeset
130 local dest_room = get_room_from_jid(bare_room);
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
131 local from_nick = default_from_nick;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
132 if post_body["username"] then
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
133 from_nick = post_body["username"];
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
134 end
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
135 local sender = jid.join(path, module.host, from_nick);
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
136 module:log("debug", "message to %s from %s", bare_room, sender);
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
137 module:log("debug", "body: %s", post_body["text"]);
3095
3a6a48b83b4f mod_slack_webhooks: Reduce scope of variable
Kim Alvefur <zash@zash.se>
parents: 3094
diff changeset
138 local message = msg({ to = bare_room, from = sender, type = "groupchat", id="webhookbot" .. now()},post_body["text"]);
3146
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
139
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
140 if type(post_body["attachments"]) == "table" then -- Buttons?
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
141 -- luacheck: ignore 631
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
142 -- defensive against JSON having whatever data in it, enjoy
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
143
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
144 for _, attachment in ipairs(post_body["attachments"]) do
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
145 if type(attachment) == "table" and type(attachment.actions) == "table" and type(attachment.callback_id) == "string" then
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
146 local buttons = {};
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
147 local button_name;
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
148 for _, action in ipairs(attachment.actions) do
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
149 if type(attachment.text) == "string" then
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
150 buttons.label = attachment.text;
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
151 end
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
152 if type(action) == "table" and action.type == "button" and type(action.name) == "string" and type(action.value) == "string" then
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
153 if not button_name then
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
154 button_name = action.name;
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
155 end
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
156 if button_name == action.name then
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
157 local button = {
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
158 value = action.value;
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
159 };
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
160 if type(action.text) == "string" then
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
161 button.label = action.text;
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
162 end
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
163 table.insert(buttons, button);
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
164 end
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
165 end
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
166 end
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
167 if button_name then
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
168 message:add_direct_child(dataform.new({
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
169 {
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
170 type = "hidden", name = "FORM_TYPE",
3147
82689acd1294 mod_slack_webhooks: Forward info about clicked button to hook
Kim Alvefur <zash@zash.se>
parents: 3146
diff changeset
171 value = button_ns,
3146
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
172 },
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
173 {
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
174 type = "hidden", name = "callback_id",
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
175 value = attachment.callback_id,
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
176 },
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
177 {
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
178 type = "hidden", name = "button_name",
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
179 value = button_name,
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
180 },
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
181 {
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
182 type = "list-single", name = "buttons",
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
183 value = "", -- FIXME util.dataforms can't do options without a value
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
184 options = buttons;
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
185 }
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
186 }):form());
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
187 break;
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
188 end
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
189 end
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
190 end
662423cea3f4 mod_slack_webhooks: Add support for buttons
Kim Alvefur <zash@zash.se>
parents: 3097
diff changeset
191 end
3000
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
192 dest_room:broadcast_message(message, true);
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
193 return 201;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
194 end
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
195
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
196 module:provides("http", {
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
197 default_path = listen_path;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
198 route = {
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
199 ["POST /*"] = route_post(handle_post);
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
200 OPTIONS = function(e)
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
201 local headers = e.response.headers;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
202 headers.allow = "POST";
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
203 headers.accept = "application/x-www-form-urlencoded, application/json";
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
204 return 200;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
205 end;
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
206 }
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
207 });
02fc3b64cbb7 Initial commit of mod_slack_webhooks.
Nathan Whitehorn <nwhitehorn@physics.ucla.edu>
parents:
diff changeset
208