Kimi实战1/100 - 读接口文档,编写接口
文章目录
- Kimi实战1/100 - 读接口文档,编写接口
- 接口调用
- requests 调用代码
- 说明
- 注意事项
- 接口提供
- FastAPI 接口代码
- 代码说明
- 测试方法
Kimi实战1/100 - 读接口文档,编写接口
接口调用
User: 根据 接口文档 https://www.eiisys.com/home/apiDetails?id=00300000280
编写 requests 调用
Kimi: 根据提供的接口文档,可以使用 Python 的 requests
库来调用该接口。以下是一个示例代码,用于获取 53kf_token
:
requests 调用代码
import requests
import time
import json
# 定义请求的URL
url = "https://appidmanager.53kf.com/appid/manager/get/token"
# 获取当前时间戳(秒级)
timestamp = int(time.time())
# 定义headers参数
headers = {
"timestamp": str(timestamp),
"Content-Type": "application/json"
}
# 定义body参数(需要替换为实际的appid和appsecret)
body = {
"cmd": "53kf_token",
"appid": "your_appid", # 替换为实际的appid
"appsecret": "your_appsecret" # 替换为实际的appsecret
}
# 发送POST请求
response = requests.post(url, headers=headers, data=json.dumps(body))
# 获取响应内容
response_data = response.json()
# 打印响应结果
print("响应状态码:", response_data.get("code"))
print("响应数据:", response_data)
# 根据返回的状态码处理结果
if response_data.get("code") == 200:
token = response_data.get("data", {}).get("53kf_token")
print("获取的53kf_token:", token)
print("Token有效期:", response_data.get("data", {}).get("expires_in"), "秒")
else:
print("调用失败,错误信息:", response_data.get("msg"))
说明
- 时间戳:
timestamp
是请求头中的必选参数,用于验证请求是否过期。代码中使用time.time()
获取当前时间戳(秒级)。 - appid 和 appsecret:需要替换为实际的值,这些值可以在客服系统后台、账号管家后台或服务商后台中查看。
- Content-Type:请求头中需要指定
Content-Type
为application/json
,因为接口要求以 JSON 格式发送数据。 - 错误处理:根据返回的状态码(
code
)判断调用是否成功,并打印相应的信息。
注意事项
- 如果返回状态码为
401
,可能是时间戳过期或缺失。确保时间戳是当前时间,并且请求在10秒内完成。 - 如果返回状态码为
102
,可能是appid
或appsecret
参数错误,请检查参数是否正确。 - 在实际使用中,建议对
appid
和appsecret
进行加密处理,以防止泄露。
接口提供
User: 根据接口文档 https://www.eiisys.com/home/apiDetails?id=00300000245
编写对应的fastapi接口
Kimi: 根据提供的接口文档,我们可以使用 FastAPI 框架编写一个对应的接口来接收海翕云的通话记录推送。以下是实现代码:
FastAPI 接口代码
from fastapi import FastAPI, Request, HTTPException
from pydantic import BaseModel
from typing import List, Optional
import json
import urllib.parse
app = FastAPI()
# 定义请求体的模型
class CallRecord(BaseModel):
cmd: str
company_id: str
phone: str
call: str
start_time: str
end_time: str
last_time: str
endtype: str
talk_page: Optional[str] = None
land_page: Optional[str] = None
referer: Optional[str] = None
se: Optional[str] = None
kw: Optional[str] = None
worker_id: Optional[str] = None
call_id: str
come_out: str
recordurl: Optional[str] = None
third_id: Optional[str] = None
out_robot_guest_name: Optional[str] = None
out_robot_record: Optional[List[dict]] = None
# 定义回调接口
@app.post("/callback")
async def callback(request: Request):
form_data = await request.form()
msg_id = form_data.get("msg_id")
content = form_data.get("content")
if not msg_id or not content:
raise HTTPException(status_code=400, detail="Missing msg_id or content")
try:
# 解码 content 数据
decoded_content = urllib.parse.unquote(content)
call_record = json.loads(decoded_content)
call_record_model = CallRecord(**call_record)
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid JSON format")
# 处理通话记录数据
print(f"Received call record with msg_id: {msg_id}")
print(call_record_model)
# 返回确认响应
return {"cmd": "OK", "token": "YOUR_TOKEN_HERE"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
代码说明
-
请求体模型:
- 使用 Pydantic 的
BaseModel
定义了CallRecord
模型,对应文档中的 JSON 数据结构。 - 其中部分字段是可选的(
Optional
),例如talk_page
、referer
等。
- 使用 Pydantic 的
-
回调接口:
- 使用
@app.post("/callback")
定义了一个 POST 接口,用于接收海翕云的推送。 - 从请求中提取
msg_id
和content
数据。 - 使用
urllib.parse.unquote
对content
进行解码,并将其解析为 JSON 数据。 - 将解析后的数据加载到
CallRecord
模型中,进行数据校验。
- 使用
-
返回确认响应:
- 根据文档要求,返回
{"cmd": "OK", "token": "YOUR_TOKEN_HERE"}
,其中YOUR_TOKEN_HERE
需要替换为实际的令牌。
- 根据文档要求,返回
-
运行方式:
- 使用
uvicorn
作为 ASGI 服务器运行 FastAPI 应用。 - 默认监听
0.0.0.0:8000
,可以根据需要修改端口。
- 使用
测试方法
-
启动 FastAPI 应用:
uvicorn main:app --reload
其中
main
是文件名,app
是 FastAPI 实例的名称。 -
发送测试请求:
使用curl
或 Postman 发送 POST 请求到http://127.0.0.1:8000/callback
,请求体为表单数据:curl -X POST "http://127.0.0.1:8000/callback" \ -F "msg_id=ee505c81-3f29-4cb4-abdf-d0bb846fab9e" \ -F "content=%7B%22cmd%22%3A%22callback_phone%22%2C%22company_id%22%3A%2272000370%22%2C%22phone%22%3A%2217357927020%22%2C%22call%22%3A%2218158360535%22%2C%22start_time%22%3A%222021-06-28+15%3A45%3A46%22%2C%22end_time%22%3A%222021-06-28+15%3A45%3A59%22%2C%22last_time%22%3A%222%22%2C%22endtype%22%3A%222%22%2C%22call_id%22%3A%2216374%22%2C%22recordurl%22%3A%22https%3A%5C%2F%5C%2Ffs.53kf.com%5C%2Frecordings%5C%2F2021-06-28%5C%2F7573bba2-6b21-4a07-8995-1cab4875a7a4.wav%22%7D"
-
检查输出:
- 确保 FastAPI 接口能够正确接收并解析数据,并返回确认响应。
如果需要进一步处理通话记录数据(例如存储到数据库或触发其他逻辑),可以在 callback
函数中添加相应的代码逻辑。