Skip to main content

Python library for Microsoft Teams webhooks

Project description

Py Lib Template

A Python library template with modern packaging setup for PyPI publishing.

Create your own libraries in the lib folder, using the py_lib_template example, you can create your own library by changing the name of the folder and update the folder name in the pyproject.toml file. Follow the steps below for more details

Installation

pip install py-lib-template

Usage

Microsoft Teams Webhook

Send adaptive card messages to Microsoft Teams channels via incoming webhooks.

Basic Usage - Single Webhook URL

from py_lib_template import TeamsWebhook

# Initialize with a single webhook URL
webhook = TeamsWebhook(
    webhook_url="https://teams.webhook.office.com/webhookb2/...",
    environment="production",
    responsible_party="DevOps Team"
)

# Send a message
webhook.send(
    title="Deployment Successful",
    message={"version": "1.2.3", "deployed_at": "2024-01-15 10:30:00"},
    data_source="My Application",
    status="Success"
)

Separate Webhooks for Success and Errors

from py_lib_template import TeamsWebhook

# Initialize with separate webhooks
webhook = TeamsWebhook(
    success_webhook_url="https://teams.webhook.office.com/webhookb2/success...",
    error_webhook_url="https://teams.webhook.office.com/webhookb2/alerts...",
    environment="production",
    responsible_party="DevOps Team"
)

# Success message goes to success webhook
webhook.send(
    title="Build Passed",
    message="All tests passed successfully",
    data_source="CI/CD Pipeline",
    status="Success"
)

# Error message goes to error webhook
webhook.send(
    title="Build Failed",
    message={"error": "Unit tests failed", "failed_tests": 5},
    data_source="CI/CD Pipeline",
    status="Error"
)

Convenience Methods

# Send success message
webhook.send_success(
    title="Operation Complete",
    message={"records_processed": 1000},
    data_source="Data Pipeline"
)

# Send error message
webhook.send_error(
    title="Processing Failed",
    message={"error": "Database connection lost"},
    data_source="Data Pipeline"
)

# Send warning message
webhook.send_warning(
    title="Disk Space Low",
    message={"disk_usage": "85%", "free_space": "2GB"},
    data_source="Server Monitor"
)

# Send info message
webhook.send_info(
    title="Scheduled Maintenance",
    message="System will be down for maintenance at 2:00 AM",
    data_source="System Admin"
)

Custom Adaptive Card Payload

# Define your own adaptive card payload
custom_payload = {
    "type": "message",
    "attachments": [
        {
            "contentType": "application/vnd.microsoft.card.adaptive",
            "content": {
                "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
                "type": "AdaptiveCard",
                "version": "1.2",
                "body": [
                    {
                        "type": "TextBlock",
                        "text": "**Custom Alert**",
                        "style": "heading"
                    },
                    {
                        "type": "TextBlock",
                        "text": "This is a custom adaptive card"
                    }
                ]
            }
        }
    ]
}

# Send with custom payload
webhook.send(
    title="Custom Alert",
    message="Message content",
    data_source="Custom App",
    status="Info",
    payload=custom_payload  # Custom payload will be used instead of default
)

Additional Custom Fields

# Add custom fields to the message
webhook.send(
    title="Build Status",
    message={"build_id": "12345"},
    data_source="CI/CD Pipeline",
    status="Success",
    custom_fields={
        "Branch": "main",
        "Commit": "abc123",
        "Duration": "5m 30s"
    }
)

Math Utilities

Basic Addition

from py_lib_template import add

# Add two numbers
result = add(1, 2)
print(result)  # 3

# Works with floats too
result = add(1.5, 2.5)
print(result)  # 4.0

Adding Multiple Numbers

from py_lib_template import add_many

# Add multiple numbers
result = add_many(1, 2, 3, 4, 5)
print(result.value)     # 15
print(result.operands)  # [1, 2, 3, 4, 5]

Development

Setup

# Create virtual environment
python -m venv venv

# Activate (Windows)
venv\Scripts\activate

# Activate (Unix/Mac)
source venv/bin/activate

# Install in development mode
pip install -e ".[dev]"

Pre-Build

  • In pyproject.toml. Update the version and name of the library that you wish to publish.

Build

# Build the package
python build_lib.py

# Or manually
python -m build --outdir pypi_publish

Test

pytest

Lint & Format

# Format code
black lib tests

# Lint
ruff lib tests

# Type check
mypy lib

Publish

# Build first
python build_lib.py

# Publish to PyPI (requires credentials)
cd pypi_publish
python -m twine upload *

Project Structure

lib/
├── py_lib_template/       # Library source code
│   ├── __init__.py        # Package entry point
│   ├── math_utils.py      # Math utilities module
│   └── teams_webhook.py   # Teams webhook module
├── pypi_publish/          # Build artifacts (wheel, sdist)
├── tests/                 # Test files
├── pyproject.toml         # Package configuration
└── README.md

License

MIT

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

rajang_pywebhook-0.1.0.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rajang_pywebhook-0.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file rajang_pywebhook-0.1.0.tar.gz.

File metadata

  • Download URL: rajang_pywebhook-0.1.0.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for rajang_pywebhook-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cf3ddeb68cf17b7b6f5ce56ee129925dbce7873ed9dcf3a8b4d70e9fc60375c6
MD5 196024e4e68defa36c375c447dab822c
BLAKE2b-256 fa167bfa4365e81870f484f438502e2e02b81128309b40269cbae2987a8d89d0

See more details on using hashes here.

File details

Details for the file rajang_pywebhook-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for rajang_pywebhook-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 004e08577a07d7e9cde12aab03a4b3e7a7f2299b9881123e77f4860d2c9df4d7
MD5 e05e848c1e87cd247ff39ac76b399ceb
BLAKE2b-256 f58fdea0f5d95bbfb961d4c3053a896b41ed13933608f9f577fa90ff34d95482

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