用纯python编写自适应卡
pyadaptivecards的Python项目详细描述
PY自适应卡
Author adaptive cards in pure python
简介
Adaptive Cards是扩展bot交互的好方法。但是,手工编写指定卡片布局所需的JSON会很麻烦,而且容易出错。虽然使用designer是手动创建卡片的好方法,但它不包括由代码生成的卡片。PyAdaptiveCards允许您在不接触底层json的情况下用原生python编写卡片。在
一个代码示例显示了超过1000个单词,因此下面的代码片段。。。在
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)
。。。生成此json。。。在
^{pr2}$。。。在Webex Teams中看起来像这样。。。在
使用Webex团队
下面是一个如何在Webex团队中使用pyadaptivecards的示例。在
使用原始请求
importrequestsimportjsonfrompyadaptivecards.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)
使用webexteamssdk
webexteamssdk为webexteamsapi提供了一个很好的包装器,可用于与原生python中的API进行交互。下面的示例演示如何在新实现的attachments选项中使用pyadaptivecards。在
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])
特点
- 支持1.1版自适应卡的所有组件、选项和功能
- 从纯python创建自适应卡
安装
您可以使用pip安装PyAdaptiveCards,方法是
$ pip install pyadaptivecards
有关如何使用此包的详细信息,请查看位于https://pyadaptivecards.readthedocs.io的项目文档。在
作者和维护者
- 马塞尔·内丁格尔mneiding@cisco.com
学分
以下资源对本项目的创建具有影响:
- 此包是用Cookiecutter和audreyr/cookiecutter-pypackage项目模板的派生项创建的。在
许可证
本项目根据Cisco SampleCode License条款授权给您。在
- 项目
标签: