Skip to main content

PyAdaptiveCards

Author adaptive cards in pure python

PyPi ReadTheDocs PyUp


Introduction

Adaptive Cards are a great way to extend your bot interactions. However, writing the JSON required to specify the card layout by hand can be cumbersome and error prone. And while using a designer is a good way to manually create cards this does not cover cards that are generated by code. PyAdaptiveCards allows you to author cards in native python without ever touching the underlying json.

A code sample says more then a thousand words so the following code snippet ...

from pyadaptivecards.card import AdaptiveCard
from pyadaptivecards.inputs import Text, Number
from pyadaptivecards.components import TextBlock
from pyadaptivecards.actions import Submit

greeting = TextBlock("Hey hello there! I am a adaptive card")
first_name = Text('first_name', placeholder="First Name")
age = Number('age', placeholder="Age")

submit = Submit(title="Send me!")

card = AdaptiveCard(body=[greeting, first_name, age], actions=[submit])
card_json = card.to_json(pretty=True)
print(card_json)

... produces this json ...

{
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "actions": [
        {
            "title": "Send me!",
            "type": "Action.Submit"
        }
    ],
    "body": [
        {
            "text": "Hey hello there! I am a adaptive card",
            "type": "TextBlock"
        },
        {
            "id": "first_name",
            "placeholder": "First name",
            "type": "Input.Text"
        },
        {
            "id": "age",
            "placeholder": "Age",
            "type": "Input.Number"
        }
    ],
    "type": "AdaptiveCard",
    "version": "1.1"
}

... which looks like this in Webex Teams ...

screenshot of card in webex teams

Usage with Webex Teams

Below is an example how to use pyadaptivecards with Webex Teams.

Using raw requests

import requests 
import json

from pyadaptivecards.card import AdaptiveCard
from pyadaptivecards.inputs import Text, Number
from pyadaptivecards.components import TextBlock
from pyadaptivecards.actions import Submit

auth_token = "<INSERT_AUTH_TOKEN_HERE>"
headers = {
    "Authorization": "Bearer " + auth_token
}

# Create card
greeting = TextBlock("Hey hello there! I am a adaptive card")
first_name = Text('first_name', placeholder="First Name")
age = Number('age', placeholder="Age")

submit = Submit(title="Send me!")

card = AdaptiveCard(body=[greeting, first_name, age], actions=[submit])

# Create attachment
attachment = {
    "contentType": "application/vnd.microsoft.card.adaptive",
    "content": card.to_dict()
}

# Create payload for the webrequest
payload = {
    "roomId": "<INSERT_YOUR_ROOM_HERE>",
    "attachments" : [attachment],
    "text": "Fallback Text"
}

response = requests.post("https://api.ciscospark.com/v1/messages", headers=headers, data=payload)

Using the webexteamssdk

The webexteamssdk provides a great wrapper around the Webex Teams API that can be used to interact with the API in native python. The following example shows how to use pyadaptivecards with the newly implemented attachments option.

from pyadaptivecards.card import AdaptiveCard
from pyadaptivecards.inputs import Text, Number
from pyadaptivecards.components import TextBlock
from pyadaptivecards.actions import Submit

from webexteamssdk import WebexTeamsAPI

greeting = TextBlock("Hey hello there! I am a adaptive card")
first_name = Text('first_name', placeholder="First Name")
age = Number('age', placeholder="Age")

submit = Submit(title="Send me!")

card = AdaptiveCard(body=[greeting, first_name, age], actions=[submit])

# Create a webex teams api connection
api = WebexTeamsAPI()
room_id = "<INSERT_ROOM_ID_HERE>"
# Create a dict that will contain the card as well as some meta information
attachment = {
    "contentType": "application/vnd.microsoft.card.adaptive",
    "content": card.to_dict(),
}
api.messages.create(roomId=room_id, text="Fallback", attachments=[attachment])

Features

  • Supports all components, options and features of adaptive cards version 1.1
  • Create adaptive cards from pure python

Installation

You can install PyAdaptiveCards using pip by issuing

$ pip install pyadaptivecards

For more information on how to use this package please check the project documentation at https://pyadaptivecards.readthedocs.io.

Authors & Maintainers

Credits

The following resources were influential in the creation of this project:

License

This project is licensed to you under the terms of the Cisco SampleCode License.

Release files for pyadaptivecards 0.1.1

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

Source distribution (sdist)

Source distribution for pyadaptivecards 0.1.1
File Size Uploaded
pyadaptivecards-0.1.1.tar.gz 59.9 kB Details

Release files / pyadaptivecards-0.1.1.tar.gz

Download URL pyadaptivecards-0.1.1.tar.gz
Size 59.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0f3bd37415b891b0e3b0849e9023d8a537c5c9a7597f2b1817bf9780449e7279
BLAKE2b-256 checksum
How to use checksums
82cc68c4cd9beb53a8222c2d1728871e393dc4a037516d4c2bf3df88034d8ef0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.1

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file

0.1.0

2 release files

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