代码之家  ›  专栏  ›  技术社区  ›  DevB2F

语音通话马上结束,nexmo在一秒钟内挂断

  •  0
  • DevB2F  · 技术社区  · 6 年前

    我在nexmo仪表板中创建了一个带有事件URL和应答URL的应用程序。我运行以下代码 Nexmo´s 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)

    调用应答URL时的操作:

    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
    

    这是调用事件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调用事件url 5次,但字典始终为空。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Sam Machin    6 年前

    `我认为你可能是“双重JSON倾销”你的NCCO,

    你创造 ncco 作为python dict,然后将其转换为json字符串, d = json.dumps(ncco) 然后你打电话来 JsonResponse 在上面,试着通过 国家商会 对象到jsonResponse

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