diff --git a/integrations/twilio/manifest.yaml b/integrations/twilio/manifest.yaml new file mode 100644 index 0000000..2d8b3db --- /dev/null +++ b/integrations/twilio/manifest.yaml @@ -0,0 +1,73 @@ +id: twilio +name: Twilio +version: 1.0.0 +description: "Twilio (REST API) — SOC out-of-band alerting: send SMS messages, place voice calls, and read message status/history. HTTP Basic authentication; stdlib-only, no extra Python dependencies." +changelog: "1.0.0 — Initial release: send SMS, make call, get/list messages." +category: notification + +# Per-instance configuration. HTTP Basic auth with the Account SID + Auth Token. +config_schema: + properties: + account_sid: + type: string + description: "Twilio Account SID" + auth_token: + type: string + description: "Twilio Auth Token" + x-soar-sensitive: true + from_number: + type: string + description: "Default sender number (E.164, e.g. +15551234567)" + required: + - account_sid + - auth_token + +commands: + - id: send_sms + name: twilio-send-sms + description: "Send an SMS message." + inputs_schema: + properties: + to: { type: string, description: "Recipient number (E.164)" } + body: { type: string, description: "Message text" } + from_number: { type: string, description: "Sender number (overrides the configured default)" } + required: [to, body] + outputs_schema: { properties: {} } + - id: make_call + name: twilio-make-call + description: "Place a voice call that plays TwiML." + inputs_schema: + properties: + to: { type: string, description: "Recipient number (E.164)" } + from_number: { type: string, description: "Caller number (overrides the configured default)" } + twiml: { type: string, description: "TwiML to execute (provide this or url)" } + url: { type: string, description: "URL returning TwiML (provide this or twiml)" } + required: [to] + outputs_schema: { properties: {} } + - id: get_message + name: twilio-get-message + description: "Get a message's status and details by SID." + risk: read + inputs_schema: + properties: + message_sid: { type: string, description: "Message SID" } + required: [message_sid] + outputs_schema: { properties: {} } + - id: list_messages + name: twilio-list-messages + description: "List recent messages." + risk: read + inputs_schema: + properties: + page_size: { type: number, description: "Max messages (default 20)" } + required: [] + outputs_schema: { properties: {} } + + - id: test_connection + name: twilio-test-connection + description: "Verify connectivity and credentials (used by the Test button)." + risk: read + inputs_schema: + properties: {} + required: [] + outputs_schema: { properties: {} } diff --git a/integrations/twilio/scripts/get_message.py b/integrations/twilio/scripts/get_message.py new file mode 100644 index 0000000..390472c --- /dev/null +++ b/integrations/twilio/scripts/get_message.py @@ -0,0 +1,60 @@ +import json, os, sys, base64, urllib.parse, urllib.request, urllib.error + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def _base(cfg): + return "https://api.twilio.com/2010-04-01/Accounts/" + str(cfg.get("account_sid", "")) + + +def _auth(cfg): + raw = str(cfg.get("account_sid", "")) + ":" + str(cfg.get("auth_token", "")) + return "Basic " + base64.b64encode(raw.encode("utf-8")).decode("utf-8") + + +def request(method, path, cfg, form=None, params=None): + url = _base(cfg) + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = None + headers = {"Authorization": _auth(cfg), "Accept": "application/json"} + if form is not None: + data = urllib.parse.urlencode({k: v for k, v in form.items() if v not in (None, "")}).encode("utf-8") + headers["Content-Type"] = "application/x-www-form-urlencoded" + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +q = lambda v: urllib.parse.quote(str(v), safe="") +_from = lambda cfg, inputs: (inputs.get("from_number") or cfg.get("from_number")) + + +def main(cfg, inputs): + message_sid = inputs.get("message_sid") + if not message_sid: + raise Exception("message_sid is required") + return request("GET", "/Messages/" + q(message_sid) + ".json", cfg) + + +_run(main) diff --git a/integrations/twilio/scripts/list_messages.py b/integrations/twilio/scripts/list_messages.py new file mode 100644 index 0000000..2543b48 --- /dev/null +++ b/integrations/twilio/scripts/list_messages.py @@ -0,0 +1,58 @@ +import json, os, sys, base64, urllib.parse, urllib.request, urllib.error + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def _base(cfg): + return "https://api.twilio.com/2010-04-01/Accounts/" + str(cfg.get("account_sid", "")) + + +def _auth(cfg): + raw = str(cfg.get("account_sid", "")) + ":" + str(cfg.get("auth_token", "")) + return "Basic " + base64.b64encode(raw.encode("utf-8")).decode("utf-8") + + +def request(method, path, cfg, form=None, params=None): + url = _base(cfg) + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = None + headers = {"Authorization": _auth(cfg), "Accept": "application/json"} + if form is not None: + data = urllib.parse.urlencode({k: v for k, v in form.items() if v not in (None, "")}).encode("utf-8") + headers["Content-Type"] = "application/x-www-form-urlencoded" + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +q = lambda v: urllib.parse.quote(str(v), safe="") +_from = lambda cfg, inputs: (inputs.get("from_number") or cfg.get("from_number")) + + +def main(cfg, inputs): + page_size = inputs.get("page_size") + return request("GET", "/Messages.json", cfg, params={"PageSize": int(page_size or 20)}) + + +_run(main) diff --git a/integrations/twilio/scripts/make_call.py b/integrations/twilio/scripts/make_call.py new file mode 100644 index 0000000..25058cb --- /dev/null +++ b/integrations/twilio/scripts/make_call.py @@ -0,0 +1,72 @@ +import json, os, sys, base64, urllib.parse, urllib.request, urllib.error + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def _base(cfg): + return "https://api.twilio.com/2010-04-01/Accounts/" + str(cfg.get("account_sid", "")) + + +def _auth(cfg): + raw = str(cfg.get("account_sid", "")) + ":" + str(cfg.get("auth_token", "")) + return "Basic " + base64.b64encode(raw.encode("utf-8")).decode("utf-8") + + +def request(method, path, cfg, form=None, params=None): + url = _base(cfg) + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = None + headers = {"Authorization": _auth(cfg), "Accept": "application/json"} + if form is not None: + data = urllib.parse.urlencode({k: v for k, v in form.items() if v not in (None, "")}).encode("utf-8") + headers["Content-Type"] = "application/x-www-form-urlencoded" + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +q = lambda v: urllib.parse.quote(str(v), safe="") +_from = lambda cfg, inputs: (inputs.get("from_number") or cfg.get("from_number")) + + +def main(cfg, inputs): + to = inputs.get("to") + if not to: + raise Exception("to is required") + frm = _from(cfg, inputs) + if not frm: + raise Exception("from_number is required (set a default or pass one)") + twiml = inputs.get("twiml") + url = inputs.get("url") + if not twiml and not url: + raise Exception("twiml or url is required") + form = {"From": frm, "To": to} + if twiml: + form["Twiml"] = twiml + elif url: + form["Url"] = url + return request("POST", "/Calls.json", cfg, form=form) + + +_run(main) diff --git a/integrations/twilio/scripts/send_sms.py b/integrations/twilio/scripts/send_sms.py new file mode 100644 index 0000000..5341e9d --- /dev/null +++ b/integrations/twilio/scripts/send_sms.py @@ -0,0 +1,66 @@ +import json, os, sys, base64, urllib.parse, urllib.request, urllib.error + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def _base(cfg): + return "https://api.twilio.com/2010-04-01/Accounts/" + str(cfg.get("account_sid", "")) + + +def _auth(cfg): + raw = str(cfg.get("account_sid", "")) + ":" + str(cfg.get("auth_token", "")) + return "Basic " + base64.b64encode(raw.encode("utf-8")).decode("utf-8") + + +def request(method, path, cfg, form=None, params=None): + url = _base(cfg) + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = None + headers = {"Authorization": _auth(cfg), "Accept": "application/json"} + if form is not None: + data = urllib.parse.urlencode({k: v for k, v in form.items() if v not in (None, "")}).encode("utf-8") + headers["Content-Type"] = "application/x-www-form-urlencoded" + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +q = lambda v: urllib.parse.quote(str(v), safe="") +_from = lambda cfg, inputs: (inputs.get("from_number") or cfg.get("from_number")) + + +def main(cfg, inputs): + to = inputs.get("to") + if not to: + raise Exception("to is required") + body = inputs.get("body") + if not body: + raise Exception("body is required") + frm = _from(cfg, inputs) + if not frm: + raise Exception("from_number is required (set a default or pass one)") + return request("POST", "/Messages.json", cfg, form={"From": frm, "To": to, "Body": body}) + + +_run(main) diff --git a/integrations/twilio/scripts/test_connection.py b/integrations/twilio/scripts/test_connection.py new file mode 100644 index 0000000..96f45ab --- /dev/null +++ b/integrations/twilio/scripts/test_connection.py @@ -0,0 +1,58 @@ +import json, os, sys, base64, urllib.parse, urllib.request, urllib.error + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def _base(cfg): + return "https://api.twilio.com/2010-04-01/Accounts/" + str(cfg.get("account_sid", "")) + + +def _auth(cfg): + raw = str(cfg.get("account_sid", "")) + ":" + str(cfg.get("auth_token", "")) + return "Basic " + base64.b64encode(raw.encode("utf-8")).decode("utf-8") + + +def request(method, path, cfg, form=None, params=None): + url = _base(cfg) + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = None + headers = {"Authorization": _auth(cfg), "Accept": "application/json"} + if form is not None: + data = urllib.parse.urlencode({k: v for k, v in form.items() if v not in (None, "")}).encode("utf-8") + headers["Content-Type"] = "application/x-www-form-urlencoded" + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +q = lambda v: urllib.parse.quote(str(v), safe="") +_from = lambda cfg, inputs: (inputs.get("from_number") or cfg.get("from_number")) + + +def main(cfg, inputs): + request("GET", ".json", cfg) + return {"ok": True} + + +_run(main)