Reputation: 151
I am working on Java Web application. I have to send sms using Twilio sms api through this application. Here is the sample code which I am using.
public class Example {
public static final String ACCOUNT_SID = "TWILIO_ACCOUNT_SID";
public static final String AUTH_TOKEN = "TWILIO_AUTH_TOKEN";
public static void main(String[]args) throws TwilioRestException {
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("From", "twilioNumber"));
MessageFactory messageFactory = client.getAccount().getMessageFactory();
Message message = messageFactory.create(params);
}
}
I have added all the credentials in the respective field ACCOUNT_SID, AUTH_TOKEN and twilioNumber. But this code throws exception as
Exception in thread "main" com.twilio.sdk.TwilioRestException: A 'To' phone number is required.
at com.twilio.sdk.TwilioRestException.parseResponse(TwilioRestException.java:74)
at com.twilio.sdk.TwilioClient.safeRequest(TwilioClient.java:497)
at com.twilio.sdk.resource.list.MessageList.create(MessageList.java:70)
at com.twilio.Example.main(Example.java:54)
I am unable to figure out what should be given in 'To' phone number as I want to send sms through my web application and not through a phone number. Please guide me how to proceed. Thanks in advance for your help.
Upvotes: 1
Views: 590
Reputation: 151
It works now. I have implemented using BasicNameValuePair. I have added Twilio number in 'From' parameter and a verified number from Twilio as 'To' parameter. I am posting sample working code below if it can help someone.
public class Example {
public static final String ACCOUNT_SID = "TWILIO_ACCOUNT_SID";
public static final String AUTH_TOKEN = "TWILIO_AUTH_TOKEN";
public static void main(String[]args) throws TwilioRestException {
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("To", "To_number"));
params.add(new BasicNameValuePair("From", "Twilio_number"));
params.add(new BasicNameValuePair("Body", "Sent from Twilio!"));
MessageFactory messageFactory = client.getAccount().getMessageFactory();
Message message = messageFactory.create(params);
try {
Message sms = messageFactory.create(params);
} catch (TwilioRestException e) {
System.out.println("Inside exception!!");
}
}
}
Thanks for your help.
Upvotes: 0
Reputation: 171
I dont know the API but it seems like you just have to add a param like
new BasicNameValuePair("To", "receivernumber");
the following tutorial is in c# wich has nearly equal Syntax, so maybe it helps http://www.markhagan.me/Samples/Receive_SMS_Text_Using_Twilio_ASPNet
Upvotes: 1