-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteams_send.py
More file actions
executable file
·48 lines (33 loc) · 1.49 KB
/
teams_send.py
File metadata and controls
executable file
·48 lines (33 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
"""Send a simple message to Microsoft Teams via incoming webhook."""
from __future__ import annotations
import argparse
import json
import os
from typing import Optional
import requests
DEFAULT_WEBHOOK_URL = ""
TEAMS_WEBHOOK_ENV = "TEAMS_WEBHOOK_URL"
def send_teams_message(message: str, webhook_url: Optional[str] = None) -> None:
"""Send a plain text message to Microsoft Teams via the incoming webhook."""
resolved_webhook = webhook_url or os.getenv(TEAMS_WEBHOOK_ENV) or DEFAULT_WEBHOOK_URL
if not resolved_webhook:
raise ValueError(
"No Teams webhook URL configured. Set TEAMS_WEBHOOK_URL or pass --webhook."
)
payload = {"message": message}
headers = {"Content-Type": "application/json"}
response = requests.post(resolved_webhook, headers=headers, data=json.dumps(payload), timeout=30)
if response.status_code >= 400:
raise RuntimeError(
f"Failed to post alert update to Teams (status {response.status_code}): {response.text}"
)
def main() -> None:
parser = argparse.ArgumentParser(description="Send a single message to Microsoft Teams")
parser.add_argument("text", nargs="?", default="Test message from the Teams helper")
parser.add_argument("--webhook", help="Teams webhook URL; defaults to TEAMS_WEBHOOK_URL env")
args = parser.parse_args()
send_teams_message(args.text, webhook_url=args.webhook)
print("Message sent to Teams.")
if __name__ == "__main__":
main()