Skip to content

Commit

Permalink
raise exception on failed webhook call
Browse files Browse the repository at this point in the history
  • Loading branch information
rveachkc committed Jul 2, 2019
1 parent fde1796 commit 62e9881
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions pymsteams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

import requests

class TeamsWebhookException(Exception):
"""custom exception for failed webhook call"""
pass

class cardsection:

def title(self, stitle):
Expand Down Expand Up @@ -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 = {}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down
12 changes: 12 additions & 0 deletions test/test_webhook.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import pytest

# add scripts to the path
sys.path.append(
Expand All @@ -9,6 +10,7 @@
)
)[0]
)

import pymsteams

def test_env_webhook_url():
Expand Down Expand Up @@ -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"

0 comments on commit 62e9881

Please sign in to comment.