twilio App to App calling is not working
Asked Answered
I

2

6

I have setup TwiMl on eroku in Python. When I call user A from user B, user A didn't get call and VOIP also, while user B is got bot message like "thanks for calling".

When I try to placeCall to user B from PostMan, user B gets call and also got bot message like "thanks for calling".

PostMan URL : https://myapp.herokuapp.com/placeCall

My requirement is when I call user A from application user B will get call and both can able to communicate.

Requirement

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

Here is my Python TwiMl Code.

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)

Error Logs

enter image description here

TwiML setting on twilio dashboard

Request URL : https://myapp.herokuapp.com/outgoing

Please let me know is there anything that I missed to configure or something that I have done wrong.

Tutorial that I followed to configure TwiML is Here

Industrious answered 14/7, 2017 at 6:29 Comment(0)
A
2

Twilio developer evangelist here.

I'm not sure whether you're building an iOS or Android app, but the idea is the same. When you place a call, like in the example here from the iOS quickstart in Swift, using code like this:

TwilioVoice.sharedInstance().call(accessToken, params: [:], delegate: self)

you should send some parameters with that call, for example the client identity that you are calling. e.g.

TwilioVoice.sharedInstance().call(accessToken, params: ["To": "ClientIdentity"], delegate: self)

Then, Twilio will call the URL you set in your TwiML application. In the quickstart the url should be /outgoing and in the quickstart application you get an example voice message. To make the call to another application you need to return a different response from /outgoing. In this case, you need to use <Dial> with a nested <Client> using the To parameter passed when making the call.

In Python/Flask this would look like:

@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)

I notice that in the comments on the question you changed the TwiML App URL to /placeCall. Make sure you change that back to /outgoing.

Let me know if this helps at all.

Agathaagathe answered 17/7, 2017 at 17:13 Comment(10)
Thanks for the answer, would you please check my updated question and error logs, that would really help me solve my issueIndustrious
That's a new error, so it's normally best to open a new question for that. In this case, the issue is that you are importing VoiceGrant from the wrong place. You need to from twilio.jwt.access_token.grants import VoiceGrant. Check the access token documentation for more detail.Agathaagathe
got another error please check updated question with error logs thanksIndustrious
This might be because you have installed the latest version of the Python helper library. In this case, try changing all the occurrences of twilio.twiml.Response to twilio.twiml.VoiceResponse.Agathaagathe
Are you sure it's the same error? Can you recreate it and add the new error to your question? And update to show your current code.Agathaagathe
Try updating your import to include from twilio.twiml.voice_response import VoiceResponse.Agathaagathe
I see you've accepted the answer now! Did the import work?Agathaagathe
Ok, what do you have right now and what issues are you seeing? Also, what version of the Python helper library are you using?Agathaagathe
added more details please check ThanksIndustrious
Can you make sure you pin the Twilio requirement to the latest version, 6.4.3, install and try again.Agathaagathe
M
0

try this code at the placeCall return statement

return str(
        '<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Client>' + IDENTITY + '</Client></Dial></Response>')
Modillion answered 14/7, 2017 at 14:0 Comment(4)
Do i need to change request url on twillio dashboard?Industrious
Ok but i am not sure about placeCall how this will going to call because request url is /outgoingIndustrious
sorry change the url at the twilio with the /placeCallModillion
Ok will let you knowIndustrious

© 2022 - 2024 — McMap. All rights reserved.