在Microsoft Power Automate Cloud中运行Python脚本

eufgjt7s  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(151)

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

ca1c2owp

ca1c2owp1#

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

  • Python脚本示例:-*
import json

def process_form(data):
    if 'approval_reason' in data:
        response = {
            'approval_status': 'Approved',
            'reason': 'Form approved successfully!'
        }
    else:
        response = {
            'approval_status': 'Rejected',
            'reason': 'Form rejected due to missing approval reason.'
        }
    return response

# Example JSON data representing a form submission
form_data = {
    "form_id": "12345",
    "submission_date": "2024-01-04",
    "name": "John Doe",
    "approval_reason": "Valid reason for approval"
}

# Process the form data
result = process_form(form_data)

# Convert the result to JSON and print it
result_json = json.dumps(result, indent=2)
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代码:-

import logging
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    
    try:
        req_body = req.get_json()
    except ValueError:
        return func.HttpResponse("Invalid JSON received", status_code=400)

    if 'approval_reason' in req_body:
        response = {
            'approval_status': 'Approved',
            'reason': 'Form approved successfully!'
        }
    else:
        response = {
            'approval_status': 'Rejected',
            'reason': 'Form rejected due to missing approval reason.'
        }
    
    return func.HttpResponse(
        body=str(response),
        status_code=200
    )

  • 文章正文:-*
{
    "form_id": "12345",
    "submission_date": "2024-01-04",
    "name": "John Doe",
    "approval_reason": "Valid reason for approval"
}

  • 函数app输出:-*

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

相关问题