2017-07-14 74 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

相關問題