Search code examples
fluttertwiliotext-to-speech

Convert a text to voice recording and then play it after calling a phone number on Flutter


I'm trying to make an app which gets the location of a user and then call a number(pre-selected by user) to send a recorded voice message, like say "Location is {longitude} degrees, {latitude} degrees" but I can't find anything to do the same I found Twilio on some searching but in the Flutter package twilio_voice I can't find anything to do what I want.

So my question is can this be done in Twilio and if not what else I can do?

(Edit :I have figured out the part to fetch the user location and just display it on the screen, the part that remains is to convert this to speech and play on call)


Solution

  • You can achieve this with Twilio, however you will need a server from which to make the requests to the Twilio API. To make an API call to Twilio you need to use your Account SID and Auth Token and if you were to embed those within your Flutter application then a malicious user would be able to decompile your app, steal your credentials and abuse your account.

    So, the best way is to build a server-side application that can make the API calls on behalf of your app and then send the relevant data (the location, in this case) from your mobile app to the server application.

    On the server-side application, once you receive the location data you can generate a call by calling on the Twilio calls resource and pass the number you want to call, the number you are calling from and the TwiML you want to execute on the call. TwiML tells Twilio what to do on a call, and in this case you can direct Twilio to <Say> the message with the location.

    I see you've asked python questions in the past, so here's a quick example of calling the Twilio API to create a call in python:

    import os
    from twilio.rest import Client
    
    # Find your Account SID and Auth Token at twilio.com/console
    # and set the environment variables. See http://twil.io/secure
    account_sid = os.environ['TWILIO_ACCOUNT_SID']
    auth_token = os.environ['TWILIO_AUTH_TOKEN']
    client = Client(account_sid, auth_token)
    
    call = client.calls.create(
                            twiml='<Response><Say>Location is {longitude} degrees, {latitude} degrees</Say></Response>',
                            to=TO_NUMBER,
                            from_=YOUR_TWILIO_NUMBER
                        )
    
    print(call.sid)