twilio App 到 App 调用不起作用
twilio App to App calling is not working
我在 Python 的 eroku 上安装了 TwiMl。
当我从用户 B 呼叫用户 A 时,用户 A 也没有接到电话和 VOIP,而用户 B 收到像 "thanks for calling".
这样的机器人消息
当我尝试从 PostMan
向用户 B placeCall
时,用户 B 接到电话并且还收到了像 "thanks for calling".
这样的机器人消息
邮递员 URL : https://myapp.herokuapp.com/placeCall
我的要求是,当我从应用程序中呼叫用户 A 时,用户 B 会接到呼叫并且两者都能够通信。
要求
Flask==0.10.1
Jinja2==2.7.3
MarkupSafe==0.23
Werkzeug==0.9.6
httplib2==0.9
itsdangerous==0.24
six==1.*
twilio
wsgiref==0.1.2
这是我的 Python TwiMl 代码。
import os
from flask import Flask, request
from twilio.jwt.access_token import AccessToken
from twilio.jwt.access_token.grants import VoiceGrant
from twilio.rest import Client
import twilio.twiml
ACCOUNT_SID = 'ACxxxxxxxx'
API_KEY = 'SKxxxxxxxx'
API_KEY_SECRET = 'TSxxxxxxxx'
PUSH_CREDENTIAL_SID = 'CRxxxxxxxx'
APP_SID = 'APxxxxxxxx'
app = Flask(__name__)
@app.route('/test', methods=['GET', 'POST'])
def test():
req_json = request.get_json(force=True)
UserName = req_json['username']
Password = req_json['password']
return str(UserName)
@app.route('/accessToken')
def token():
IDENTITY = request.args.get('identity')
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
api_key = os.environ.get("API_KEY", API_KEY)
api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
app_sid = os.environ.get("APP_SID", APP_SID)
grant = VoiceGrant(push_credential_sid=push_credential_sid,outgoing_application_sid=app_sid)
token = AccessToken(account_sid, api_key, api_key_secret, IDENTITY)
token.add_grant(grant)
return str(token)
@app.route('/outgoing', methods=['GET', 'POST'])
def outgoing():
req_json = request.get_json(force=True)
CALLER_ID = req_json['callerid']
resp = twilio.twiml.VoiceResponse()
dial = Dial()
dial.client(CALLER_ID)
resp.append(dial)
#resp.say("Congratulations! You have made your first oubound call! Good bye.")
#resp.say("Thanks for Calling.",voice='woman',)
return str(resp)
@app.route('/incoming', methods=['GET', 'POST'])
def incoming():
resp = twilio.twiml.VoiceResponse()
#resp.say("Congratulations! You have received your first inbound call! Good bye.")
#resp.say("Thanks for Calling.",voice='woman',)
return str(resp)
@app.route('/placeCall', methods=['GET', 'POST'])
def placeCall():
req_json = request.get_json(force=True)
IDENTITY = req_json['identity']
CALLER_ID = req_json['callerid']
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
api_key = os.environ.get("API_KEY", API_KEY)
api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
client = Client(api_key, api_key_secret, account_sid)
call = client.calls.create(url=request.url_root + 'incoming', to='client:' + CALLER_ID, from_='client:' + IDENTITY)
return str(call.sid)
@app.route('/', methods=['GET', 'POST'])
def welcome():
resp = twilio.twiml.VoiceResponse()
resp.say("Welcome")
return str(resp)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port, debug=True)
错误日志
twilio 仪表板上的 TwiML 设置
请求URL:https://myapp.herokuapp.com/outgoing
请告诉我是否有任何我遗漏的配置或我做错了什么。
我遵循的配置 TwiML 的教程是 Here
在 placeCall
return 语句
中尝试此代码
return str(
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Client>' + IDENTITY + '</Client></Dial></Response>')
此处为 Twilio 开发人员布道师。
我不确定您是在构建 iOS 还是 Android 应用程序,但想法是一样的。当您拨打电话时,就像此处来自 iOS quickstart in Swift 的示例一样,使用如下代码:
TwilioVoice.sharedInstance().call(accessToken, params: [:], delegate: self)
您应该在该调用中发送一些参数,例如您正在调用的客户端身份。例如
TwilioVoice.sharedInstance().call(accessToken, params: ["To": "ClientIdentity"], delegate: self)
然后,Twilio 将调用您在 TwiML 应用程序中设置的 URL。在快速入门中,url 应该是 /outgoing
,在快速入门应用程序中,您会得到一个示例语音消息。要调用另一个应用程序,您需要 return 来自 /outgoing
的不同响应。在这种情况下,您需要使用 <Dial>
with a nested <Client>
使用调用时传递的 To
参数。
在 Python/Flask 中看起来像:
@app.route('/outgoing', methods=['GET', 'POST'])
def outgoing():
resp = twilio.twiml.Response()
dial = Dial()
dial.client(request.form['To'])
response.append(dial)
return str(resp)
我注意到在问题的评论中,您将 TwiML App URL 更改为 /placeCall
。确保将其更改回 /outgoing
.
如果这有帮助,请告诉我。
我在 Python 的 eroku 上安装了 TwiMl。 当我从用户 B 呼叫用户 A 时,用户 A 也没有接到电话和 VOIP,而用户 B 收到像 "thanks for calling".
这样的机器人消息当我尝试从 PostMan
向用户 B placeCall
时,用户 B 接到电话并且还收到了像 "thanks for calling".
邮递员 URL : https://myapp.herokuapp.com/placeCall
我的要求是,当我从应用程序中呼叫用户 A 时,用户 B 会接到呼叫并且两者都能够通信。
要求
Flask==0.10.1
Jinja2==2.7.3
MarkupSafe==0.23
Werkzeug==0.9.6
httplib2==0.9
itsdangerous==0.24
six==1.*
twilio
wsgiref==0.1.2
这是我的 Python TwiMl 代码。
import os
from flask import Flask, request
from twilio.jwt.access_token import AccessToken
from twilio.jwt.access_token.grants import VoiceGrant
from twilio.rest import Client
import twilio.twiml
ACCOUNT_SID = 'ACxxxxxxxx'
API_KEY = 'SKxxxxxxxx'
API_KEY_SECRET = 'TSxxxxxxxx'
PUSH_CREDENTIAL_SID = 'CRxxxxxxxx'
APP_SID = 'APxxxxxxxx'
app = Flask(__name__)
@app.route('/test', methods=['GET', 'POST'])
def test():
req_json = request.get_json(force=True)
UserName = req_json['username']
Password = req_json['password']
return str(UserName)
@app.route('/accessToken')
def token():
IDENTITY = request.args.get('identity')
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
api_key = os.environ.get("API_KEY", API_KEY)
api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
app_sid = os.environ.get("APP_SID", APP_SID)
grant = VoiceGrant(push_credential_sid=push_credential_sid,outgoing_application_sid=app_sid)
token = AccessToken(account_sid, api_key, api_key_secret, IDENTITY)
token.add_grant(grant)
return str(token)
@app.route('/outgoing', methods=['GET', 'POST'])
def outgoing():
req_json = request.get_json(force=True)
CALLER_ID = req_json['callerid']
resp = twilio.twiml.VoiceResponse()
dial = Dial()
dial.client(CALLER_ID)
resp.append(dial)
#resp.say("Congratulations! You have made your first oubound call! Good bye.")
#resp.say("Thanks for Calling.",voice='woman',)
return str(resp)
@app.route('/incoming', methods=['GET', 'POST'])
def incoming():
resp = twilio.twiml.VoiceResponse()
#resp.say("Congratulations! You have received your first inbound call! Good bye.")
#resp.say("Thanks for Calling.",voice='woman',)
return str(resp)
@app.route('/placeCall', methods=['GET', 'POST'])
def placeCall():
req_json = request.get_json(force=True)
IDENTITY = req_json['identity']
CALLER_ID = req_json['callerid']
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
api_key = os.environ.get("API_KEY", API_KEY)
api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
client = Client(api_key, api_key_secret, account_sid)
call = client.calls.create(url=request.url_root + 'incoming', to='client:' + CALLER_ID, from_='client:' + IDENTITY)
return str(call.sid)
@app.route('/', methods=['GET', 'POST'])
def welcome():
resp = twilio.twiml.VoiceResponse()
resp.say("Welcome")
return str(resp)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port, debug=True)
错误日志
twilio 仪表板上的 TwiML 设置
请求URL:https://myapp.herokuapp.com/outgoing
请告诉我是否有任何我遗漏的配置或我做错了什么。
我遵循的配置 TwiML 的教程是 Here
在 placeCall
return 语句
return str(
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Client>' + IDENTITY + '</Client></Dial></Response>')
此处为 Twilio 开发人员布道师。
我不确定您是在构建 iOS 还是 Android 应用程序,但想法是一样的。当您拨打电话时,就像此处来自 iOS quickstart in Swift 的示例一样,使用如下代码:
TwilioVoice.sharedInstance().call(accessToken, params: [:], delegate: self)
您应该在该调用中发送一些参数,例如您正在调用的客户端身份。例如
TwilioVoice.sharedInstance().call(accessToken, params: ["To": "ClientIdentity"], delegate: self)
然后,Twilio 将调用您在 TwiML 应用程序中设置的 URL。在快速入门中,url 应该是 /outgoing
,在快速入门应用程序中,您会得到一个示例语音消息。要调用另一个应用程序,您需要 return 来自 /outgoing
的不同响应。在这种情况下,您需要使用 <Dial>
with a nested <Client>
使用调用时传递的 To
参数。
在 Python/Flask 中看起来像:
@app.route('/outgoing', methods=['GET', 'POST'])
def outgoing():
resp = twilio.twiml.Response()
dial = Dial()
dial.client(request.form['To'])
response.append(dial)
return str(resp)
我注意到在问题的评论中,您将 TwiML App URL 更改为 /placeCall
。确保将其更改回 /outgoing
.
如果这有帮助,请告诉我。