2017-07-14 77 views
6

我已经在Python中使用eroku设置TwiMl。 当我从用户B呼叫用户A时,用户A也没有得到呼叫和VOIP,而用户B得到了“感谢致电”等bot信息。twilio应用程序调用不起作用

当我尝试从PostManplaceCall给用户B时,用户B得到呼叫,并且还得到了诸如“感谢致电”之类的机器人消息。

邮递员网址: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) 

错误日志

enter image description here

TwiML上twilio仪表盘设置

请求URL:https://myapp.herokuapp.com/outgoing

请让我知道是有什么,我错过了配置或我做错了一些事情。

教程,我也跟着来配置TwiML是Here

回答

2

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,并且在快速入门应用程序中,您将获得一个示例语音消息。要拨打另一个应用程序,您需要从/outgoing返回不同的响应。在这种情况下,您需要使用<Dial>,嵌套<Client>,使用拨打电话时传递的To参数。

在Python /瓶,这将是这样的:

@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应用程序的URL /placeCall。确保你改回/outgoing

让我知道这是否有帮助。

+0

感谢您的答案,请你检查我更新的问题和错误日志,这将真正帮助我解决我的问题 – PinkeshGjr

+0

这是一个新的错误,所以通常最好打开一个新的问题。在这种情况下,问题在于你从错误的地方导入'VoiceGrant'。你需要'从twilio.jwt.access_token.grants导入VoiceGrant'。检查[访问令牌文档](https://www.twilio.com/docs/api/rest/access-tokens?code-sample=code-creating-an-access-token-voice&code-language=py&code-sdk-版本= 6.x)了解更多详情。 – philnash

+0

得到了另一个错误,请检查更新的问题与错误日志 谢谢 – PinkeshGjr

0

placeCall return语句试试这个代码

return str(
        '<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Client>' + IDENTITY + '</Client></Dial></Response>') 
+0

我是否需要更改twillio仪表板上的请求url? – PinkeshGjr

+0

没有必要,只是检查散列表是发送正确 –

+0

好吧,但我不知道placeCall这将打电话,因为请求网址是/传出 – PinkeshGjr

相关问题