Python Wrapper Library to send requests to Microsoft Teams Webhooks. Microsoft refers to these messages as Connector Cards. A message can be sent with only the main Connector Card, or additional sections can be included into the message.
This library uses Webhook Connectors for Microsoft Teams. Please visit the following Microsoft Documentation link for instructions on how to obtain the correct url for your Channel: https://dev.outlook.com/Connectors/GetStarted#creating-messages-through-office-365-connectors-in-microsoft-teams
Please refer to the Microsoft Documentation for the most up to date screenshots. https://dev.outlook.com/connectors/reference
Install with pip:
bash
pip install pymsteams
Install with async capabilities (python 3.6+):
bash
pip install pymsteams[async]
At time of writing, the latest release supported by Python 2 is Version 0.1.16
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
myTeamsMessage = pymsteams.connectorcard("
myTeamsMessage.text("this is my text")
myTeamsMessage.send() ```
```python import asyncio import pymsteams
loop = asyncio.get_event_loop()
myTeamsMessage = pymsteams.async_connectorcard("
myTeamsMessage.text("This is my message")
loop.run_until_complete(myTeamsMessage.send()) ```
Please visit the python asyncio documentation for more info on using asyncio and the event loop: https://docs.python.org/3/library/asyncio-eventloop.html
python
myTeamsMessage.title("This is my message title")
python
myTeamsMessage.addLinkButton("This is the button Text", "https://github.com/rveachkc/pymsteams/")
This is useful in the event you need to post the same message to multiple rooms.
python
myTeamsMessage.newhookurl("<My New URL>")
This sets the theme color of the card. The parameter is expected to be a hex color code without the hash or the string red.
python
myTeamsMessage.color("<Hex Color Code>")
This is a simple print command to view your connector card message object before sending.
python
myTeamsMessage.printme()
To create a section and add various formatting elements ```python
myMessageSection = pymsteams.cardsection()
myMessageSection.title("Section title")
myMessageSection.activityTitle("my activity title") myMessageSection.activitySubtitle("my activity subtitle") myMessageSection.activityImage("http://i.imgur.com/c4jt321l.png") myMessageSection.activityText("This is my activity Text")
myMessageSection.addFact("this", "is fine") myMessageSection.addFact("this is", "also fine")
myMessageSection.text("This is my section text")
myMessageSection.addImage("http://i.imgur.com/c4jt321l.png", ititle="This Is Fine")
myTeamsMessage.addSection(myMessageSection)
You may also add multiple sections to a connector card message as well.
python
Section1 = pymsteams.cardsection() Section1.text("My First Section")
Section2 = pymsteams.cardsection() Section2.text("My Second Section")
myTeamsMessage.addSection(Section1) myTeamsMessage.addSection(Section2)
myTeamsMessage.send() ```
To create a actions on which the user can interect with in MS Teams To find out more information on what actions can be used, please visit https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/connectors/connectors-using#setting-up-a-custom-incoming-webhook
```python
myTeamsMessage = pymsteams.connectorcard("
myTeamsPotentialAction1 = pymsteams.potentialaction(_name = "Add a comment") myTeamsPotentialAction1.addInput("TextInput","comment","Add a comment here",False) myTeamsPotentialAction1.addAction("HttpPost","Add Comment","https://..."")
myTeamsPotentialAction2 = pymsteams.potentialaction(_name = "Set due date") myTeamsPotentialAction2.addInput("DateInput","dueDate","Enter due date") myTeamsPotentialAction2.addAction("HttpPost","save","https://...")
myTeamsPotentialAction3 = pymsteams.potentialaction(_name = "Change Status") myTeamsPotentialAction3.choices.addChoices("In progress","0") myTeamsPotentialAction3.choices.addChoices("Active","1") myTeamsPotentialAction3.addInput("MultichoiceInput","list","Select a status",False) myTeamsPotentialAction3.addAction("HttpPost","Save","https://...")
myTeamsMessage.addPotentialAction(myTeamsPotentialAction1) myTeamsMessage.addPotentialAction(myTeamsPotentialAction2) myTeamsMessage.addPotentialAction(myTeamsPotentialAction3)
myTeamsMessage.summary("Test Message")
myTeamsMessage.send() ```
```python
myTeamsMessage = pymsteams.connectorcard("
myTeamsPotentialAction1 = pymsteams.potentialaction(_name = "Add a comment")
myTeamsPotentialAction1.addInput("TextInput","comment","Add a comment here",False)
myTeamsPotentialAction1.addAction("HttpPost","Add Comment","https://...", "{{comment.value}}") myTeamsMessage.addPotentialAction(myTeamsPotentialAction1)
myTeamsMessage.summary("Test Message")
myTeamsMessage.send()
```
Please use Github issues to report any bugs or request enhancements.
This module is really just a nice wrapper pointed at the Microsoft API. To help troubleshoot missing messages, the requests response content is saved to the connectorcard class attribute last_http_response
.
To get the last http status code: ```python import pymsteams
myTeamsMessage = pymsteams.connectorcard("
last_status_code = myTeamsMessage.last_http_response.status_code ```
More info on the Response Content is available in the requests documentation, link.
If the call to the Microsoft Teams webhook service fails, a TeamsWebhookException
will be thrown.
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
MS_TEAMS_WEBHOOK=MicrosoftWebhookURL
export MS_TEAMS_WEBHOOK
pytest --cov=./pymsteams --cov-report=term-missing --cov-branch
This will send two MS Teams messages describing how they are formatted. Manually validate that the message comes through as expected.
In some situations, a custom CA bundle must be used. This can be set on class initialization, by setting the verify parameter.
```python import pymsteams
msg = pymsteams.connectorcard("
msg = pymsteams.connectorcard("
Set to either the path of a custom CA bundle or False to disable.
The requests documentation can be referenced for full details: https://2.python-requests.org/en/master/user/advanced/#ssl-cert-verification
Describe the bug Sometimes "==" is not sent via the hook.
To Reproduce
my_teams_message = pymsteams.connectorcard(...)
my_teams_message.text("0==0 1==1")
my_teams_message.send()
Expected behavior A message containing "0==0 1==1"
Screenshots
Additional context pymsteams 0.2.2
Describe the bug I used to send HTML tables on my automatic msg, this feature stopped working (probably teams changed someting)
To Reproduce
Just try to post this msg, the table won't appear
"<h2>My table:</h2> <table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n <th></th>\n <th>0</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>1</td>\n </tr>\n <tr>\n <th>1</th>\n <td>2</td>\n </tr>\n <tr>\n <th>2</th>\n <td>3</td>\n </tr>\n </tbody>\n</table> 123"
Expected behavior See the table in the card
Dear rveach maintainer,
i would like to submit this issue on android phone that the adaptive card is not rendered well. That is, from android when you click on the buttons (of a potential action) the context menu will appears, such as the calendar or the multi-selection of some values. What does not appear instead is the enter key of the choice you want to make. The pictures perhaps explain better. This is a comparison table, where:
For the image see the follow link: https://i.imgur.com/VvAaH5S.png
The code I am using is the following.
```python import pymsteams
myTeamsMessage = pymsteams.connectorcard("MY-CURRENT-WEBHOOK")
myTeamsMessage.text("this is my text") myTeamsMessage.title("This is my message title")
myMessageSection = pymsteams.cardsection()
myTeamsPotentialAction1 = pymsteams.potentialaction(_name = "Add a comment") myTeamsPotentialAction1.addInput("TextInput","comment","Add a comment here",False) myTeamsPotentialAction1.addAction("HttpPost","Add Comment","https://www.lg.com")
myTeamsPotentialAction2 = pymsteams.potentialaction(_name = "Set due date") myTeamsPotentialAction2.addInput("DateInput","dueDate","Enter due date") myTeamsPotentialAction2.addAction("HttpPost","save","https://www.virustotal.com")
myTeamsPotentialAction3 = pymsteams.potentialaction(_name = "Change Status") myTeamsPotentialAction3.choices.addChoices("In progress","0") myTeamsPotentialAction3.choices.addChoices("Active","1") myTeamsPotentialAction3.addInput("MultichoiceInput","list","Select a status",False) myTeamsPotentialAction3.addAction("HttpPost","Save","https://nvd.nist.gov/")
myTeamsMessage.addPotentialAction(myTeamsPotentialAction1) myTeamsMessage.addPotentialAction(myTeamsPotentialAction2) myTeamsMessage.addPotentialAction(myTeamsPotentialAction3)
myTeamsMessage.summary("Test Message")
myTeamsMessage.send() ```
I can confirm that is probably something with the pymsteams and not related to android, as i have other Teams channel where on android , the button on potential action works as expected. May i can link some info about that
Regards
Adding the startGroup for a section
Hi, I don't know if it's possible. I could not find anything about this in Microsoft Docs either. It is about "HttpPost". Is there also the possibility for an authentication there, similar to "requests" with basic auth. via "auth=("user", "pw")?
Thanks in advance!
This library uses a the webhook api and connector card format. When requesting new functionality, please reference the API to ensure this is possible.
Can we please have StartGroup field under the section as given in https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference#section-fields
Thanks
Full Changelog: https://github.com/rveachkc/pymsteams/compare/0.2.1...0.2.2
Version 0.2.1 corrects the python setup information, preventing incompatible code from being installed on Python 2.
Full Changelog: https://github.com/rveachkc/pymsteams/compare/0.2.0...0.2.1
Full Changelog: https://github.com/rveachkc/pymsteams/compare/0.1.16...0.2.0
Full Changelog: https://github.com/rveachkc/pymsteams/compare/0.1.15...0.1.16
Resolves #75, connectorcard.send()
will now raise a TeamsWebhookException
when a sent message is too large for the API to handle. Thanks @stmoody.