function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kamal ThakurKamal Thakur 

Discover Lightning Actions : Integrate Third-Party APIs TwilioRestClient Error?

I reached at this trailhead for sending SMS through Twilio. But I am Getting These Errors while saving the Controller. 
  1. Invalid type: TwilioRestClient
  2. Invalid type: TwilioSMS
  3. Variable does not exist: sms
Best Answer chosen by Kamal Thakur
Aman MalikAman Malik
Hi Kamal,
In the TwilioSendSMSController, rather than using the default client credentials, you should use your own. Goto Twilio Website and signup for a free account. Generate Account Token and Access Token and replace it in your code.
 
global class TwilioSendSMSController {
   @AuraEnabled
      webService static String sendMessages(String mobNumber, String message, Id contactId) {
       
         if (mobNumber == null) {
            mobNumber = getPhoneNumber(contactId);
         }
 
        String account = 'YOUR_ACCOUNT_TOKEN_SRING';
  String token = 'YOUR_TOKEN_STRING';
         try {
            TwilioRestClient client = new TwilioRestClient(account, token);

            Map<String,String> params = new Map<String,String> {
               'To' => mobNumber,
               'From' => '+12156318540',
               'Body' => message
               };
            TwilioSMS sms = client.getAccount().getSMSMessages().create(params);
            return sms.getStatus();
         } catch(exception ex) {
            return 'failed';
         }
      }

      @AuraEnabled
      public static String getPhoneNumber(Id contactId) {
         Contact currentRecord = [SELECT Phone FROM Contact WHERE Id = :contactId];
         return currentRecord.Phone.replace(' ', '').replace('-', '').replace(')', '').replace('(', '');
   }
}
Thanks,
Aman
 

All Answers

Aman MalikAman Malik
Hi Kamal,
In the TwilioSendSMSController, rather than using the default client credentials, you should use your own. Goto Twilio Website and signup for a free account. Generate Account Token and Access Token and replace it in your code.
 
global class TwilioSendSMSController {
   @AuraEnabled
      webService static String sendMessages(String mobNumber, String message, Id contactId) {
       
         if (mobNumber == null) {
            mobNumber = getPhoneNumber(contactId);
         }
 
        String account = 'YOUR_ACCOUNT_TOKEN_SRING';
  String token = 'YOUR_TOKEN_STRING';
         try {
            TwilioRestClient client = new TwilioRestClient(account, token);

            Map<String,String> params = new Map<String,String> {
               'To' => mobNumber,
               'From' => '+12156318540',
               'Body' => message
               };
            TwilioSMS sms = client.getAccount().getSMSMessages().create(params);
            return sms.getStatus();
         } catch(exception ex) {
            return 'failed';
         }
      }

      @AuraEnabled
      public static String getPhoneNumber(Id contactId) {
         Contact currentRecord = [SELECT Phone FROM Contact WHERE Id = :contactId];
         return currentRecord.Phone.replace(' ', '').replace('-', '').replace(')', '').replace('(', '');
   }
}
Thanks,
Aman
 
This was selected as the best answer
Kamal ThakurKamal Thakur
@Aman, it solved the problem. Don't know why the default is not working. 
Philip BonthaPhilip Bontha
Hi Aman,

I used my Account Token and Access Token Still I am getting the same error.
Could you please help me.

Thanks in advance.