Skip to content

Commit

Permalink
Merge pull request #27 from rveachkc/develop
Browse files Browse the repository at this point in the history
Version 0.1.9 - Python 2.7 support and http timeouts
  • Loading branch information
rveachkc authored Dec 21, 2018
2 parents f5c7a4d + f1caccf commit 18dd368
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 22 deletions.
33 changes: 26 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "requirements.txt" }}
- v1-dependencies-{{ checksum "dev-requirements.txt" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

Expand All @@ -24,12 +24,12 @@ jobs:
command: |
python3 -m venv venv
. venv/bin/activate
pip install -r requirements.txt
pip install -r dev-requirements.txt
- save_cache:
paths:
- ./venv
key: v1-dependencies-{{ checksum "requirements.txt" }}
key: v1-dependencies-{{ checksum "dev-requirements.txt" }}

# run tests!
# this example uses Django's built-in test-runner
Expand All @@ -45,8 +45,23 @@ jobs:
#- store_artifacts:
# path: test-reports
# destination: test-reports


test:
docker:
- image: circleci/python:3-stretch
working_directory: ~/repo

steps:
- checkout

- restore_cache:
key: v1-dependencies-{{ checksum "dev-requirements.txt" }}
- run:
name: verify git tag vs version
command: |
python3 -m venv venv
. venv/bin/activate
pytest
deploy:
docker:
- image: circleci/python:3-stretch
Expand All @@ -56,7 +71,7 @@ jobs:
- checkout

- restore_cache:
key: v1-dependencies-{{ checksum "requirements.txt" }}
key: v1-dependencies-{{ checksum "dev-requirements.txt" }}

- run:
name: verify git tag vs version
Expand Down Expand Up @@ -86,12 +101,16 @@ jobs:
workflows:
version: 2
build_and_deploy:
build_test_deploy:
jobs:
- build:
filters:
tags:
only: /.*/
- test:
filters:
tags:
only: /.*/
- deploy:
context: PYPI
requires:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ ENV/
.ropeproject
*.pypirc

rvtest.py
rvtest.py
.vscode/settings.json
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2018 Ryan Veach

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
41 changes: 34 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ This library uses Webhook Connectors for Microsoft Teams. Please visit the foll
Please refer to the Microsoft Documentation for the most up to date screenshots.
https://dev.outlook.com/connectors/reference

## Creating ConnectorCard Messages
## Installation

Install with pip:

```bash
pip install pymsteams
```

## Usage

### Creating ConnectorCard Messages
This is the simplest implementation of pymsteams. It will send a message to the teams webhook url with plain text in the message.
```python
import pymsteams
Expand All @@ -23,31 +33,31 @@ myTeamsMessage.text("this is my text")
myTeamsMessage.send()
```

## Optional Formatting Methods for Cards
### Optional Formatting Methods for Cards

### Add a title
#### Add a title
```python
myTeamsMessage.title("This is my message title")
```

### Add a link button
#### Add a link button
```python
myTeamsMessage.addLinkButton("This is the button Text", "https://github.com/rveachkc/pymsteams/")
```

### Change URL
#### Change URL
This is useful in the event you need to post the same message to multiple rooms.
```python
myTeamsMessage.newhookurl("<My New URL>")
```

### Preview your object
#### Preview your object
This is a simple print command to view your connector card message object before sending.
```python
myTeamsMessage.printme()
```

## Adding sections to the Connector Card Message
### Adding sections to the Connector Card Message
To create a section and add various formatting elements
```python
# create the section
Expand Down Expand Up @@ -94,3 +104,20 @@ myTeamsMessage.send()
```

Please use Github issues to report any bugs or request enhancements.

## 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.

Then, from the root of the repo, install the requirements and run pytest.

```bash
pip install -r dev-requirements.txt
pytest
```

This will send two MS Teams messages describing how they are formatted. Manually validate that the message comes through as expected.

**Master** [![CircleCI](https://circleci.com/gh/rveachkc/pymsteams/tree/master.svg?style=shield)](https://circleci.com/gh/rveachkc/pymsteams/tree/master)

**Develop** [![CircleCI](https://circleci.com/gh/rveachkc/pymsteams/tree/develop.svg?style=shield)](https://circleci.com/gh/rveachkc/pymsteams/tree/develop)
22 changes: 22 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
atomicwrites==1.2.1
attrs==18.2.0
bleach==3.0.2
certifi==2018.4.16
chardet==3.0.4
colorama==0.4.1
docutils==0.14
idna==2.7
more-itertools==4.3.0
pkginfo==1.4.2
pluggy==0.8.0
py==1.7.0
Pygments==2.3.1
pytest==4.0.2
readme-renderer==24.0
requests==2.21.0
requests-toolbelt==0.8.0
six==1.12.0
tqdm==4.28.1
twine==1.12.1
urllib3==1.23
webencodings==0.5.1
11 changes: 9 additions & 2 deletions pymsteams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,25 @@ def printme(self):

def send(self):
headers = {"Content-Type":"application/json"}
r = requests.post(self.hookurl, json=self.payload, headers=headers, proxies=self.proxies)
r = requests.post(
self.hookurl,
json=self.payload,
headers=headers,
proxies=self.proxies,
timeout=self.http_timeout,
)

if r.status_code == requests.codes.ok:
return True
else:
print(r.text)
return False

def __init__(self, hookurl, http_proxy=None, https_proxy=None):
def __init__(self, hookurl, http_proxy=None, https_proxy=None, http_timeout=60):
self.payload = {}
self.hookurl = hookurl
self.proxies = {}
self.http_timeout = http_timeout

if http_proxy:
self.proxies['http'] = http_proxy
Expand Down
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

7 changes: 4 additions & 3 deletions 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.8"
VERSION = "0.1.9"

def readme():
""" print long description """
Expand Down Expand Up @@ -47,9 +47,10 @@ def run(self):
"Operating System :: OS Independent",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Internet",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Topic :: Office/Business",
"Topic :: Office/Business :: Groupware",
],
Expand All @@ -58,7 +59,7 @@ def run(self):
install_requires=[
'requests>=2.20.0',
],
python_requires='>=3',
python_requires='>=2.7',
cmdclass={
'verify': VerifyVersionCommand,
}
Expand Down
Binary file added test/desk_toys_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/desk_toys_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions test/test_webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import sys

# add scripts to the path
sys.path.append(
os.path.split(
os.path.dirname(
os.path.abspath(__file__)
)
)[0]
)
import pymsteams

def test_env_webhook_url():
"""
Test that we have the webhook set as an environment variable.
This is testing our test environment, not the code.
"""
webhook_url = os.getenv("MS_TEAMS_WEBHOOK", None)
assert webhook_url
assert webhook_url.find("https") == 0

def test_send_message():
"""
This sends a simple text message with a title and link button.
"""

teams_message = pymsteams.connectorcard(os.getenv("MS_TEAMS_WEBHOOK"))
teams_message.text("This is a simple text message.")
teams_message.title("Simple Message Title")
teams_message.addLinkButton("Go to the Repo", "https://github.com/rveachkc/pymsteams")
teams_message.send()


def test_send_sectioned_message():
"""
This sends a message with sections.
"""

# start the message
teams_message = pymsteams.connectorcard(os.getenv("MS_TEAMS_WEBHOOK"))
teams_message.text("This is the main title.")
teams_message.title("Sectioned Message Title")

# section 1
section_1 = pymsteams.cardsection()
section_1.title("Section 1 title")
section_1.activityTitle("my activity title")
section_1.activitySubtitle("my activity subtitle")
section_1.activityImage("https://raw.githubusercontent.com/rveachkc/pymsteams/develop/test/desk_toys_1.jpg")
section_1.activityText("This is my activity Text. You should see an activity image, activity title, activity subtitle, and this text (of course).")
section_1.addFact("Fact", "this is fine")
section_1.addFact("Fact", "this is also fine")
section_1.text("This is my section 1 text. This section has an activity above and two facts below.")
teams_message.addSection(section_1)

# section 2
section_2 = pymsteams.cardsection()
section_2.text("This is section 2. You should see an image. This section does not have facts or a title.")
section_2.addImage("https://raw.githubusercontent.com/rveachkc/pymsteams/develop/test/desk_toys_2.jpg", ititle="Pew Pew Pew")
teams_message.addSection(section_2)


# send
teams_message.send()

0 comments on commit 18dd368

Please sign in to comment.