The Java client library makes it easy for developers to use SMSFactor's API.
In order to use it, make sure to have an account. You can register here. Once your account is created, you need to generate your first API token. You can find the complete documentation of our API here.
<dependency>
<groupId>com.smsfactor</groupId>
<artifactId>smsfactor</artifactId>
<version>1.0</version>
</dependency>
implementation 'com.smsfactor:smsfactor:1.0'
SMSFactorExample.java
import java.util.HashMap;
import java.util.Map;
import com.smsfactor.SMSFactor;
import com.smsfactor.model.Campaign;
import com.smsfactor.response.CampaignHistoryResponse;
import com.smsfactor.exception.SMSFactorException;
public class SMSFactorExample
{
public static void main(String[] args) {
try {
SMSFactor.apiToken = "your.token";
// Parameters
Map<String, Object> params = new HashMap<String, Object>();
params.put("length", 10);
CampaignHistoryResponse response = Campaign.history(params);
for(CampaignHistoryResponse.Campaign campaign:response.campaigns) {
System.out.println(campaign.id+" "+campaign.text+" "+campaign.date);
}
}
catch(SMSFactorException e) {
System.out.println(e.getMessage());
System.out.println("SMS Factor error code : " + e.getSMSFactorCode());
}
}
}
AccountCreditsResponse response = Account.credits();
AccountGetResponse response = Account.get();
AccountSubAccountsResponse response = Account.subAccounts();
// Params
Map<String, String> user = new HashMap<String, String>();
user.put("firstname", "George");
user.put("lastname", "Abitbol");
user.put("city", "Pluvigner");
user.put("phone", "0011223344");
user.put("address1", "3 avenue du président Coty");
user.put("zip", "56330");
user.put("country_code", "fr");
user.put("isChild", "0"); //Is the account a subaccount ?
user.put("unlimited", "0"); //If isChild, should the subaccount use the parent's credits
user.put("email", "g.abitbol+" + UUID.randomUUID().toString() + "@smsfactor.com");
user.put("password", "edR3meDeDn0m");
Map<String, Object> params = new HashMap<String, Object>();
params.put("account", user);
AccountCreateResponse response = Account.create(params);
// Params
Map<String, String> message = new HashMap<String, String>();
message.put("text", "test skd java");
message.put("pushtype", "alert"); //alert(default) or marketing
message.put("sender", "SDK"); //Optional
message.put("delay", "2019-06-08 10:21:15"); //Optional. Omit for immediate send
List<Map<String, String>> gsm = new ArrayList<Map<String, String>>();
gsm.add(
new HashMap<String, String>() {{
put("value", "33601000000");
}}
);
Map<String, List<Map<String, String>>> recipients = new HashMap<String, List<Map<String, String>>>();
recipients.put("gsm", gsm);
Map<String, Object> sms = new HashMap<String, Object>();
sms.put("message", message);
sms.put("recipients", recipients);
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("sms", sms);
CampaignSendResponse response = Campaign.send(payload, false); // True to simulate the campaign (no SMS sent)
// Params
Map<String, String> message = new HashMap<String, String>();
message.put("text", "test skd java");
message.put("pushtype", "alert"); //alert(default) or marketing
message.put("sender", "SDK"); //Optional
message.put("delay", "2019-06-08 10:21:15"); //Optional. Omit for immediate send
List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
lists.add(
new HashMap<String, String>() {{
put("value", "your_list_id");
}}
);
Map<String, Object> sms = new HashMap<String, Object>();
sms.put("message", message);
sms.put("lists", lists);
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("sms", sms);
CampaignSendResponse response = Campaign.sendToLists(payload, false); // True to simulate the campaign (no SMS sent)
Use the campaign ticket value returned by our API after sending a campaign to get information about that campaign. Given the last example :
CampaignGetResponse response = Campaign.get(ticket);
CampaignHistoryResponse response = Campaign.history(ticket);
Map<String, Object> length = new HashMap<String, Object>();
length.put("length", 5);
CampaignHistoryResponse response = Campaign.history(length); // Get the last 5 campaigns
You can customize each contact with up to 4 optional information
List<Map<String, String>> gsm = new ArrayList<Map<String, String>>();
gsm.add(
new HashMap<String, String>() {{
put("value", "33600000001");
put("info1", "Lastname");
put("info2", "Firstname");
put("info3", "Gender");
}}
);
gsm.add(
new HashMap<String, String>() {{
put("value", "33600000002");
put("info1", "Lastname");
put("info2", "Firstname");
put("info3", "Gender");
}}
);
Map<String, Object> contacts = new HashMap<String, Object>();
contacts.put("gsm", gsm);
Map<String, Object> object = new HashMap<String, Object>();
object.put("name", "sdk list");
object.put("contacts", contacts);
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("list", object);
ContactListCreateResponse response = ContactList.create(payload);
Integer list_id = response.id;
List<Map<String, String>> gsm = new ArrayList<Map<String, String>>();
gsm.add(
new HashMap<String, String>() {{
put("value", "33600000003");
put("info1", "Lastname");
put("info2", "Firstname");
put("info3", "Gender");
}}
);
gsm.add(
new HashMap<String, String>() {{
put("value", "33600000004");
put("info1", "Lastname");
put("info2", "Firstname");
put("info3", "Gender");
}}
);
Map<String, Object> contacts = new HashMap<String, Object>();
contacts.put("gsm", gsm);
Map<String, Object> object = new HashMap<String, Object>();
object.put("list", list_id);
object.put("contacts", contacts);
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("list", object);
ContactListAddContactsResponse response = ContactList.addContacts(payload);
ContactListRemoveContactResponse response = ContactList.removeContact(contact_id); // Use Get list to get contact id
ContactListGetResponse response = ContactList.get(list_id);
ContactListDeduplicateResponse response = ContactList.deduplicate(list_id);
ContactListAllResponse response = ContactList.all();
ContactListDeleteResponse response = ContactList.delete(list_id);
List<Map<String, String>> gsm = new ArrayList<Map<String, String>>();
gsm.add(
new HashMap<String, String>() {{
put("value", "33600000003");
}}
);
gsm.add(
new HashMap<String, String>() {{
put("value", "33600000004");
}}
);
Map<String, Object> contacts = new HashMap<String, Object>();
contacts.put("gsm", gsm);
Map<String, Object> object = new HashMap<String, Object>();
object.put("contacts", contacts);
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("blacklist", object);
ContactListAddToBlacklistResponse response = ContactList.addToBlacklist(payload);
ContactListBlacklistResponse response = ContactList.blacklist();
List<Map<String, String>> gsm = new ArrayList<Map<String, String>>();
gsm.add(
new HashMap<String, String>() {{
put("value", "33600000003");
}}
);
gsm.add(
new HashMap<String, String>() {{
put("value", "33600000004");
}}
);
Map<String, Object> contacts = new HashMap<String, Object>();
contacts.put("gsm", gsm);
Map<String, Object> object = new HashMap<String, Object>();
object.put("contacts", contacts);
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("npai", object);
ContactListAddToNpaiResponse response = ContactList.addToNpai(payload);
ContactListNpaiResponse response = ContactList.npai();
Map<String, String> token = new HashMap<String, String>();
token.put("name", "token sdk");
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("token", token);
TokenCreateResponse response = Token.create(payload);
Integer token_id = response.token_id;
TokenAllResponse response = Token.all();
TokenDeleteResponse response = Token.delete(token_id);
To see all available webhooks, please go to our official documentation.
Map<String, String> webhook = new HashMap<String, String>();
webhook.put("type", "DLR");
webhook.put("url", "https://yourserverurl.com");
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("webhook", webhook);
WebhookCreateResponse response = Webhook.create(payload);
Integer webhook_id = response.webhook.webhook_id
WebhookAllResponse response = Webhook.all();
Map<String, String> webhook = new HashMap<String, String>();
webhook.put("type", "MO");
webhook.put("url", "https://yourserverurl.net");
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("webhook", webhook);
WebhookUpdateResponse response = Webhook.update(webhook_id, payload);
WebhookDeleteResponse response = Webhook.delete(webhook_id);