外部 API
了解如何集成和配置 External API 功能,使角色能够访问实时信息、创建任务并与第三方平台交互。
最后更新于
这有帮助吗?
这有帮助吗?
{
"parameters": {
"city": {
"type": "string",
"description": "要获取天气信息的城市名称(例如:'London'、'New York'、'Tokyo')"
}
},
"required": [
"city"
]
}import requests
API_KEY = "<your-api-key>"
def handle_event(data):
city = data.get("city")
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}"
response = requests.get(url)
weather_data = response.json()
return {"weather": weather_data["weather"][0]["description"]}{
"city": "New York"
}{
"weather": "晴朗"
}{
"parameters": {
"summary": {
"type": "string",
"description": "Jira 工单的简短标题"
},
"description": {
"type": "string",
"description": "Jira 问题的详细描述"
}
},
"required": [
"summary",
"description"
]
}import requests
from requests.auth import HTTPBasicAuth
import json
# Jira 配置
JIRA_DOMAIN = "mycompany.atlassian.net" # 替换为你的 Jira 域名
EMAIL = "user@example.com" # 替换为你的 Atlassian 账户邮箱
API_TOKEN = "abc123xyz456..." # 替换为你的 Jira API 令牌
JIRA_PROJECT_KEY = "EX" # 替换为你的 Jira 项目键
ISSUE_TYPE = "Story" # 问题类型:Story、Task 或 Bug
API_ENDPOINT = f"https://{JIRA_DOMAIN}/rest/api/3/issue"
# Jira 所需的标准标头。
headers = {"Accept": "application/json", "Content-Type": "application/json"}
def create_jira_ticket(ticket_data):
"""
使用提供的 ticket_data 字典创建一个 JIRA 工单。
预期的 ticket_data 键:
- summary: (str) 问题的简要摘要。
- description: (str) 问题的详细描述。
返回:
- 如果工单创建成功,则返回 JSON 响应。
- 如果出错,则返回错误消息。
"""
# 将纯文本描述转换为 Atlassian 文档格式
description_adf = {
"version": 1,
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{"type": "text", "text": ticket_data.get("description", "")}
],
}
],
}
# 为 JIRA 问题构造负载
payload = {
"fields": {
"project": {"key": JIRA_PROJECT_KEY},
"summary": ticket_data.get("summary"),
"description": description_adf,
"issuetype": {"name": ISSUE_TYPE},
}
}
# 将负载转换为 JSON 字符串
payload_json = json.dumps(payload)
# 向 JIRA API 端点发送 POST 请求
response = requests.post(
API_ENDPOINT,
data=payload_json,
headers=headers,
auth=HTTPBasicAuth(EMAIL, API_TOKEN),
)
# 检查是否成功创建(HTTP 201 Created)
if response.status_code == 201:
return response.json()
else:
return {"error": f"创建工单失败:{response.status_code}"}
def handle_event(data):
return create_jira_ticket(data){
"summary": "这是用于测试工单创建的",
"description": "使用外部 API 创建"
}{
"id": "10004",
"key": "EX-5",
"self": "https://mycompany.atlassian.net/rest/api/3/issue/10004"
}