Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/CiscoSE/pyadaptivecards
Author adaptive cards in native python
https://github.com/CiscoSE/pyadaptivecards
Last synced: about 1 month ago
JSON representation
Author adaptive cards in native python
- Host: GitHub
- URL: https://github.com/CiscoSE/pyadaptivecards
- Owner: CiscoSE
- License: other
- Created: 2019-10-30T18:15:17.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-26T20:48:02.000Z (almost 2 years ago)
- Last Synced: 2024-11-06T17:08:07.238Z (about 1 month ago)
- Language: Python
- Size: 82 KB
- Stars: 30
- Watchers: 7
- Forks: 9
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.rst
- Contributing: CONTRIBUTING.rst
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.rst
Awesome Lists containing this project
- awesome-adaptive-cards - Python - Author adaptive cards in pure python. (Uncategorized / Uncategorized)
README
# PyAdaptiveCards
*Author adaptive cards in pure python*
[![PyPi](https://img.shields.io/pypi/v/pyadaptivecards.svg)](https://pypi.python.org/pypi/pyadaptivecards)
[![ReadTheDocs](https://readthedocs.org/projects/pyadaptivecards/badge/?version=latest)](https://pyadaptivecards.readthedocs.io/en/latest/?badge=latest)
[![PyUp](https://pyup.io/repos/github/CiscoSE/pyadaptivecards/shield.svg)](https://pyup.io/repos/github/CiscoSE/pyadaptivecards/)---
## Introduction
[Adaptive Cards](https://adaptivecards.io/) 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](https://adaptivecards.io/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 ...
```python
from pyadaptivecards.card import AdaptiveCard
from pyadaptivecards.inputs import Text, Number
from pyadaptivecards.components import TextBlock
from pyadaptivecards.actions import Submitgreeting = 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 ...
```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](https://teams.webex.com) ...
![screenshot of card in webex teams](cards_sample.png)
## Usage with Webex Teams
Below is an example how to use pyadaptivecards with Webex Teams.
### Using raw requests
```python
import requests
import jsonfrom pyadaptivecards.card import AdaptiveCard
from pyadaptivecards.inputs import Text, Number
from pyadaptivecards.components import TextBlock
from pyadaptivecards.actions import Submitauth_token = ""
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": "",
"attachments" : [attachment],
"text": "Fallback Text"
}response = requests.post("https://api.ciscospark.com/v1/messages", headers=headers, data=payload)
```### Using the webexteamssdk
The [webexteamssdk](https://github.com/CiscoDevNet/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.```python
from pyadaptivecards.card import AdaptiveCard
from pyadaptivecards.inputs import Text, Number
from pyadaptivecards.components import TextBlock
from pyadaptivecards.actions import Submitfrom 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 = ""
# 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
```bash
$ pip install pyadaptivecards
```For more information on how to use this package please check the project documentation at https://pyadaptivecards.readthedocs.io.
## Authors & Maintainers
- Marcel Neidinger
## Credits
The following resources were influential in the creation of this project:
- This package was created with [Cookiecutter](https://github.com/audreyr/cookiecutter) and a derivative of the[audreyr/cookiecutter-pypackage](https://github.com/audreyr/cookiecutter-pypackage) project template.
## License
This project is licensed to you under the terms of the [Cisco SampleCode License](./LICENSE).