'Twilio Call Recorder

I am trying to work with the Twilio API to receive a text for each voicemail. I found that they had a tutorial about it. I tried following it, but I am getting errors and unable to run the code.

The errors and in the following lines:

  • new TwilioRestClient
  • .getMessageFactorty
  • TwilioRestException
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import com.twilio.rest.*;


import java.util.ArrayList;
import java.util.List;
import com.twilio.Twilio;
import com.twilio.http.TwilioRestClient;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
import io.github.cdimascio.dotenv.Dotenv;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.logging.log4j.message.MessageFactory;

public class CallRecorder extends HttpServlet {

    public static final String ACCOUNT_SID = "TWILIO_ACCOUNT_SID";
    public static final String AUTH_TOKEN = "TWILIO_AUTH_TOKEN";
    public static final String YOUR_NUMBER = "MY_CELLPHONE_NUMBER";
    public static final String TWILIO_NUMBER = "TWILIO_NUMBER";

    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {

        sendSMS(request.getParameter("From"), request.getParameter("RecordingUrl"));
    }

    public void sendSMS(String from, String recordingLink) {
        try {
            TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

            // Build a filter for the MessageList
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("Body", 
                "You've received a voicemail from " + 
                from + 
                ". Use the following link to here this message: \n" +
                recordingLink
                )
            );
            params.add(new BasicNameValuePair("To", YOUR_NUMBER));
            params.add(new BasicNameValuePair("From", TWILIO_NUMBER));
         
            MessageFactory messageFactory = client.getAccountSid().getMessageFactory();
            Message message = messageFactory.create(params);
            System.out.println(message.getSid());
        }
        catch (TwilioRestException e) {
            System.out.println(e.getErrorMessage());
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source