首页 > 解决方案 > 语音通话立即结束,nexmo 在一秒钟内挂断

问题描述

我在 Nexmo 仪表板中创建了一个带有事件 url 和答案 url 的应用程序。我运行从Nexmo 的 GitHub获得的以下代码:

client = nexmo.Client(key=api_key, secret=api_secret, application_id=application_key, private_key=private_key)

response = client.create_call({
  'to': [{'type': 'phone', 'number': 'call_to_number'}],
  'from': {'type': 'phone', 'number': 'call_from_number'},
  'answer_url': ['http://my_event_url']
})

电话号码被拨打,但 nexmo 立即挂断(在一秒钟内没有说任何话)。

在我的服务器上,我看到 Nexmo 调用了答案 url(带有 ncco)

调用答案网址时我会做什么:

import nexmo
import json
from django.http import JsonResponse

@csrf_exempt
def answer(request):

    ncco = [{
      "action": "talk",
      "voiceName": "Amy",
      "text": "Thank you for calling Nexmo. Please leave your message after the tone."
    }]

    d = json.dumps(ncco)
    j = is_json(d)
    if j:
        MyObject.objects.create(message="valid")
    else:
        MyObject.objects.create(message=user_ip + "json error")

    return JsonResponse(d, status=200, safe=False)


def is_json(myjson):
    try:
        json_object = json.loads(myjson)
    except ValueError:
        return False
    return True

这就是我调用 event_url 时所做的事情:

@csrf_exempt
def event(request):

    d = json.dumps(request.POST)
    DataReceived.objects.create(answer=d)

    data = {"ok": True}

    return JsonResponse(data, status=200)

Nexmo 调用了 5 次事件 URL,但字典始终为空。

标签: jsondjangopython-3.xnexmo

解决方案


`我认为您可能正在“双重 JSON 倾销”您的 NCCO,

您将其创建ncco为python dict,然后将其转换为json字符串, d = json.dumps(ncco)然后调用JsonResponse它,尝试将ncco对象传递给JsonResponse

return JsonResponse(ncco, status=200, safe=False)


推荐阅读