格式化邮件并发送给Microsoft团队。
pymsteams的Python项目详细描述
PymsTeams
python包装器库,用于向microsoft团队webhook发送请求。 Microsoft将这些消息称为连接器卡。只能使用主连接器卡发送邮件,或者可以在邮件中包含其他部分。
此库为Microsoft团队使用Webhook连接器。请访问以下Microsoft文档链接以获取有关如何获取频道正确URL的说明:https://dev.outlook.com/Connectors/GetStarted#creating-messages-through-office-365-connectors-in-microsoft-teams
请参阅Microsoft文档以获取最新的屏幕截图。 https://dev.outlook.com/connectors/reference
安装
使用pip安装:
pip install pymsteams
用法
创建connectorcard消息
这是pymsteams最简单的实现。它将向团队webhook url发送一条消息,其中包含纯文本。
importpymsteams# You must create the connectorcard object with the Microsoft Webhook URLmyTeamsMessage=pymsteams.connectorcard("<Microsoft Webhook URL>")# Add text to the message.myTeamsMessage.text("this is my text")# send the message.myTeamsMessage.send()
卡的可选格式化方法
添加标题
myTeamsMessage.title("This is my message title")
添加链接按钮
myTeamsMessage.addLinkButton("This is the button Text","https://github.com/rveachkc/pymsteams/")
更改URL
如果您需要将同一条消息发送到多个房间,这将非常有用。
myTeamsMessage.newhookurl("<My New URL>")
预览对象
这是一个简单的打印命令,用于在发送之前查看连接器卡消息对象。
myTeamsMessage.printme()
将节添加到连接器卡消息
创建节并添加各种格式元素
# create the sectionmyMessageSection=pymsteams.cardsection()# Section TitlemyMessageSection.title("Section title")# Activity ElementsmyMessageSection.activityTitle("my activity title")myMessageSection.activitySubtitle("my activity subtitle")myMessageSection.activityImage("http://i.imgur.com/c4jt321l.png")myMessageSection.activityText("This is my activity Text")# Facts are key value pairs displayed in a list.myMessageSection.addFact("this","is fine")myMessageSection.addFact("this is","also fine")# Section TextmyMessageSection.text("This is my section text")# Section ImagesmyMessageSection.addImage("http://i.imgur.com/c4jt321l.png",ititle="This Is Fine")# Add your section to the connector card object before sendingmyTeamsMessage.addSection(myMessageSection)
您也可以将多个部分添加到连接器卡消息中。
# Create Section 1Section1=pymsteams.cardsection()Section1.text("My First Section")# Create Section 2Section2=pymsteams.cardsection()Section2.text("My First Section")# Add both Sections to the main card objectmyTeamsMessage.addSection(Section1)myTeamsMessage.addSection(Section2)# Then send the cardmyTeamsMessage.send()
向连接器卡消息添加潜在操作
创建用户可以在ms团队中与之交互的操作 要了解有关可以使用哪些操作的更多信息,请访问https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/connectors/connectors-using#setting-up-a-custom-incoming-webhook
myTeamsMessage = pymsteams.connectorcard("<Microsoft Webhook URL>")
myTeamsPotentialAction1 = pymsteams.potentialaction(_name = "Add a comment")
myTeamsPotentialAction1.addInput("TextInput","comment","Add a comment here",False)
myTeamsPotentialAction1.addAction("HttpPost","Add Comment","https://...")
myTeamsPotentialAction2 = pymsteams.potentialaction(_name = "Set due date")
myTeamsPotentialAction2.addInput("DateInput","dueDate","Enter due date")
myTeamsPotentialAction2.addAction("HttpPost","save","https://...")
myTeamsPotentialAction3 = pymsteams.potentialaction(_name = "Change Status")
myTeamsPotentialAction3.choices.addChoices("In progress","0")
myTeamsPotentialAction3.choices.addChoices("Active","1")
myTeamsPotentialAction3.addInput("MultichoiceInput","list","Select a status",False)
myTeamsPotentialAction3.addAction("HttpPost","Save","https://...")
myTeamsMessage.addPotentialAction(myTeamsPotentialAction1)
myTeamsMessage.addPotentialAction(myTeamsPotentialAction2)
myTeamsMessage.addPotentialAction(myTeamsPotentialAction3)
myTeamsMessage.summary("Test Message")
myTeamsMessage.send()
请使用github问题报告任何错误或请求增强功能。
例外情况
如果对microsoft teams webhook服务的调用失败,将抛出TeamsWebhookException
。
测试
为了使用pytest在您的环境中进行测试,请将环境变量MS_TEAMS_WEBHOOK
设置为您要使用的Microsoft Team Webhook URL。
然后,从repo的根目录安装需求并运行pytest。
pip install -r dev-requirements.txt pytest
这将发送两条ms teams消息,描述它们的格式。手动验证消息是否按预期传递。
证书验证
在某些情况下,必须使用自定义ca包。这可以通过设置verify参数在类初始化时设置。
importpymsteams# set custom ca bundlemsg=pymsteams.connectorcard("<Microsoft Webhook URL>",verify="/path/to/file")# disable CA validationmsg=pymsteams.connectorcard("<Microsoft Webhook URL>",verify=False)
设置为自定义ca捆绑包的路径,或设置为false以禁用。
可以参考请求文档以获取完整的详细信息:https://2.python-requests.org/en/master/user/advanced/#ssl-cert-verification