Skip to main content

Format messages and post to Microsoft Teams through Microsoft Power Automate WorkFlow.

Project description

pymsteams

Python Wrapper Library to send requests to Microsoft Teams via Microsoft Power Automate workflows.

I have created this library because the Microsoft Teams Webhook is being retired. Retirement of Office 365 connectors within Microsoft Teams

Overview

This library allows sending messages with rich formatting to Microsoft Teams by utilizing Power Automate workflows. Messages can include simple text, titles, link buttons, and more. You can also use asynchronous operations to send messages in parallel.

Installation

Install with pip:

pip install pymsteams-workflow

Usage

Creating ConnectorCard Messages

Below is a basic example demonstrating how to send a message using a Power Automate workflow URL:

from pymsteams import connectorcard

# You must create the connectorcard object with the Power Automate URL
myTeamsMessage = connectorcard("<Power Automate URL>")

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

# Send the message
myTeamsMessage.send()

Asynchronous ConnectorCard Messages

You can send messages asynchronously using async_connectorcard. This is useful when sending multiple messages or performing other asynchronous tasks.

import asyncio
from pymsteams import async_connectorcard

loop = asyncio.get_event_loop()

# The async_connectorcard object is used for asynchronous operations
myTeamsMessage = async_connectorcard("<Power Automate URL>")

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

# Send the message asynchronously
loop.run_until_complete(myTeamsMessage.send())

Optional Formatting Methods

Add a Title

You can add a title to the message, which will be displayed prominently:

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

Add a Link Button

Add a link button to your message to redirect users to a specific URL:

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

Example: Sending a Message with a Title and Link Button

Here's a complete example of sending a message with both a title and a link button:

from your_module import connectorcard  # Replace 'your_module' with your module name

webhook_url = "<Power Automate URL>"

# Create the connector card
card = connectorcard(webhook_url)
card.title("Important Notification")
card.text("Please review the latest updates.")
card.addLinkButton("Review Updates", "https://example.com/updates")

# Send the message
card.send()

Exception Handling

If the call to the Power Automate service fails, a TeamsWebhookException will be thrown. Ensure you handle this exception in your code:

try:
    card.send()
except TeamsWebhookException as e:
    print(f"Failed to send message: {e}")

Testing

In order to test in your environment with pytest, set the environment variable MS_TEAMS_WORKFLOW to the Power Automate workflow 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_WORKFLOW=<PowerAutomateURL>
export MS_TEAMS_WORKFLOW
pytest --cov=./your_module --cov-report=term-missing --cov-branch  # Replace 'your_module' with your module name

Docs

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymsteams_workflow-1.0.5.tar.gz (9.4 kB view details)

Uploaded Source

File details

Details for the file pymsteams_workflow-1.0.5.tar.gz.

File metadata

  • Download URL: pymsteams_workflow-1.0.5.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.1

File hashes

Hashes for pymsteams_workflow-1.0.5.tar.gz
Algorithm Hash digest
SHA256 663899dbe1870c07a77ffe80f190d98400d30b61cdd01983e5a3b779244e3df0
MD5 bb3e2856bb16a02485153d99c0da08c3
BLAKE2b-256 f7d19839493e0622fbacd017436d88582b6f1362dadcfc48771f9b3ec4751d90

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page