0

I'm trying to find a way to use Twilio data inside of Salesforce Objects. I've already embeded Twilio Flex using the Open CTI and is working fine, but now I need to transcribe the calls and put that transcription inside a Text Area(Long) Field in Salesforce.

Another option could be not using Flex and just connecting to Twilio's APIs, but I'm having a hard time doing that too. I can't connect to the API because I think I'm not creating the Headers the right way.

Could someone please help me create this functionality?

Thank you so much!!

I'm adding the code I'm trying to use and I get a status code 400 bad request in the log.

public class Prueba { public String mobileNumber {get;set;} public String responseBody {get;set;} public String DEFAULT_VERSION = '2010-04-01'; public String AccountSID = 'AC6b8c58deeccc0eaa680eef757596af41'; public String Authtoken = '60e451a5dbba8de2baf00b5916b5f1a4'; public String FromPhoneNumber = '+12159952919'; public String VoiceAPIEndPoint = 'https://demo.twilio.com/welcome/voice/';

 public String Body = '';
 
 public Void VoiceAPI()
 { 
 String Crediential = EncodingUtil.base64Encode(Blob.valueOf(AccountSID + ':' + Authtoken));
 String EndPoint = 'https://api.twilio.com/'+DEFAULT_VERSION+'/Accounts/'+AccountSID+'/Calls.json';
 
 if(mobileNumber != '')
 {
 Body = 'To='+mobileNumber+'&'+
 'From='+FromPhoneNumber+'&'+
 'Url='+VoiceAPIEndPoint;
 }
 else
 { 
 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Mobile Number is required.'); 
 ApexPages.addMessage(myMsg); 
 return;
 }
 
 Http h = new Http();
 HttpRequest request = new HttpRequest();
 request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
 request.setHeader('Authorization', 'Basic '+ Crediential);
 request.setEndpoint(EndPoint);
 request.setMethod('GET');
 request.setBody(Body);
 HttpResponse res = h.send(request);
 responseBody = res.getBody();
 
}

}

1 Answer 1

0

Haven't used Salesforce Connect, but I have tight integration with Twilio via Apex and external services.

Here is the example of how you can build requests in Apex

        HttpRequest request = new HttpRequest();

        request.setMethod('GET');
        request.setEndpoint('https://api.twilio.com/2010-04-01/Accounts.json');
        request.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(accountId + ':' + authToken)));

        HttpResponse response = new Http().send(request);
        System.debug(JSON.serializePretty(JSON.deserializeUntyped(response.getBody())));

And you can use Basic Authentification in Postman to test your APIs the same way Account_Id for Username and Auth_Token.

Hopefully it'll help

UPDATE

try like this

public String Body = '';

public void VoiceAPI() {
    String Crediential = EncodingUtil.base64Encode(Blob.valueOf(AccountSID + ':' + Authtoken));
    String EndPoint = 'https://api.twilio.com/'+DEFAULT_VERSION+'/Accounts/'+AccountSID+'/Calls.json';

    if(mobileNumber != '') {
        Body = 'To='+EncodingUtil.urlEncode(mobileNumber, 'utf-8')+'&'+
        'From='+EncodingUtil.urlEncode(FromPhoneNumber, 'utf-8')+'&'+
        'Url='+EncodingUtil.urlEncode(VoiceAPIEndPoint, 'urf-8');
    }
    else {
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Mobile Number is required.');
        ApexPages.addMessage(myMsg);
        return;
    }

    Http h = new Http();
    HttpRequest request = new HttpRequest();
    request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
    request.setHeader('Authorization', 'Basic '+ Crediential);
    request.setEndpoint(EndPoint);
    request.setMethod('POST');
    request.setBody(Body);
    HttpResponse res = h.send(request);
    responseBody = res.getBody();

}

17
  • Thank you for your help!! I just added the class I'm trying to use with a Twilio trial account. When I execute Prueba.VoiceAPI(); I see a status 400 bad request in the logs. What could be going on? @ytiq Commented Mar 29, 2021 at 21:17
  • should work. you can test it in anonymous Apex in Dev Console on somewhere else
    – ytiq
    Commented Mar 29, 2021 at 21:19
  • I'm sorry for that many questions, but I'm new in the development world. How could I test it to see the json that is coming back from Twilio? Commented Mar 29, 2021 at 21:21
  • I updated the code for you to test wit your creds
    – ytiq
    Commented Mar 29, 2021 at 21:21
  • you should get formatted JSON for your existing account
    – ytiq
    Commented Mar 29, 2021 at 21:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .