使用 Python 构建 bot 以解析 JSON 响应
使用 Automation 360 Python 脚本执行 Python 功能以构建 bot。使用 Python 函数从 REST Web 服务 GET 请求解析 JSON 响应。
先决条件
确保您具备以下功能以构建 bot:
- 对 Python 编程语言的基本理解。
- 创建 Automation Anywhere bots 的基本经验。
- 下载并安装 Python 3。
在 Python 安装期间,向环境变量添加路径,然后选择将 Python 3.8 添加到 PATH。
过程
- 登录到 Control Room。
- 转到机器人 > 我的机器人,然后单击创建机器人图标。
- 在名称字段中,输入 PythonTutorial。
- 单击创建和编辑。
-
在操作窗格中,单击变量以创建以下新变量:
- 名称 dResponse,类型 > 字典和子类型 > 字符串。
- 名称 dRetrieveValue,类型 > 字典和子类型 > 任意。
- 名称 sResponseBody,类型 > 字符串。
- 名称 sFullName,类型 > 字符串。
- 名称 sLocation,类型 > 字符串。
- 名称 sTotalUserCount,类型 > 字符串。
- 名称 nTotalUserCount,类型 > 数字。
- 名称 nCurrentUser,类型 > 数字。
- 名称 sCurrentUser,类型 > 字符串。
-
从操作窗格中,选择 REST Web 服务 > GET 方法,并将其置于 流程的开始bot下方。
-
在 URI 字段中,输入 https://randomuser.me/api/?results=5&inc=name,email,location&nat=us。
这是一个示例 API,可将随机用户详细信息返回给调用应用程序。
- 在验证模式下拉列表中,选择无验证。
- 在将输出分配给变量下拉列表中,选择 dResponse -Dictionary of Strings。
-
在 URI 字段中,输入 https://randomuser.me/api/?results=5&inc=name,email,location&nat=us。
-
从操作窗格中,单击字符串 > 分配。
- 在选择源字符串变量值字段中,输入 $dResponse{Body}$。
- 在将输出分配给变量下拉列表中,选择 dResponseBody - String。
-
从操作窗格中,单击 Python 脚本 > 打开。
- 在 Python 字段中,选择手动输入。
-
将以下代码复制并粘贴到在此输入脚本字段。
import json def get_node_count(response): #parse response as json response_dict=json.loads(response) # Create list from JSON body response_body = response_dict['results'] #return the count of entries in JSON body as string lengthasstring = str(len(response_body)) return lengthasstring def get_full_name(dictRequest): itemCount = int(dictRequest['count']) #parse response as json response_dict=json.loads(dictRequest['response']) # Create list from JSON body response_body = response_dict['results'] #Extract values to return return response_body[itemCount]['name']['first'] + " " + response_body[itemCount]['name']['last'] def get_location(dictRequest): itemCount = int(dictRequest['count']) #parse response as json response_dict=json.loads(dictRequest['response']) # Create list from JSON body response_body = response_dict['results'] #Extract values to return return response_body[itemCount]['location']['city'] + ", " + response_body[itemCount]['location']['state']
- 在 Python 运行时版本字段中,保留默认值为 3。
-
从操作窗格中,单击 Python 脚本 > 执行功能。
- 在 Python 会话字段中,保留默认值。
- 在输入要执行的函数的名称字段中,输入 get_node_count。
- 在函数参数下拉列表中,选择 dResponseBody - String。
- 在将输出分配给变量下拉列表中,选择 sTotalUserCount - String。
-
从操作窗格中,单击字典 > Put。
- 在字典变量字段中,选择 dRetrieveValue -Dictionary。
- 在关联到此键字段中,输入响应。
- 在新值下拉列表中,选择 dResponseBody - String。
- 在为变量分配上一个值下拉列表中,选择 prompt-assignment - String。
-
从操作窗格中,单击字符串 > 至数字。
- 在输入字符串字段中,输入 sTotalUserCount。
- 在将输出分配给变量下拉列表中,选择 nTotalUserCount - Number。
-
从操作窗格中,单击循环 > 循环。
- 在循环类型 > 迭代器中,从下拉列表中选择 n 次。
- 在时间字段中,输入 $nTotalUserCount$。
- 在将当前值分配给变量下拉列表中,选择 nCurrentUser - Number。
-
从操作窗格中,单击数字,选择减量并将其放入循环操作中。
- 在输入号码字段中,输入 $nCurrentUser$。
- 在输入减量值字段中,输入 1。
- 在将输出分配给变量下拉列表中,选择 nCurrentUser - Number。
-
从操作窗格中,单击数字,选择至字符串并将其放入循环操作中,位于数字 > 减量下方。
- 在输入号码字段中,输入 $nCurrentUser$。
- 在输入小数点后的位数字段中,输入 0。
- 在将输出分配给变量下拉列表中,选择 sCurrentUser - String。
-
从操作窗格中,单击字典 > Put并将其放入循环操作中。
- 在字典变量字段中,选择 dRetrieveValue -Dictionary。
- 在关联到此键字段中,输入计数。
- 在新值下拉列表中,选择 sCurrentUser - String。
- 在为变量分配上一个值下拉列表中,选择 prompt-assignment - String。
-
从操作窗格中,单击 Python 脚本 > 执行函数并将其放入循环操作中。
- 在 Python 会话字段中,保留默认值。
- 在输入要执行的函数的名称字段中,输入 get_full_name。
- 在函数参数下拉列表中,选择 dRetrieveValue - Dictionary。
- 在将输出分配给变量下拉列表中,选择 sFullName - String。
-
从操作窗格中,单击 Python 脚本 > 执行函数并将其放入循环操作中。
- 在 Python 会话字段中,保留默认值。
- 在输入要执行的函数的名称字段中,输入 get_location。
- 在函数参数下拉列表中,选择 dRetrieveValue - Dictionary。
- 在将输出分配给变量下拉列表中,选择 sLocation - String。
-
从操作窗格中,单击消息框并将其放入循环操作中。
-
在输入要显示的消息字段中输入以下值:
Full Name: $sFullName$ Location: $sLocation$
- 选择在以下 > 秒数后关闭消息框,输入 5。
-
在输入要显示的消息字段中输入以下值:
-
从操作窗格中,单击 Python 脚本 > 关闭并将其放在循环操作之外。
- 在 Python 会话字段中,保留默认值。
-
单击保存以保存您的 bot,然后单击运行。
bot 运行,显示五个完整的用户名及位置,每个用户名及位置大约显示 5 秒钟,然后成功完成执行。