执行python程序时获取错误,以便从twilio服务器获得呼叫者的电话会议

时间:2017-11-13 18:13:27

标签: python-2.7 twilio twilio-api ngrok twiml

我写了一篇关于[Ph Num 1< - >]的python程序。 Twilio Server< - > Ph Num2] [ - 从我的电话号码(Num 1)发送包含另一个电话号码(Num 2)的消息到twilio服务器 - twilio取了号码(Num2)并给我回电话(Num1)(说你即将开会) - twilio召唤Num 2并召集我(Num1)

这是我的计划:

from flask import Flask, request
from twilio import twiml
from twilio.rest import Client

app = Flask(__name__)

account = "AC7d2a***************"
token = "f83b27**************"
client = Client(account, token)

@app.route("/sms", methods=['GET', 'POST'])
def sms():
    number=request.form["From"]

    call= client.calls.create(from_="+1857*******",to=number,url="https://handler.twilio.com/twiml/EHb330731ab2c9a35fcdc620bef1b88536")

    return str(resp)

if __name__=="__main__":
    app.run()

url =“https://handler.twilio.com/twiml/EHb330731ab2c9a35fcdc620bef1b88536包含

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say>You are about to enter a conference</Say>
  <Dial><Conference>number</Conference></Dial>
</Response> 

我也做过Ngrok并在twilio号码的语音和留言网址中添加了我的Ngrok链接

我尝试了很多更改,并且在发送消息(带有电话号码(Num2)到会议)后给我发送了所有类型的错误而不是twilio号码

执行上述代码时出现的最新错误是 的错误:

[2017-11-13 09:46:41,570] ERROR in app: Exception on /sms [POST]
Traceback (most recent call last):
  File "/home/arif/my_app/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/arif/my_app/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/arif/my_app/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/arif/my_app/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/arif/my_app/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "conf.py", line 17, in sms
    return str(resp)
NameError: global name 'resp' is not defined
127.0.0.1 - - [13/Nov/2017 09:46:41] "POST /sms HTTP/1.1" 500 -

我的计划是否得到了我所期望的结果?任何人都可以帮助我问题在哪里?

1 个答案:

答案 0 :(得分:0)

Twilio开发者传道者在这里。

这里有几个问题。首先,您的webhook响应中出现错误。您可以在堆栈跟踪的底部看到错误:

  File "conf.py", line 17, in sms
    return str(resp)
NameError: global name 'resp' is not defined
127.0.0.1 - - [13/Nov/2017 09:46:41] "POST /sms HTTP/1.1" 500 -

它表示resp未定义。

您可以在方法中看到,您从未定义resp但是您尝试返回其str版本:

def sms():
    number=request.form["From"]
    call= client.calls.create(from_="+1857*******",to=number,url="https://handler.twilio.com/twiml/EHb330731ab2c9a35fcdc620bef1b88536")
    return str(resp)

在这种情况下,您只需将空字符串返回给Twilio。

def sms():
    number=request.form["From"]
    call= client.calls.create(from_="+1857*******",to=number,url="https://handler.twilio.com/twiml/EHb330731ab2c9a35fcdc620bef1b88536")
    return ('', 204)

一旦确定,你就会发现你只打了一个电话,拨打电话给你自己。要使用您要呼叫的号码进行会议,您需要生成两个呼叫。你说你要发送的邮件正文是你想要拨入会议的另一个号码,所以这应该有用:

def sms():
    my_number=request.form["From"]
    other_number=request.form["Body"]
    twilio_number="+1857*******"
    conference_url="https://handler.twilio.com/twiml/EHb330731ab2c9a35fcdc620bef1b88536"
    client.calls.create(from_=twilio_number,to=my_number,url=conference_url)
    client.calls.create(from_=twilio_number,to=other_number,url=conference_url)
    return ('', 204)

如果有帮助,请告诉我。