Mercurial > prosody-modules
comparison mod_slack_webhooks/mod_slack_webhooks.lua @ 3146:662423cea3f4
mod_slack_webhooks: Add support for buttons
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 03 Jun 2018 02:02:51 +0200 |
| parents | c7f4e3987ed0 |
| children | 82689acd1294 |
comparison
equal
deleted
inserted
replaced
| 3145:a3afab416271 | 3146:662423cea3f4 |
|---|---|
| 11 local jid = require "util.jid"; | 11 local jid = require "util.jid"; |
| 12 local now = require "util.datetime".datetime; | 12 local now = require "util.datetime".datetime; |
| 13 local json = require "util.json" | 13 local json = require "util.json" |
| 14 local formdecode = require "net.http".formdecode; | 14 local formdecode = require "net.http".formdecode; |
| 15 local http = require "net.http"; | 15 local http = require "net.http"; |
| 16 local dataform = require "util.dataforms"; | |
| 16 | 17 |
| 17 local function get_room_from_jid(mod_muc, room_jid) | 18 local function get_room_from_jid(mod_muc, room_jid) |
| 18 if mod_muc.get_room_from_jid then | 19 if mod_muc.get_room_from_jid then |
| 19 return mod_muc.get_room_from_jid(room_jid); | 20 return mod_muc.get_room_from_jid(room_jid); |
| 20 elseif mod_muc.rooms then | 21 elseif mod_muc.rooms then |
| 112 end | 113 end |
| 113 local sender = jid.join(path, module.host, from_nick); | 114 local sender = jid.join(path, module.host, from_nick); |
| 114 module:log("debug", "message to %s from %s", bare_room, sender); | 115 module:log("debug", "message to %s from %s", bare_room, sender); |
| 115 module:log("debug", "body: %s", post_body["text"]); | 116 module:log("debug", "body: %s", post_body["text"]); |
| 116 local message = msg({ to = bare_room, from = sender, type = "groupchat", id="webhookbot" .. now()},post_body["text"]); | 117 local message = msg({ to = bare_room, from = sender, type = "groupchat", id="webhookbot" .. now()},post_body["text"]); |
| 118 | |
| 119 if type(post_body["attachments"]) == "table" then -- Buttons? | |
| 120 -- luacheck: ignore 631 | |
| 121 -- defensive against JSON having whatever data in it, enjoy | |
| 122 | |
| 123 for _, attachment in ipairs(post_body["attachments"]) do | |
| 124 if type(attachment) == "table" and type(attachment.actions) == "table" and type(attachment.callback_id) == "string" then | |
| 125 local buttons = {}; | |
| 126 local button_name; | |
| 127 for _, action in ipairs(attachment.actions) do | |
| 128 if type(attachment.text) == "string" then | |
| 129 buttons.label = attachment.text; | |
| 130 end | |
| 131 if type(action) == "table" and action.type == "button" and type(action.name) == "string" and type(action.value) == "string" then | |
| 132 if not button_name then | |
| 133 button_name = action.name; | |
| 134 end | |
| 135 if button_name == action.name then | |
| 136 local button = { | |
| 137 value = action.value; | |
| 138 }; | |
| 139 if type(action.text) == "string" then | |
| 140 button.label = action.text; | |
| 141 end | |
| 142 table.insert(buttons, button); | |
| 143 end | |
| 144 end | |
| 145 end | |
| 146 if button_name then | |
| 147 message:add_direct_child(dataform.new({ | |
| 148 { | |
| 149 type = "hidden", name = "FORM_TYPE", | |
| 150 value = "xmpp:prosody.im/community/mod_slack_webhooks#buttons", | |
| 151 }, | |
| 152 { | |
| 153 type = "hidden", name = "callback_id", | |
| 154 value = attachment.callback_id, | |
| 155 }, | |
| 156 { | |
| 157 type = "hidden", name = "button_name", | |
| 158 value = button_name, | |
| 159 }, | |
| 160 { | |
| 161 type = "list-single", name = "buttons", | |
| 162 value = "", -- FIXME util.dataforms can't do options without a value | |
| 163 options = buttons; | |
| 164 } | |
| 165 }):form()); | |
| 166 break; | |
| 167 end | |
| 168 end | |
| 169 end | |
| 170 end | |
| 117 dest_room:broadcast_message(message, true); | 171 dest_room:broadcast_message(message, true); |
| 118 return 201; | 172 return 201; |
| 119 end | 173 end |
| 120 | 174 |
| 121 module:provides("http", { | 175 module:provides("http", { |
