feat(telegram): new Telegram bot alerting integration
Telegram Bot API, 5 commands: send message, send photo, get chat, get updates. Bot-token auth, stdlib-only. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,70 @@
|
|||||||
|
id: telegram
|
||||||
|
name: Telegram
|
||||||
|
version: 1.0.0
|
||||||
|
description: "Telegram Bot API — SOC alerting via a bot: send text messages and photos to a chat, read chat info, and poll updates. Bot-token authentication; stdlib-only, no extra Python dependencies."
|
||||||
|
changelog: "1.0.0 — Initial release: send message, send photo, get chat, get updates."
|
||||||
|
category: notification
|
||||||
|
|
||||||
|
# Per-instance configuration. The bot token is placed in the API path
|
||||||
|
# (https://api.telegram.org/bot<token>/...).
|
||||||
|
config_schema:
|
||||||
|
properties:
|
||||||
|
bot_token:
|
||||||
|
type: string
|
||||||
|
description: "Telegram bot token (from BotFather)"
|
||||||
|
x-soar-sensitive: true
|
||||||
|
default_chat_id:
|
||||||
|
type: string
|
||||||
|
description: "Default chat ID to send to (optional)"
|
||||||
|
required:
|
||||||
|
- bot_token
|
||||||
|
|
||||||
|
commands:
|
||||||
|
- id: send_message
|
||||||
|
name: telegram-send-message
|
||||||
|
description: "Send a text message to a chat."
|
||||||
|
inputs_schema:
|
||||||
|
properties:
|
||||||
|
chat_id: { type: string, description: "Chat ID (overrides the configured default)" }
|
||||||
|
text: { type: string, description: "Message text" }
|
||||||
|
parse_mode: { type: string, description: "Optional: Markdown or HTML" }
|
||||||
|
required: [text]
|
||||||
|
outputs_schema: { properties: {} }
|
||||||
|
- id: send_photo
|
||||||
|
name: telegram-send-photo
|
||||||
|
description: "Send a photo (by URL) to a chat."
|
||||||
|
inputs_schema:
|
||||||
|
properties:
|
||||||
|
chat_id: { type: string, description: "Chat ID (overrides the configured default)" }
|
||||||
|
photo: { type: string, description: "Photo URL" }
|
||||||
|
caption: { type: string, description: "Optional caption" }
|
||||||
|
required: [photo]
|
||||||
|
outputs_schema: { properties: {} }
|
||||||
|
- id: get_chat
|
||||||
|
name: telegram-get-chat
|
||||||
|
description: "Get information about a chat."
|
||||||
|
risk: read
|
||||||
|
inputs_schema:
|
||||||
|
properties:
|
||||||
|
chat_id: { type: string, description: "Chat ID (overrides the configured default)" }
|
||||||
|
required: []
|
||||||
|
outputs_schema: { properties: {} }
|
||||||
|
- id: get_updates
|
||||||
|
name: telegram-get-updates
|
||||||
|
description: "Poll recent updates (incoming messages) for the bot."
|
||||||
|
risk: read
|
||||||
|
inputs_schema:
|
||||||
|
properties:
|
||||||
|
limit: { type: number, description: "Max updates (default 20)" }
|
||||||
|
offset: { type: number, description: "Update offset (optional)" }
|
||||||
|
required: []
|
||||||
|
outputs_schema: { properties: {} }
|
||||||
|
|
||||||
|
- id: test_connection
|
||||||
|
name: telegram-test-connection
|
||||||
|
description: "Verify the bot token (used by the Test button)."
|
||||||
|
risk: read
|
||||||
|
inputs_schema:
|
||||||
|
properties: {}
|
||||||
|
required: []
|
||||||
|
outputs_schema: { properties: {} }
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
import json, os, sys, 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.telegram.org/bot" + str(cfg.get("bot_token", ""))
|
||||||
|
|
||||||
|
|
||||||
|
def call(method_name, cfg, body=None, params=None, http="POST"):
|
||||||
|
url = _base(cfg) + "/" + method_name
|
||||||
|
if params:
|
||||||
|
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
||||||
|
if clean:
|
||||||
|
url += "?" + urllib.parse.urlencode(clean)
|
||||||
|
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||||
|
headers = {"Accept": "application/json"}
|
||||||
|
if data is not None:
|
||||||
|
headers["Content-Type"] = "application/json"
|
||||||
|
req = urllib.request.Request(url, data=data, headers=headers, method=http)
|
||||||
|
with urllib.request.urlopen(req, timeout=60) as r:
|
||||||
|
resp = json.loads(r.read() or "{}")
|
||||||
|
if not resp.get("ok"):
|
||||||
|
raise Exception("Telegram error: " + json.dumps(resp))
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
_chat = lambda cfg, inputs: (inputs.get("chat_id") or cfg.get("default_chat_id"))
|
||||||
|
|
||||||
|
|
||||||
|
def main(cfg, inputs):
|
||||||
|
chat = _chat(cfg, inputs)
|
||||||
|
if not chat:
|
||||||
|
raise Exception("chat_id is required (set a default or pass one)")
|
||||||
|
|
||||||
|
resp = call("getChat", cfg, params={"chat_id": chat}, http="GET")
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
_run(main)
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
import json, os, sys, 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.telegram.org/bot" + str(cfg.get("bot_token", ""))
|
||||||
|
|
||||||
|
|
||||||
|
def call(method_name, cfg, body=None, params=None, http="POST"):
|
||||||
|
url = _base(cfg) + "/" + method_name
|
||||||
|
if params:
|
||||||
|
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
||||||
|
if clean:
|
||||||
|
url += "?" + urllib.parse.urlencode(clean)
|
||||||
|
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||||
|
headers = {"Accept": "application/json"}
|
||||||
|
if data is not None:
|
||||||
|
headers["Content-Type"] = "application/json"
|
||||||
|
req = urllib.request.Request(url, data=data, headers=headers, method=http)
|
||||||
|
with urllib.request.urlopen(req, timeout=60) as r:
|
||||||
|
resp = json.loads(r.read() or "{}")
|
||||||
|
if not resp.get("ok"):
|
||||||
|
raise Exception("Telegram error: " + json.dumps(resp))
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
_chat = lambda cfg, inputs: (inputs.get("chat_id") or cfg.get("default_chat_id"))
|
||||||
|
|
||||||
|
|
||||||
|
def main(cfg, inputs):
|
||||||
|
limit = inputs.get("limit")
|
||||||
|
offset = inputs.get("offset")
|
||||||
|
|
||||||
|
params = {"limit": int(limit or 20)}
|
||||||
|
if offset not in (None, ""):
|
||||||
|
params["offset"] = int(offset)
|
||||||
|
|
||||||
|
resp = call("getUpdates", cfg, params=params, http="GET")
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
_run(main)
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import json, os, sys, 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.telegram.org/bot" + str(cfg.get("bot_token", ""))
|
||||||
|
|
||||||
|
|
||||||
|
def call(method_name, cfg, body=None, params=None, http="POST"):
|
||||||
|
url = _base(cfg) + "/" + method_name
|
||||||
|
if params:
|
||||||
|
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
||||||
|
if clean:
|
||||||
|
url += "?" + urllib.parse.urlencode(clean)
|
||||||
|
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||||
|
headers = {"Accept": "application/json"}
|
||||||
|
if data is not None:
|
||||||
|
headers["Content-Type"] = "application/json"
|
||||||
|
req = urllib.request.Request(url, data=data, headers=headers, method=http)
|
||||||
|
with urllib.request.urlopen(req, timeout=60) as r:
|
||||||
|
resp = json.loads(r.read() or "{}")
|
||||||
|
if not resp.get("ok"):
|
||||||
|
raise Exception("Telegram error: " + json.dumps(resp))
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
_chat = lambda cfg, inputs: (inputs.get("chat_id") or cfg.get("default_chat_id"))
|
||||||
|
|
||||||
|
|
||||||
|
def main(cfg, inputs):
|
||||||
|
chat = _chat(cfg, inputs)
|
||||||
|
if not chat:
|
||||||
|
raise Exception("chat_id is required (set a default or pass one)")
|
||||||
|
|
||||||
|
text = inputs.get("text")
|
||||||
|
if not text:
|
||||||
|
raise Exception("text is required")
|
||||||
|
|
||||||
|
parse_mode = inputs.get("parse_mode")
|
||||||
|
|
||||||
|
body = {"chat_id": chat, "text": text}
|
||||||
|
if parse_mode:
|
||||||
|
body["parse_mode"] = parse_mode
|
||||||
|
|
||||||
|
resp = call("sendMessage", cfg, body=body)
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
_run(main)
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import json, os, sys, 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.telegram.org/bot" + str(cfg.get("bot_token", ""))
|
||||||
|
|
||||||
|
|
||||||
|
def call(method_name, cfg, body=None, params=None, http="POST"):
|
||||||
|
url = _base(cfg) + "/" + method_name
|
||||||
|
if params:
|
||||||
|
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
||||||
|
if clean:
|
||||||
|
url += "?" + urllib.parse.urlencode(clean)
|
||||||
|
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||||
|
headers = {"Accept": "application/json"}
|
||||||
|
if data is not None:
|
||||||
|
headers["Content-Type"] = "application/json"
|
||||||
|
req = urllib.request.Request(url, data=data, headers=headers, method=http)
|
||||||
|
with urllib.request.urlopen(req, timeout=60) as r:
|
||||||
|
resp = json.loads(r.read() or "{}")
|
||||||
|
if not resp.get("ok"):
|
||||||
|
raise Exception("Telegram error: " + json.dumps(resp))
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
_chat = lambda cfg, inputs: (inputs.get("chat_id") or cfg.get("default_chat_id"))
|
||||||
|
|
||||||
|
|
||||||
|
def main(cfg, inputs):
|
||||||
|
chat = _chat(cfg, inputs)
|
||||||
|
if not chat:
|
||||||
|
raise Exception("chat_id is required (set a default or pass one)")
|
||||||
|
|
||||||
|
photo = inputs.get("photo")
|
||||||
|
if not photo:
|
||||||
|
raise Exception("photo is required")
|
||||||
|
|
||||||
|
caption = inputs.get("caption")
|
||||||
|
|
||||||
|
body = {"chat_id": chat, "photo": photo}
|
||||||
|
if caption:
|
||||||
|
body["caption"] = caption
|
||||||
|
|
||||||
|
resp = call("sendPhoto", cfg, body=body)
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
_run(main)
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import json, os, sys, 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.telegram.org/bot" + str(cfg.get("bot_token", ""))
|
||||||
|
|
||||||
|
|
||||||
|
def call(method_name, cfg, body=None, params=None, http="POST"):
|
||||||
|
url = _base(cfg) + "/" + method_name
|
||||||
|
if params:
|
||||||
|
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
||||||
|
if clean:
|
||||||
|
url += "?" + urllib.parse.urlencode(clean)
|
||||||
|
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||||
|
headers = {"Accept": "application/json"}
|
||||||
|
if data is not None:
|
||||||
|
headers["Content-Type"] = "application/json"
|
||||||
|
req = urllib.request.Request(url, data=data, headers=headers, method=http)
|
||||||
|
with urllib.request.urlopen(req, timeout=60) as r:
|
||||||
|
resp = json.loads(r.read() or "{}")
|
||||||
|
if not resp.get("ok"):
|
||||||
|
raise Exception("Telegram error: " + json.dumps(resp))
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
_chat = lambda cfg, inputs: (inputs.get("chat_id") or cfg.get("default_chat_id"))
|
||||||
|
|
||||||
|
|
||||||
|
def main(cfg, inputs):
|
||||||
|
resp = call("getMe", cfg, http="GET")
|
||||||
|
return {"ok": True, "bot": resp.get("result", {}).get("username")}
|
||||||
|
|
||||||
|
|
||||||
|
_run(main)
|
||||||
Reference in New Issue
Block a user