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