在Microsoft Power Automate Cloud中运行Python脚本

eufgjt7s  于 2024-01-05  发布在  Python
关注(0)|答案(1)|浏览(245)

我使用Power Automate从表单中收集响应并根据响应发送电子邮件。主要目标是使用Python自动化决策以批准或拒绝表单。我知道我可以使用Azure运行Python脚本,但我不确定是否可以从脚本返回值。您有其他建议吗?
我尝试过不使用Python,但使用条件块的desition太复杂了

ca1c2owp

ca1c2owp1#

Power Automate在线浏览器版本没有Python脚本连接器,因此您需要在**Azure Automation accountAzure Functions中运行Python脚本,并在Power Automate流中调用它。
但是
Power Automate Desktop**版本有一个连接器来运行python脚本,参考下面:-

  • Python脚本示例:-*
  1. import json
  2. def process_form(data):
  3. if 'approval_reason' in data:
  4. response = {
  5. 'approval_status': 'Approved',
  6. 'reason': 'Form approved successfully!'
  7. }
  8. else:
  9. response = {
  10. 'approval_status': 'Rejected',
  11. 'reason': 'Form rejected due to missing approval reason.'
  12. }
  13. return response
  14. # Example JSON data representing a form submission
  15. form_data = {
  16. "form_id": "12345",
  17. "submission_date": "2024-01-04",
  18. "name": "John Doe",
  19. "approval_reason": "Valid reason for approval"
  20. }
  21. # Process the form data
  22. result = process_form(form_data)
  23. # Convert the result to JSON and print it
  24. result_json = json.dumps(result, indent=2)
  25. print(result_json)

字符串

Power Automate Desktop:-

x1c 0d1x的数据
并在您的Power Automate浏览器版本中调用此流程。通过将Power Automate Desktop Flow连接到Power Automate浏览器。

Azure自动化:-

你可以在Azure自动化服务中创建一个Python运行手册,如下所示:



并在Power Automate Browser流中调用此Runbook:



根据@Skin的评论,您还可以使用Http触发器编写Azure函数代码来批准和拒绝您的表单,然后在您的自动化工作流中调用此函数URL:

Azure函数:-

我创建了一个示例Azure Http触发器函数,并将其部署到Azure函数应用程序:

Azure Functions HttpTrigger代码:-

  1. import logging
  2. import azure.functions as func
  3. def main(req: func.HttpRequest) -> func.HttpResponse:
  4. logging.info('Python HTTP trigger function processed a request.')
  5. try:
  6. req_body = req.get_json()
  7. except ValueError:
  8. return func.HttpResponse("Invalid JSON received", status_code=400)
  9. if 'approval_reason' in req_body:
  10. response = {
  11. 'approval_status': 'Approved',
  12. 'reason': 'Form approved successfully!'
  13. }
  14. else:
  15. response = {
  16. 'approval_status': 'Rejected',
  17. 'reason': 'Form rejected due to missing approval reason.'
  18. }
  19. return func.HttpResponse(
  20. body=str(response),
  21. status_code=200
  22. )

  • 文章正文:-*
  1. {
  2. "form_id": "12345",
  3. "submission_date": "2024-01-04",
  4. "name": "John Doe",
  5. "approval_reason": "Valid reason for approval"
  6. }

  • 函数app输出:-*

在Power Automate中调用此函数URL如下:-

展开查看全部

相关问题