Skip to content

Instantly share code, notes, and snippets.

@shinkou
Last active April 24, 2024 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinkou/20fbd280f586594510be9b91812b51b4 to your computer and use it in GitHub Desktop.
Save shinkou/20fbd280f586594510be9b91812b51b4 to your computer and use it in GitHub Desktop.
MS Teams Poster
#!/usr/bin/env python3
# vim: fileencoding=utf-8 ff=unix
import argparse, json, requests, sys
def message(
webhook, msg
, links=None, facts=None, subtitle=None, title=None, themecolor=None
):
payload = {
"@type": "MessageCard"
, "@context": "http://schema.org/extensions"
, "summary": "Summary"
, "sections": []
}
if isinstance(themecolor, str) and len(themecolor):
payload["themeColor"] = themecolor
if isinstance(msg, str) and len(msg):
payload_section = {"text": msg}
if isinstance(title, str) and len(title):
payload_section["activityTitle"] = title
if isinstance(subtitle, str) and len(subtitle):
payload_section["activitySubtitle"] = subtitle
if facts:
payload_section["facts"] = [{"name": k, "value": v} for k, v in facts.items()]
payload["sections"].append(payload_section)
if links:
payload["potentialAction"] = []
for n, lnk in links.items():
payload["potentialAction"].append({
"@type": "OpenUri"
, "name": n
, "targets": [{
"os": "default"
, "uri": lnk
}]
})
response = requests.post(
webhook
, json=payload
, headers={"content-type": "application/json"}
)
if 200 != response.status_code:
print(response.text, file=sys.stderr)
print("\nPayload:")
print(json.dumps(payload, indent=4))
def getargs():
parser = argparse.ArgumentParser(description="MS Teams Poster")
parser.add_argument("--webhook", type=str, help="Webhook URL")
parser.add_argument("--themecolor", type=str, help="Theme Color")
parser.add_argument("--title", type=str, help="Title")
parser.add_argument("--subtitle", type=str, help="Subtitle")
parser.add_argument(
"--facts"
, type=json.loads
, help="Facts in JSON format '{\"name\": \"value\"}'"
)
parser.add_argument(
"--links"
, type=json.loads
, help="Links in JSON format '{\"text\": \"URL\"}'"
)
parser.add_argument("messages", nargs="+")
return parser.parse_args()
def main():
args = getargs()
prms = dict(vars(args))
webhook = args.webhook
del prms["webhook"]
del prms["messages"]
for msg in args.messages:
message(webhook, msg, **prms)
if '__main__' == __name__:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment