skip to Main Content

I’m using the Twilio API to send an SMS from my Twilio number to my cell.

I have two files:
1. a JSP file with a form and a method call
2. a java class with the Twilio API that receives the method call from item 1 and does the sending sms part
(see the files below)

Why am I getting the exception A “From” phone number is required?

The program works, delivers correctly, tried many times. I’m not getting an error. It works. It’s just that exception that puzzles me. I have purchased a twilio number, so I’m not using the trial number.

There is a stack trace associated with it, but can post it later.

In smsParams.put(“From”, from) I tried replacing from directly with a cell number like this (used a valid No):
smsParams.put(“From”, “1231231234”);
and then I get an exception that goes to the next line (smsParams.put(“To”, from);) and says A “To” number is required. When I replace variable with a number it skips to the next line and gives an exception for the message body.

Thanks

//***********************************
//java class MyMessage
//***********************************

package package_sms;
import java.util.HashMap;
import java.util.Map;
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.SmsFactory;
import com.twilio.sdk.resource.instance.Account;


public class MyMessage {

/** The Constant ACCOUNT_SID. */
public static final String ACCOUNT_SID = "AC11d68faa7db85a48557aa33ae0b88261";

/** The Constant AUTH_TOKEN. */
public static final String AUTH_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";


public String to, from, text;

    public void provideNumbers(String to, String from, String text)
    {
        // Create a rest client
        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

        // Get the main account (The one we used to authenticate the client
        Account mainAccount = client.getAccount();

        // Send an sms
        SmsFactory smsFactory = mainAccount.getSmsFactory();
            Map<String, String> smsParams = new HashMap<String, String>();
        smsParams.put("From", from);    // Twillio No
        smsParams.put("To", to);        // target phone No
        smsParams.put("Body", text);    // message text
        try 
        {
            smsFactory.create(smsParams);
        } 
        catch (TwilioRestException e) 
        {
            e.printStackTrace();
        }   
    }



<% *****************************
// jsp file SendSms
***************************** %>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="package_sms.MyMessage"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>form</title>
</head>
    <body style="background-color:Azure;">
        <h3>Welcome to your text messenger</h3>
        <form action="SendSms.jsp" name="form1" method="POST" >
        From: <input type="text" name="phoneFrom" style="background-color: Beige;border:1px solid Black;"/><br><br> 
        To: <input type="text" name="phoneTo" style="background-color: Beige;border:1px solid Black;"/><br><br>
        Message text: <input type="text" name="messageText" style="background-color: Beige;border:1px solid Black;"/><br><br>
        <input type="submit" name="submit" value="Send Message" />
        </form><br>

        <%  /* Declared string variables that provide parameters to the 
               call to provideNumber method. provideNumbers method is located in MyMessage class.
               provideNumbers method is called on a 'message' object.  */

        String number_from = request.getParameter("phoneFrom");
        String number_to = request.getParameter("phoneTo");
        String messageBody = request.getParameter("messageText");

        MyMessage message = new MyMessage();
            message.provideNumbers(number_to, number_from, messageBody);
            %>

    </body>
</html>

2

Answers


  1. Despite you are including MyMessage in the JSP, you are not setting a value for from, to and message variables.
    Get them from the request and initialize those variables.

    I just saw that I did oversee something… in the end of your JSP, there’re assignments from the request and some logic on the JSP. I would suggest processing the request in the included class, as a bean, so you’re sure the process is done before “drawing” the page and hence, you’ll be able to show some process result.

    In your JSP, at the very beginning

    <jsp:useBean id="myBean" scope="request" class="package_sms.MyMessage">
       <% myBean.provideNumbers( request ); %>
    </jsp:useBean>
    

    … and in your class, something like this

    public void provideNumbers(HTTPServletRequest request)
    {
        String number_from = request.getParameter("phoneFrom");
        String number_to = request.getParameter("phoneTo");
        String messageBody = request.getParameter("messageText");
        ...
        smsParams.put("From", number_from);    // Twillio No
        smsParams.put("To", number_to);        // target phone No
        smsParams.put("Body", messageBody);    // message text
    

    In the class, you could set a Public String resultString with a message to be shown according to the result of the sending process, and, in the JSP, you could print it out with something like <%= myBean.resultString %>

    Login or Signup to reply.
  2. The error is in your JSP page. When the JSP is loaded it runs through executing all the code. Part of that code is you grabbing the inputs and calling your method.

    However when the file first loads there are no values in the form. Therefore the getParameter calls are getting null values. Therefore the “message.provideNumbers” method is passing your Java code nulls. This is turn means that when your Java code tries to call Twilio it is passing null values and an Exception is triggered.

    To correct this you could surround your JSP Java code in an IF statement checking that the variables have entries. For example:

    <% if (request.getParameter("phoneFrom") != null 
           && request.getParameter("phoneTo") != null 
           && request.getParameter("messageText") != null) {  
    
        String number_from = request.getParameter("phoneFrom");
        String number_to = request.getParameter("phoneTo");
        String messageBody = request.getParameter("messageText");
    
        MyMessage message = new MyMessage();
        message.provideNumbers(number_to, number_from, messageBody);
    }%>
    

    Hope that helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search