用纯python编写自适应卡

pyadaptivecards的Python项目详细描述


PY自适应卡

Author adaptive cards in pure python

PyPiReadTheDocsPyUp


简介

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中看起来像这样。。。在

screenshot of card in 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的项目文档。在

作者和维护者

学分

以下资源对本项目的创建具有影响:

许可证

本项目根据Cisco SampleCode License条款授权给您。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Spring引导应用程序中的java循环视图路径异常   java在PreferenceActivity中显示ProgressDialog   java RESTEasy如何将基本身份验证设置为ClientRequest   使用MongoDB进行java漏斗分析?   java JUnit系统日期   java创建一个集合,每个类有一个对象   java如何在子模块项目中测试Springbean   java Websocket服务器Android   javaitext:如何在新页面上重复表单元格内容?   java一旦spring mvc样板项目建立起来,python/RoR的开发速度会更快吗?   java为什么Maven Tycho不能在工具链中找到正确的JDK。xml?   Java Selenium获取用于单击的元素   java如何使用嵌套的if语句而不是系统阻止程序在blueprint类中运行。出口(0);?   java模板聚合匹配和投影一个没有id的字段   java您的内容必须有一个id属性为“安卓”的TabHost。安卓应用程序的R.id.tabhost错误   JavaEclipseLink 1.2和BatchFetch   java如何更改HttpClient中的超时值?   java在toString方法中将对象转换为特定数据类型   java正在跳过运行for循环