Skip to main content

pymsteams

Important: Ongoing Support and Request for Maintainers

Looking for new maintainers. Please see https://github.com/rveachkc/pymsteams/issues/151

PyPI version

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

Installation

Install with pip:

pip install pymsteams

Install with async capabilities (python 3.6+):

pip install pymsteams[async]

Python 2 Installation

At time of writing, the latest release supported by Python 2 is Version 0.1.16

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.

import pymsteams

# You must create the connectorcard object with the Microsoft Webhook URL
myTeamsMessage = pymsteams.connectorcard("<Microsoft Webhook URL>")

# Add text to the message.
myTeamsMessage.text("this is my text")

# send the message.
myTeamsMessage.send()

Creating CreatorCard Messages to send via async loop

import asyncio
import pymsteams

loop = asyncio.get_event_loop()

# the async_connectorcard object is used instead of the normal one.
myTeamsMessage = pymsteams.async_connectorcard("<Microsoft Webhook URL>")

# all formatting for the message should be the same
myTeamsMessage.text("This is my message")

# to send the message, pass to the event loop
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

Optional Formatting Methods for Cards

Add a title

myTeamsMessage.title("This is my message title")

Add a link button

myTeamsMessage.addLinkButton("This is the button Text", "https://github.com/rveachkc/pymsteams/")

Change URL

This is useful in the event you need to post the same message to multiple rooms.

myTeamsMessage.newhookurl("<My New URL>")

Set Color Theme

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.

myTeamsMessage.color("<Hex Color Code>")

Preview your object

This is a simple print command to view your connector card message object before sending.

myTeamsMessage.printme()

Adding sections to the Connector Card Message

To create a section and add various formatting elements

# create the section
myMessageSection = pymsteams.cardsection()

# Section Title
myMessageSection.title("Section title")

# Activity Elements
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")

# Facts are key value pairs displayed in a list.
myMessageSection.addFact("this", "is fine")
myMessageSection.addFact("this is", "also fine")

# Section Text
myMessageSection.text("This is my section text")

# Section Images
myMessageSection.addImage("http://i.imgur.com/c4jt321l.png", ititle="This Is Fine")

# Add your section to the connector card object before sending
myTeamsMessage.addSection(myMessageSection)

You may also add multiple sections to a connector card message as well.

# Create Section 1
Section1 = pymsteams.cardsection()
Section1.text("My First Section")

# Create Section 2
Section2 = pymsteams.cardsection()
Section2.text("My Second Section")

# Add both Sections to the main card object
myTeamsMessage.addSection(Section1)
myTeamsMessage.addSection(Section2)

# Then send the card
myTeamsMessage.send()

Adding potential actions to the Connector Card Message

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

myTeamsMessage = pymsteams.connectorcard("<Microsoft Webhook URL>")

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()

Adding HTTP Post to potential actions in the Connector Card Message

myTeamsMessage = pymsteams.connectorcard("<Microsoft Webhook URL>")

myTeamsPotentialAction1 = pymsteams.potentialaction(_name = "Add a comment")
# You can add a TextInput to your potential action like below - Please note the 2nd argment below as the id name
myTeamsPotentialAction1.addInput("TextInput","comment","Add a comment here",False)
# we use the 2nd argument above as the id name to parse the values into the body post like below.
myTeamsPotentialAction1.addAction("HttpPost","Add Comment","https://...", "{{comment.value}}")
myTeamsMessage.addPotentialAction(myTeamsPotentialAction1)


myTeamsMessage.summary("Test Message")

myTeamsMessage.send()

# Notes:
# If you post anything via teams, you will get some Javascript encoding happening via the post - For example:
# Posting this:  {"name":"john", "comment" : "nice"}
# Output will be:  b'{\\u0022name\\u0022:\\u0022john\\u0022, \\u0022comment\\u0022 : \\u0022nice\\u0022}'
# i solved this issue by decoding unicode escape for a custom rest backend.

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

Troubleshooting HTTP response

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:

import pymsteams

myTeamsMessage = pymsteams.connectorcard("<Microsoft Webhook URL>")
myTeamsMessage.text("this is my text")
myTeamsMessage.send()

last_status_code = myTeamsMessage.last_http_response.status_code

More info on the Response Content is available in the requests documentation, link.

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.

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

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.

Certificate Validation

In some situations, a custom CA bundle must be used. This can be set on class initialization, by setting the verify parameter.

import pymsteams

# set custom ca bundle
msg = pymsteams.connectorcard("<Microsoft Webhook URL>", verify="/path/to/file")

# disable CA validation
msg = pymsteams.connectorcard("<Microsoft Webhook URL>", verify=False)

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

Release files for pymsteams 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymsteams 0.2.5
File Size Uploaded
pymsteams-0.2.5.tar.gz 88.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pymsteams 0.2.5
File Interpreter ABI Platform
pymsteams-0.2.5-py3-none-any.whl Python 3 none any Details

Total release size: 103.6 kB

Release files / pymsteams-0.2.5.tar.gz

Download URL pymsteams-0.2.5.tar.gz
Size 88.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9f76ca3a3de17b49ce3c5c314ee0e88b8bd2be78fc66f693ade1b7cabf23af70
BLAKE2b-256 checksum
How to use checksums
d9f58b9b9572d4f582e5a3a135110c07218cd43ad6d067a986576d0467bf6251
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 7, 2025.

Transparency log

Release files / pymsteams-0.2.5-py3-none-any.whl

Download URL pymsteams-0.2.5-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bda78f36c4a59baa10fa21928980349a841b03c78dc7d6020f230aea4aeab2b7
BLAKE2b-256 checksum
How to use checksums
77552f83baa2a9d1eada20f41dcced4d4fb7ba14d864b160be812786802e39c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 7, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.5 This release

2 release files

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.16

1 release file

0.1.15

1 release file

0.1.14

1 release file

0.1.13

1 release file

0.1.12

1 release file

0.1.11

1 release file

0.1.10

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page