Comparison

mod_rest/mod_rest.lua @ 5087:438fbebf74ac

mod_rest: Wrap webhook setup in a function for future reuse The goal is to allow binding a session to a webhook via API to allow e.g. bots to set up handling of incoming messages.
author Kim Alvefur <zash@zash.se>
date Sun, 27 Nov 2022 00:34:00 +0100
parent 5086:dec4b2e31d1c
child 5332:e0b5468aae49
comparison
equal deleted inserted replaced
5086:dec4b2e31d1c 5087:438fbebf74ac
459 ["GET /demo/openapi.yaml"] = demo_handlers.schema; 459 ["GET /demo/openapi.yaml"] = demo_handlers.schema;
460 ["GET /demo/*"] = demo_handlers.resources; 460 ["GET /demo/*"] = demo_handlers.resources;
461 }; 461 };
462 }); 462 });
463 463
464 -- Forward stanzas from XMPP to HTTP and return any reply 464 function new_webhook(rest_url, send_type)
465 local rest_url = module:get_option_string("rest_callback_url", nil);
466 if rest_url then
467 local function get_url() return rest_url; end 465 local function get_url() return rest_url; end
468 if rest_url:find("%b{}") then 466 if rest_url:find("%b{}") then
469 local httputil = require "util.http"; 467 local httputil = require "util.http";
470 local render_url = require"util.interpolation".new("%b{}", httputil.urlencode); 468 local render_url = require"util.interpolation".new("%b{}", httputil.urlencode);
471 function get_url(stanza) 469 function get_url(stanza)
472 local at = stanza.attr; 470 local at = stanza.attr;
473 return render_url(rest_url, { kind = stanza.name, type = at.type, to = at.to, from = at.from }); 471 return render_url(rest_url, { kind = stanza.name, type = at.type, to = at.to, from = at.from });
474 end 472 end
475 end 473 end
476 local send_type = module:get_option_string("rest_callback_content_type", "application/xmpp+xml");
477 if send_type == "json" then 474 if send_type == "json" then
478 send_type = "application/json"; 475 send_type = "application/json";
479 end 476 end
480 477
481 module:set_status("info", "Not yet connected"); 478 module:set_status("info", "Not yet connected");
599 end); 596 end);
600 597
601 return true; 598 return true;
602 end 599 end
603 600
601 return handle_stanza;
602 end
603
604 -- Forward stanzas from XMPP to HTTP and return any reply
605 local rest_url = module:get_option_string("rest_callback_url", nil);
606 if rest_url then
607 local send_type = module:get_option_string("rest_callback_content_type", "application/xmpp+xml");
608
609 local handle_stanza = new_webhook(rest_url, send_type);
610
604 local send_kinds = module:get_option_set("rest_callback_stanzas", { "message", "presence", "iq" }); 611 local send_kinds = module:get_option_set("rest_callback_stanzas", { "message", "presence", "iq" });
605 612
606 local event_presets = { 613 local event_presets = {
607 -- Don't override everything on normal VirtualHosts by default 614 -- Don't override everything on normal VirtualHosts by default
608 ["local"] = { "host" }, 615 ["local"] = { "host" },