diff --git a/README.md b/README.md index 8397752..7269931 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,10 @@ myTeamsMessage.send() Please use Github issues to report any bugs or request enhancements. +## Exceptions + +If the call to the Microsoft Teams webhook service fails, a `TeamsWebhookException` will be thrown. + ## Testing In order to test in your environment with pytest, set the environment variable `MS_TEAMS_WEBHOOK` to the Microsoft Teams Webhook url you would like to use. diff --git a/pymsteams/__init__.py b/pymsteams/__init__.py index ac70197..77842e6 100644 --- a/pymsteams/__init__.py +++ b/pymsteams/__init__.py @@ -5,6 +5,10 @@ import requests +class TeamsWebhookException(Exception): + """custom exception for failed webhook call""" + pass + class cardsection: def title(self, stitle): @@ -196,8 +200,7 @@ def send(self): if r.status_code == requests.codes.ok: return True else: - print(r.text) - return False + raise TeamsWebhookException(r.text) def __init__(self, hookurl, http_proxy=None, https_proxy=None, http_timeout=60): self.payload = {} diff --git a/setup.py b/setup.py index b5e7c35..c1ef440 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ from setuptools import setup from setuptools.command.install import install -VERSION = "0.1.10" +VERSION = "0.1.11" def readme(): """ print long description """ diff --git a/test/test_webhook.py b/test/test_webhook.py index c585371..6e1cc3f 100644 --- a/test/test_webhook.py +++ b/test/test_webhook.py @@ -1,5 +1,6 @@ import os import sys +import pytest # add scripts to the path sys.path.append( @@ -9,6 +10,7 @@ ) )[0] ) + import pymsteams def test_env_webhook_url(): @@ -93,3 +95,13 @@ def test_send_potential_action(): myTeamsMessage.addPotentialAction(myTeamsPotentialAction3) myTeamsMessage.summary("Message Summary") myTeamsMessage.send() + +def test_bad_webhook_call(): + with pytest.raises(pymsteams.TeamsWebhookException): + #myTeamsMessage = pymsteams.connectorcard(os.getenv("MS_TEAMS_WEBHOOK")) + myTeamsMessage = pymsteams.connectorcard("https://httpstat.us/500") + myTeamsMessage.text("This is a simple text message.") + myTeamsMessage.title("Simple Message Title") + myTeamsMessage.send() + #myTeamsMessage.hookurl = "https://httpstat.us/500" + \ No newline at end of file