<Twilio/Python>jsonify调用日志

2024-04-27 05:00:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我按照Twilio教程来设置一个iOS项目。因为它需要一个后端,所以我选择了Python(我是一个iOS开发人员,对Python的知识一无所知),所以这个问题可能很愚蠢,但我没有找到合适的语法。你知道吗

目标: 获取所有通话记录和会议记录,并将其作为JSON响应返回。你知道吗

我的代码:

@app.route('/getRecordings', methods=['GET'])
def getRecordings():
    client = Client(ACCOUNT_SID, ACCOUNT_AUTH_TOKEN)
    recordings = []
    for record in client.calls.list():
        recordings.append(record.sid)
    conferences = []
    for conf in client.conferences.list():
        conferences.append(conf.sid)
    return jsonify(calls=recordings, conferences=conferences)

响应: 我得到了正确的响应,因为我只附加了每个调用的SID属性。你知道吗

{
  "calls": [
    "CAxxx",
    "CAxxx",
  ],
  "conferences": [
    "CFxxx",
    "CFxxx",
  ]
}

但是我想从Twilio(参考:https://www.twilio.com/docs/api/voice/conference)获得每个记录的完整细节,如这个示例的output选项卡所示 当我尝试JSONify记录时,它说它不能JSONify这个类型的对象。你知道吗

我知道我应该将对象转换为模型并附加它,但是我该怎么做呢?任何链接或线索,以帮助得到这个排序是非常感谢。你知道吗


Tags: inclientforaccountrecordlistiostwilio
2条回答

您需要创建一个包含所需值的词典列表。像这样:

for record in client.calls.list():
    call = {"account_sid": record.account_sid, "api_version": record.api_version, "date_created": record.date_created, "etc": record.etc}
    recordings.append(call)

这应该会给你一个如下的回答:

{
  "calls": [
    {
      "accound_sid": "1234", 
      "api_version": "2010-04-01", 
      "date_created": "Wed, 18 Aug 2010 20:20:06 +0000", 
      "etc": "etc", 
    },
    {
      "accound_sid": "4321", 
      "api_version": "2010-04-01", 
      "date_created": "Wed, 18 Aug 2010 20:20:06 +0000", 
      "etc": "etc", 
    }
  ]
}

Twilio开发者福音传道者。你知道吗

如果您希望将完整的JSON响应代理给您的应用程序,那么您可能会发现避免使用Twilio库更容易,只需向jsonapi端点发出请求并直接发送响应。你知道吗

例如,要使用Python的Requests library获得list of calls,可以执行以下操作:

@app.route('/getCalls', methods=['GET'])
def getCalls():
  url = 'https://api.twilio.com/2010-04-01/Accounts/YOUR_ACCOUNT_SID/Calls/.json'
  request = requests.get(url, auth=(YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN)

  resp = Response(response=request.text,
                  status=200,
                  mimetype="application/json")
  return resp

如果有帮助,请告诉我。你知道吗

相关问题 更多 >