Package: pyadaptivecards
PyAdaptiveCardsAuthor adaptive cards in pure pythonIntroductionAdaptive Cardsare 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 adesigneris 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 ...frompyadaptivecards.cardimportAdaptiveCardfrompyadaptivecards.inputsimportText,Numberfrompyadaptivecards.componentsimportTextBlockfrompyadaptivecards.actionsimportSubmitgreeting=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 inWebex Teams...Usage with Webex TeamsBelow is an example how to use pyadaptivecards with Webex Teams.Using raw requestsimportrequestsimportjsonfrompyadaptivecards.cardimportAdaptiveCardfrompyadaptivecards.inputsimportText,Numberfrompyadaptivecards.componentsimportTextBlockfrompyadaptivecards.actionsimportSubmitauth_token="<INSERT_AUTH_TOKEN_HERE>"headers={"Authorization":"Bearer "+auth_token}# Create cardgreeting=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 attachmentattachment={"contentType":"application/vnd.microsoft.card.adaptive","content":card.to_dict()}# Create payload for the webrequestpayload={"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 webexteamssdkThewebexteamssdkprovides 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.frompyadaptivecards.cardimportAdaptiveCardfrompyadaptivecards.inputsimportText,Numberfrompyadaptivecards.componentsimportTextBlockfrompyadaptivecards.actionsimportSubmitfromwebexteamssdkimportWebexTeamsAPIgreeting=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 connectionapi=WebexTeamsAPI()room_id="<INSERT_ROOM_ID_HERE>"# Create a dict that will contain the card as well as some meta informationattachment={"contentType":"application/vnd.microsoft.card.adaptive","content":card.to_dict(),}api.messages.create(roomId=room_id,text="Fallback",attachments=[attachment])FeaturesSupports all components, options and features of adaptive cards version 1.1Create adaptive cards from pure pythonInstallationYou can install PyAdaptiveCards using pip by issuing$pipinstallpyadaptivecardsFor more information on how to use this package please check the project documentation athttps://pyadaptivecards.readthedocs.io.Authors & MaintainersMarcel Neidingermneiding@cisco.comCreditsThe following resources were influential in the creation of this project:This package was created withCookiecutterand a derivative of theaudreyr/cookiecutter-pypackageproject template.LicenseThis project is licensed to you under the terms of theCisco SampleCode License.