Skip to content

Instantly share code, notes, and snippets.

@voidbar
Last active January 30, 2022 08:41
Show Gist options
  • Save voidbar/6ec7d0cc84fe9587c7cec5ba8acc161e to your computer and use it in GitHub Desktop.
Save voidbar/6ec7d0cc84fe9587c7cec5ba8acc161e to your computer and use it in GitHub Desktop.
Send a Teams message with proper markdown support using python
import logging
import requests
import sys
from os import path
# https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook
TEAMS_WEBHOOK = "{YOUR_WEBHOOK_LINK}"
LOG_FILE = path.join(path.dirname(__file__), "log.txt")
logger = logging.getLogger(__name__)
fh = logging.FileHandler(LOG_FILE)
fh.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(fh)
logger.setLevel(logging.DEBUG)
def get_payload(title: str, markdown_message: str) -> dict:
return {
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"summary": title,
"sections": [{
"activityTitle": title,
"markdown": True,
"text": markdown_message,
}]
}
def main():
logger.info("Sending teams message!")
res = requests.post(TEAMS_WEBHOOK, json=get_payload("💰 This Is The Title! 💰", "💸 This is the content and it can be `markdown`ed💸"))
if not res.ok:
logger.error(
f"Bad response from webhook! Error code: {res.status_code}. Text: {res.text}")
sys.exit(1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment