当前位置: 首页 > news >正文

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"))

说明

  1. 时间戳timestamp 是请求头中的必选参数,用于验证请求是否过期。代码中使用 time.time() 获取当前时间戳(秒级)。
  2. appid 和 appsecret:需要替换为实际的值,这些值可以在客服系统后台、账号管家后台或服务商后台中查看。
  3. Content-Type:请求头中需要指定 Content-Typeapplication/json,因为接口要求以 JSON 格式发送数据。
  4. 错误处理:根据返回的状态码(code)判断调用是否成功,并打印相应的信息。

注意事项

  • 如果返回状态码为 401,可能是时间戳过期或缺失。确保时间戳是当前时间,并且请求在10秒内完成。
  • 如果返回状态码为 102,可能是 appidappsecret 参数错误,请检查参数是否正确。
  • 在实际使用中,建议对 appidappsecret 进行加密处理,以防止泄露。

接口提供

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)

代码说明

  1. 请求体模型

    • 使用 Pydantic 的 BaseModel 定义了 CallRecord 模型,对应文档中的 JSON 数据结构。
    • 其中部分字段是可选的(Optional),例如 talk_pagereferer 等。
  2. 回调接口

    • 使用 @app.post("/callback") 定义了一个 POST 接口,用于接收海翕云的推送。
    • 从请求中提取 msg_idcontent 数据。
    • 使用 urllib.parse.unquotecontent 进行解码,并将其解析为 JSON 数据。
    • 将解析后的数据加载到 CallRecord 模型中,进行数据校验。
  3. 返回确认响应

    • 根据文档要求,返回 {"cmd": "OK", "token": "YOUR_TOKEN_HERE"},其中 YOUR_TOKEN_HERE 需要替换为实际的令牌。
  4. 运行方式

    • 使用 uvicorn 作为 ASGI 服务器运行 FastAPI 应用。
    • 默认监听 0.0.0.0:8000,可以根据需要修改端口。

测试方法

  1. 启动 FastAPI 应用

    uvicorn main:app --reload
    

    其中 main 是文件名,app 是 FastAPI 实例的名称。

  2. 发送测试请求
    使用 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"
    
  3. 检查输出

    • 确保 FastAPI 接口能够正确接收并解析数据,并返回确认响应。

如果需要进一步处理通话记录数据(例如存储到数据库或触发其他逻辑),可以在 callback 函数中添加相应的代码逻辑。

相关文章:

  • 对贵司需求的PLC触摸的远程调试的解决方案
  • 算法06-回溯算法
  • 京东 旋转验证码 分析
  • 伯克利 CS61A 课堂笔记 09 —— Data Abstraction
  • 图书管理项目(spring boot + Vue)
  • Cisco Catalyst交换机和ASR路由器上加vty下的列表时最后的vrf-also命令作用
  • DedeBIZ系统审计小结
  • RabbitMQ的死信队列的产生与处理
  • PHP 超级全局变量
  • 手机用流量怎样设置代理ip?
  • ArcGIS基础知识之ArcMap基础设置——ArcMap选项:常规选项卡设置及作用
  • 蓝桥杯篇---温度传感器 DS18B20
  • visual studio导入cmake项目后打开无法删除和回车
  • 51-ArrayList
  • 【LeetCode Hot100 双指针】移动零、盛最多水的容器、三数之和、接雨水
  • 人工智能之深度学习的革命性突破
  • 【Stable Diffusion部署至GNU/Linux】安装流程
  • Dify 是什么?Dify是一个开源的LLM应用开发平台,支持快速搭建生成式AI应用,具有RAG管道、Agent功能、模型集成等特点
  • 计算机网络,大白话
  • 代码随想录算法【Day44】
  • 做今日头条的怎么去网站找视频/扫图片识别图片原图
  • cad做兼职区哪个网站/合肥搜索引擎推广
  • 郑州网站建设网站推广/windows优化工具
  • 自贡做响应式网站开发公司/b2b电子商务平台排名
  • 新手怎么做网站打理/seo建站公司
  • wordpress主题加入html压缩/宁波seo服务