diff --git a/.gitignore b/.gitignore index b1f3107..fa6e5d3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ .mtj.tmp/ # Package Files # -*.jar *.war *.ear diff --git a/README.md b/README.md new file mode 100644 index 0000000..41e88e7 --- /dev/null +++ b/README.md @@ -0,0 +1,182 @@ +# SerwerSMS.pl JAVA Client API +Klient JAVA do komunikacji zdalnej z API v2 SerwerSMS.pl + +Zalecane jest, aby komunikacja przez HTTPS API odbywała się z loginów utworzonych specjalnie do połączenia przez API. Konto użytkownika API można utworzyć w Panelu Klienta → Ustawienia interfejsów → HTTPS XML API → Użytkownicy. + +#### Przykładowe wywołanie +```java +import java.io.*; +import java.util.*; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import org.json.*; +import org.w3c.dom.*; +import org.xml.sax.InputSource; +import SerwerSMS.*; + +public class NewMain { + + public static void main(String[] args) { + + try { + + SerwerSMS SerwerSMSApi = new SerwerSMS("demo", "demo"); + + HashMap options = new HashMap(); + options.put("test", "true"); + options.put("details", "true"); + + String type = "json"; + SerwerSMSApi.setFormat(type); + String result = SerwerSMSApi.message.sendSms("500600700", "Test message", "", options); + + if (type == "xml") { + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(new InputSource(new StringReader(result))); + NodeList list = document.getElementsByTagName("item"); + + for (int temp = 0; temp < list.getLength(); temp++) { + + Node item = list.item(temp); + if (item.getNodeType() == Node.ELEMENT_NODE) { + + Element element = (Element) item; + String id = element.getElementsByTagName("id").item(0).getTextContent(); + String phone = element.getElementsByTagName("phone").item(0).getTextContent(); + String status = element.getElementsByTagName("status").item(0).getTextContent(); + + System.out.println(id + " - " + phone + " - " + status); + } + } + + } else if (type == "json") { + + JSONObject json = new JSONObject(result); + JSONArray jsonArray = new JSONArray(json.get("items").toString()); + + for (int i = 0; i < jsonArray.length(); i++) { + + JSONObject tmp = new JSONObject(jsonArray.get(i).toString()); + String id = tmp.get("id").toString(); + String phone = tmp.get("phone").toString(); + String status = tmp.get("status").toString(); + + System.out.println(id + " - " + phone + " - " + status); + } + } + + } catch (Exception e) { + System.out.println(e.getMessage()); + } + + } +} +``` + +#### Wysyłka SMS +```java +try { + + SerwerSMS SerwerSMSApi = new SerwerSMS("demo", "demo"); + String type = "json"; + SerwerSMSApi.setFormat(type); + + // SMS FULL + HashMap options = new HashMap(); + options.put("test", "true"); + options.put("details", "true"); + String result = SerwerSMSApi.message.sendSms("500600700,600700800", "Test message", "INFORMACJA", options); + System.out.println(result); + + // SMS ECO + HashMap options = new HashMap(); + options.put("test", "true"); + options.put("details", "true"); + String result = SerwerSMSApi.message.sendSms("500600700,600700800", "Test message", "", options); + System.out.println(result); + + // VOICE from text + HashMap options = new HashMap(); + options.put("test", "true"); + options.put("text", "Test message"); + options.put("details", "true"); + String result = SerwerSMSApi.message.sendVoice("500600700", options); + System.out.println(result); + + // MMS + HashMap options = new HashMap(); + options.put("test", "true"); + options.put("file_id", "82aa9f108c"); + String result = SerwerSMSApi.message.sendMms("500600700", "Title message", options); + System.out.println(result); + +} catch (Exception e) { + System.out.println(e.getMessage()); +} +``` + +#### Wysyłka spersonalizowanych SMS +```java +try { + + ArrayList> listMessages = new ArrayList>(); + + // Message nr. 1 + HashMap message = new HashMap(); + message.put("phone", "500600700"); + message.put("text", "Test message 1"); + listMessages.add(message); + + // Message nr. 2 + message = new HashMap(); + message.put("phone", "600700800"); + message.put("text", "Test message 2"); + listMessages.add(message); + + HashMap options = new HashMap(); + options.put("test", "true"); + options.put("details", "true"); + + String result = SerwerSMSApi.message.sendPersonalized(listMessages, "", options); + System.out.println(result); + +} catch (Exception e) { + System.out.println(e.getMessage()); +} +``` + +#### Pobieranie raportów doręczeń +```java +try { + + HashMap options = new HashMap(); + options.put("id", "b7ac0db51a,0a83b5d5eb"); + String result = SerwerSMSApi.message.reports(options); + System.out.println(result); + +} catch (Exception e) { + System.out.println(e.getMessage()); +} +``` + +#### Pobieranie wiadomości przychodzących +```java +try { + + HashMap options = new HashMap(); + options.put("phone", "500600700"); + String result = SerwerSMSApi.message.recived("ndi",options); + +} catch (Exception e) { + System.out.println(e.getMessage()); +} +``` +## Wymagania +Java SE 8 + +Biblioteka do obsługi formatu JSON lub XML + +## Dokumentacja +http://dev.serwersms.pl diff --git a/dist/SerwerSMS.jar b/dist/SerwerSMS.jar new file mode 100644 index 0000000..5f88ccc Binary files /dev/null and b/dist/SerwerSMS.jar differ diff --git a/src/SerwerSMS/Account.java b/src/SerwerSMS/Account.java new file mode 100644 index 0000000..6a27a90 --- /dev/null +++ b/src/SerwerSMS/Account.java @@ -0,0 +1,90 @@ +package SerwerSMS; + +import java.util.HashMap; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Account { + + private SerwerSMS master = null; + + public Account(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * Register new account + * + * @param array params + * @option String "phone" + * @option String "email" + * @option String "first_name" + * @option String "last_name" + * @option String "company" + * @return array + * @option bool "success" + */ + + public String add(HashMap params) { + + return master.send("account/add", params); + + } + + /** + * Return limits SMS + * + * @param array params + * @option boolean "show_type" + * @return array + * @option array "items" + * @option string "type" Type of message + * @option string "chars_limit" The maximum length of message + * @option string "value" Limit messages + * + */ + public String limits(HashMap params) { + + return master.send("account/limits", params); + + } + + /** + * Return contact details + * + * @return array + * @option String "telephone" + * @option String "email" + * @option String "form" + * @option String "faq" + * @option array "account_maintainer" + * @option String "name" + * @option String "email" + * @option String "telephone" + * @option String "photo" + */ + public String help() { + + return master.send("account/help", new HashMap()); + + } + + /** + * Return messages from the administrator + * + * @return array + * @option boolean "new" Marking unread message + * @option String "message" + */ + public String messages() { + + return master.send("account/messages", new HashMap()); + + } +} \ No newline at end of file diff --git a/src/SerwerSMS/Blacklist.java b/src/SerwerSMS/Blacklist.java new file mode 100644 index 0000000..53d73b4 --- /dev/null +++ b/src/SerwerSMS/Blacklist.java @@ -0,0 +1,97 @@ +package SerwerSMS; + +import java.io.IOException; +import java.util.HashMap; +import java.util.ArrayList; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Blacklist { + + private SerwerSMS master = null; + + public Blacklist(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * Add phone to the blacklist + * + * @param String phone + * @return array + * @option boolean "success" + * @option int "id" + */ + public String add(String phone) { + + HashMap params = new HashMap(); + params.put("phone", phone); + + return master.send("blacklist/add", params); + + } + + /** + * List of blacklist phones + * + * @param String phone + * @param array params + * @option int "page" The number of the displayed page + * @option int "limit" Limit items are displayed on the single page + * @return array + * @option array "paging" + * @option int "page" The number of current page + * @option int "count" The number of all pages + * @option array "items" + * @option String "phone" + * @option String "added" Date of adding phone + */ + public String index(String phone, HashMap params) { + + HashMap options = new HashMap(); + options.put("phone", phone); + + params.putAll(options); + + return master.send("blacklist/index", params); + + } + + /** + * Checking if phone is blacklisted + * + * @param String phone + * @return array + * @option boolean "exists" + */ + public String check(String phone) { + + HashMap params = new HashMap(); + params.put("phone", phone); + + return master.send("blacklist/check", params); + + } + + /** + * Deleting phone from the blacklist + * + * @param String phone + * @return array + * @option boolean "success" + */ + public String delete(String phone) { + + HashMap params = new HashMap(); + params.put("phone", phone); + + return master.send("blacklist/delete", params); + + } +} \ No newline at end of file diff --git a/src/SerwerSMS/Broadcaster.java b/src/SerwerSMS/Broadcaster.java new file mode 100644 index 0000000..3f83fce --- /dev/null +++ b/src/SerwerSMS/Broadcaster.java @@ -0,0 +1,56 @@ +package SerwerSMS; + +import java.util.HashMap; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Broadcaster { + + private SerwerSMS master = null; + + public Broadcaster(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * Creating new Sender name + * + * @param String name + * @return array + * @option booleanean "success" + */ + public String add(String name) { + + HashMap params = new HashMap(); + params.put("name", name); + + return master.send("senders/add", params); + + } + + /** + * Senders list + * + * @param array params + * @option bool "predefined" + * @option String "sort" Values: name + * @option String "order" Values: asc|desc + * @return array + * @option array "items" + * @option String "name" + * @option String "agreement" delivered|required|not_required + * @option String "status" + * pending_authorization|authorized|rejected|deactivated + */ + public String index(HashMap params) { + + return master.send("senders/index", params); + + } +} \ No newline at end of file diff --git a/src/SerwerSMS/Contact.java b/src/SerwerSMS/Contact.java new file mode 100644 index 0000000..a109dfc --- /dev/null +++ b/src/SerwerSMS/Contact.java @@ -0,0 +1,213 @@ +package SerwerSMS; + +import java.util.ArrayList; +import java.util.HashMap; +import org.json.JSONObject; +import org.json.JSONArray; + + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Contact { + + private SerwerSMS master = null; + + public Contact(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * Add new contact + * + * @param String group_id + * @param String phone + * @param array params + * @option String "email" + * @option String "first_name" + * @option String "last_name" + * @option String "company" + * @option String "tax_id" + * @option String "address" + * @option String "city" + * @option String "description" + * @return array + * @option boolean "success" + * @option int "id" + */ + public String add(String group_id, String phone, HashMap params) { + + HashMap options = new HashMap(); + options.put("group_id", group_id); + options.put("phone", phone); + + params.putAll(options); + + return master.send("contacts/add", params); + + } + + /** + * List of contacts + * + * @param String group_id + * @param String search + * @param array params + * @option int "page" The number of the displayed page + * @option int "limit" Limit items are displayed on the single page + * @option String "sort" Values: first_name|last_name|phone|company|tax_id|email|address|city|description + * @option String "order" Values: asc|desc + * @return array + * @option array "paging" + * @option int "page" The number of current page + * @option int "count" The number of all pages + * @options array "items" + * @option int "id" + * @option String "phone" + * @option String "email" + * @option String "company" + * @option String "first_name" + * @option String "last_name" + * @option String "tax_id" + * @option String "address" + * @option String "city" + * @option String "description" + * @option boolean "blacklist" + * @option int "group_id" + * @option String "group_name" + */ + public String index(String group_id, String search, HashMap params) { + + HashMap options = new HashMap(); + options.put("group_id", group_id); + options.put("search", search); + + params.putAll(options); + + return master.send("contacts/index", params); + + } + + /** + * View single contact + * + * @param int id + * @return array + * @option int "id" + * @option String "phone" + * @option String "email" + * @option String "company" + * @option String "first_name" + * @option String "last_name" + * @option String "tax_id" + * @option String "address" + * @option String "city" + * @option String "description" + * @option boolean "blacklist" + */ + public String view(String id) { + + HashMap params = new HashMap(); + params.put("id", id); + + return master.send("contacts/view", params); + + } + + + /** + * Editing a contact + * + * @param int id + * @param String group_id + * @param String phone + * @param array params + * @option String "email" + * @option String "first_name" + * @option String "last_name" + * @option String "company" + * @option String "tax_id" + * @option String "address" + * @option String "city" + * @option String "description" + * @return array + * @option boolean "success" + * @option int "id" + */ + public String edit(String id, String group_id, String phone, HashMap params) { + + HashMap options = new HashMap(); + options.put("id", id); + options.put("group_id", group_id); + options.put("phone", phone); + + params.putAll(options); + + return master.send("contacts/edit", params); + + } + + + /** + * Deleting a phone from contacts + * + * @param int id + * @return array + * @option boolean "success" + */ + public String delete(String id) { + + HashMap params = new HashMap(); + params.put("id", id); + + return master.send("contacts/delete", params); + + } + + /** + * Import contact list + * + * @param String group_name + * @param array contact + * @option String "phone" + * @option String "email" + * @option String "first_name" + * @option String "last_name" + * @option String "company" + * @return array + * @option boolean "success" + * @option int "id" + * @option int "correct" Number of contacts imported correctly + * @option int "failed" Number of errors + */ + public String imports(String group_name, ArrayList> contact) throws Exception { + + + HashMap params = new HashMap(); + params.put("group_name", group_name); + + JSONArray array = new JSONArray(); + + for (HashMap temp : contact) { + JSONObject json = new JSONObject(); + json.put("phone", temp.get("phone")); + json.put("email", temp.get("email")); + json.put("first_name", temp.get("first_name")); + json.put("last_name", temp.get("last_name")); + json.put("company", temp.get("company")); + array.put(json); + } + + params.put("contact", array.toString()); + + return master.send("contacts/import", params); + + } + + +} \ No newline at end of file diff --git a/src/SerwerSMS/Fault.java b/src/SerwerSMS/Fault.java new file mode 100644 index 0000000..8ac9dd0 --- /dev/null +++ b/src/SerwerSMS/Fault.java @@ -0,0 +1,35 @@ +package SerwerSMS; + +import java.util.HashMap; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Fault { + + private SerwerSMS master = null; + + public Fault(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * Preview error + * + * @param int code + * @return array + * @option int "code" + * @option String "type" + * @option String "message" + */ + public String view(int code) { + + return master.send("error/" + code, new HashMap()); + + } +} \ No newline at end of file diff --git a/src/SerwerSMS/File.java b/src/SerwerSMS/File.java new file mode 100644 index 0000000..6e4caa1 --- /dev/null +++ b/src/SerwerSMS/File.java @@ -0,0 +1,104 @@ +package SerwerSMS; + +import java.io.IOException; +import java.util.HashMap; +import java.util.ArrayList; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class File { + + private SerwerSMS master = null; + + public File(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * Add new file + * + * @param String type - mms|voice + * @param array params + * @option String "url" URL address to file + * @return array + * @option boolean "success" + * @option String "id" + */ + public String add(String type, HashMap params) { + + HashMap options = new HashMap(); + options.put("type", type); + + params.putAll(options); + + return master.send("files/add", params); + + } + + /** + * List of files + * + * @param String type - mms|voice + * @return array + * @option array "items" + * @option String "id" + * @option String "name" + * @option int "size" + * @option String "type" - mms|voice + * @option String "date" + */ + public String index(String type) { + + HashMap params = new HashMap(); + params.put("type", type); + + return master.send("files/index", params); + + } + + /** + * View file + * + * @param String id + * @param String type - mms|voice + * @return array + * @option String "id" + * @option String "name" + * @option int "size" + * @option String "type" - mms|voice + * @option String "date" + */ + public String view(String id, String type) { + + HashMap params = new HashMap(); + params.put("id", id); + params.put("type", type); + + return master.send("files/view", params); + + } + + /** + * Deleting a file + * + * @param String id + * @param String type - mms|voice + * @return array + * @option boolean "success" + */ + public String delete(String id, String type) { + + HashMap params = new HashMap(); + params.put("id", id); + params.put("type", type); + + return master.send("files/delete", params); + + } +} \ No newline at end of file diff --git a/src/SerwerSMS/Group.java b/src/SerwerSMS/Group.java new file mode 100644 index 0000000..96b8d25 --- /dev/null +++ b/src/SerwerSMS/Group.java @@ -0,0 +1,138 @@ +package SerwerSMS; + +import java.util.HashMap; +import java.util.ArrayList; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Group { + + private SerwerSMS master = null; + + public Group(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * Add new group + * + * @param String name + * @return array + * @option booleanean "success" + * @option int "id" + */ + public String add(String name) { + + HashMap params = new HashMap(); + params.put("name", name); + + return master.send("groups/add", params); + + } + + /** + * List of group + * + * @param String search Group name + * @param array params + * @option int "page" The number of the displayed page + * @option int "limit" Limit items are displayed on the single page + * @option String "sort" Values: name + * @option String "order" Values: asc|desc + * @return array + * @option array "paging" + * @option int "page" The number of current page + * @option int "count" The number of all pages + * @option array "items" + * @option int "id" + * @option String "name" + * @option int "count" Number of contacts in the group + */ + public String index(String search, HashMap params) { + + HashMap options = new HashMap(); + params.put("search", search); + + params.putAll(options); + + return master.send("groups/index", params); + + } + + /** + * View single group + * + * @param int id + * @return array + * @option int "id" + * @option String "name" + * @option int "count" Number of contacts in the group + */ + public String view(String id) { + + HashMap params = new HashMap(); + params.put("id", id); + + return master.send("groups/view", params); + + } + + /** + * Editing a group + * + * @param String id + * @param String name + * @return array + * @option booleanean "success" + * @option int "id" + */ + public String edit(String id, String name) { + + HashMap params = new HashMap(); + params.put("id", id); + params.put("name", name); + + return master.send("groups/edit", params); + + } + + /** + * Deleting a group + * + * @param int id + * @return array + * @option booleanean "success" + */ + public String delete(String id) { + + HashMap params = new HashMap(); + params.put("id", id); + + return master.send("groups/delete", params); + + } + + /** + * Viewing a groups containing phone + * + * @param String phone + * @return array + * @option int "id" + * @option int "group_id" + * @option String "group_name" + */ + public String check(String phone) { + + HashMap params = new HashMap(); + params.put("phone", phone); + + return master.send("groups/check", params); + + } +} \ No newline at end of file diff --git a/src/SerwerSMS/Message.java b/src/SerwerSMS/Message.java new file mode 100644 index 0000000..ba6a77c --- /dev/null +++ b/src/SerwerSMS/Message.java @@ -0,0 +1,418 @@ +package SerwerSMS; + +import java.util.HashMap; +import java.util.ArrayList; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Message { + + private SerwerSMS master = null; + + public Message(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * Sending messages + * + * @param String phone + * @param String text Message + * @param String sender Sender name only for FULL SMS + * @param array params + * @option String "details" Show details of messages + * @option String "utf" Change encoding to UTF-8 (Only for FULL SMS) + * @option String "flash" + * @option String "speed" Priority canal only for FULL SMS + * @option String "test" Test mode + * @option String "vcard" vCard message + * @option String "wap_push" WAP Push URL address + * @option String "date" Set the date of sending + * @option String "group_id" Sending to the group instead of a phone number + * @option String "contact_id" Sending to phone from contacts + * @option String "unique_id" Own identifiers of messages + * @return array + * @option boolean "success" + * @option int "queued" Number of queued messages + * @option int "unsent" Number of unsent messages + * @option array "items" + * @option String "id" + * @option String "phone" + * @option String "status" - queued|unsent + * @option String "queued" Date of enqueued + * @option int "parts" Number of parts a message + * @option int "error_code" + * @option String "error_message" + * @option String "text" + */ + public String sendSms(String phone, String text, String sender, HashMap params) { + + HashMap options = new HashMap(); + options.put("phone", phone); + options.put("text", text); + options.put("sender", sender); + + params.putAll(options); + + return master.send("messages/send_sms", params); + + } + + /** + * Sending personalized messages + * + * @param array messages + * @option String "phone" + * @option String "text" + * @param String sender Sender name only for FULL SMS + * @param array params + * @option String "details" Show details of messages + * @option String "utf" Change encoding to UTF-8 (only for FULL SMS) + * @option String "flash" + * @option String "speed" Priority canal only for FULL SMS + * @option String "test" Test mode + * @option String "date" Set the date of sending + * @option String "group_id" Sending to the group instead of a phone number + * @option String "text" Message if is set group_id + * @option String|array "uniqe_id" Own identifiers of messages + * @option String "voice" Send VMS + * @return array + * @option boolean "success" + * @option int "queued" Number of queued messages + * @option int "unsent" Number of unsent messages + * @option array "items" + * @option String "id" + * @option String "phone" + * @option String "status" - queued|unsent + * @option String "queued" Date of enqueued + * @option int "parts" Number of parts a message + * @option int "error_code" + * @option String "error_message" + * @option String "text" + */ + public String sendPersonalized(ArrayList> messages, String sender, HashMap params) { + + HashMap options = new HashMap(); + options.put("sender", sender); + + StringBuffer messageList = new StringBuffer(); + + for (HashMap temp : messages) { + messageList.append(temp.get("phone")); + messageList.append(":"); + messageList.append(temp.get("text")); + messageList.append("]|["); + } + + options.put("messages", messageList.toString()); + + params.putAll(options); + + return master.send("messages/send_personalized", params); + + } + + /** + * Sending Voice message + * + * @param String phone + * @param array params + * @option String "text" If send of text to voice + * @option String "file_id" ID from wav files + * @option String "date" Set the date of sending + * @option String "test" Test mode + * @option String "group_id" Sending to the group instead of a phone number + * @option String "contact_id" Sending to phone from contacts + * @return array + * @option boolean "success" + * @option int "queued" Number of queued messages + * @option int "unsent" Number of unsent messages + * @option array "items" + * @option String "id" + * @option String "phone" + * @option String "status" - queued|unsent + * @option String "queued" Date of enqueued + * @option int "parts" Number of parts a message + * @option int "error_code" + * @option String "error_message" + * @option String "text" + */ + public String sendVoice(String phone, HashMap params) { + + HashMap options = new HashMap(); + options.put("phone", phone); + + params.putAll(options); + + return master.send("messages/send_voice", params); + + } + + /** + * Sending MMS + * + * @param String phone + * @param String title Title of message (max 40 chars) + * @param array params + * @option String "file_id" + * @option String "file" File in base64 encoding + * @option String "date" Set the date of sending + * @option boolean "test" Test mode + * @option String "group_id" Sending to the group instead of a phone number + * @return array + * @option boolean "success" + * @option int "queued" Number of queued messages + * @option int "unsent" Number of unsent messages + * @option String "items" + * @option String "id" + * @option String "phone" + * @option String "status" - queued|unsent + * @option String "queued" Date of enqueued + * @option int "parts" Number of parts a message + * @option int "error_code" + * @option String "error_message" + * @option String "text" + */ + public String sendMms(String phone, String title, HashMap params) { + + HashMap options = new HashMap(); + options.put("phone", phone); + options.put("title", title); + + params.putAll(options); + + return master.send("messages/send_mms", params); + + } + + /** + * View single message + * + * @param String id + * @param array params + * @option String "unique_id" + * @option boolean "show_contact" Show details of the recipient from the contacts + * @return array + * @option String "id" + * @option String "phone" + * @option String "status" - delivered: The message is sent and delivered - + * undelivered: The message is sent but not delivered - sent: The message is + * sent and waiting for report - unsent: The message wasn't sent - + * in_progress: The message is queued for sending - saved: The message was + * saved in schedule + * @option String "queued" Date of enqueued + * @option String "sent" Date of sending + * @option String "delivered" Date of deliver + * @option String "sender" + * @option String "type" - eco|full|mms|voice + * @option String "text" + * @option String "reason" - message_expired - unsupported_number - + * message_rejected - missed_call - wrong_number - limit_exhausted - + * lock_send - wrong_message - operator_error - wrong_sender_name - + * number_is_blacklisted - sending_to_foreign_networks_is_locked - + * no_permission_to_send_messages - other_error + * @option array "contact" + * @option String "first_name" + * @option String "last_name" + * @option String "company" + * @option String "phone" + * @option String "email" + * @option String "tax_id" + * @option String "city" + * @option String "address" + * @option String "description" + */ + public String view(String id, HashMap params) { + + HashMap options = new HashMap(); + options.put("id", id); + + params.putAll(options); + + return master.send("messages/view", params); + + } + + /** + * Checking messages reports + * + * @param array params + * @option String "id" ID message + * @option String "unique_id" ID message + * @option String "phone" + * @option String "date_from" The scope of the initial + * @option String "date_to" The scope of the final + * @option String "status" delivered|undelivered|pending|sent|unsent + * @option String "type" eco|full|mms|voice + * @option int "stat_id" Id package messages + * @option boolean "show_contact" Show details of the recipient from the contacts + * @option int "page" The number of the displayed page + * @option int "limit" Limit items are displayed on the single page + * @option String "order" asc|desc + * @return array + * @option array "paging" + * @option int "page" The number of current page + * @option int "count" The number of all pages + * @option array items + * @option String "id" + * @option String "phone" + * @option String "status" - delivered: The message is sent and delivered - + * undelivered: The message is sent but not delivered - sent: The message is + * sent and waiting for report - unsent: The message wasn't sent - + * in_progress: The message is queued for sending - saved: The message was + * saved in the scheduler + * @option String "queued" Date of enqueued + * @option String "sent" Date of sending + * @option String "delivered" Date of deliver + * @option String "sender" + * @option String "type" - eco|full|mms|voice + * @option String "text" + * @option boolean "flash" + * @option boolean "utf" + * @option int "parts" + * @option float "cost" + * @option String "method" + * @option int "mnc" + * @option String "country" + * @option String "network" + * @option array "attachments" + * @option int "id" + * @option String "reason" - message_expired - unsupported_number - + * message_rejected - missed_call - wrong_number - limit_exhausted - + * lock_send - wrong_message - operator_error - wrong_sender_name - + * number_is_blacklisted - sending_to_foreign_networks_is_locked - + * no_permission_to_send_messages - other_error + * @option array "contact" + * @option int "id" + * @option String "first_name" + * @option String "last_name" + * @option String "company" + * @option String "phone" + * @option String "email" + * @option String "tax_id" + * @option String "city" + * @option String "address" + * @option String "description" + */ + public String reports(HashMap params) { + + return master.send("messages/reports", params); + + } + + /** + * Deleting message from the scheduler + * + * @param String id + * @param String unique_id + * @return array + * @option boolean "success" + */ + public String delete(String id, String unique_id) { + + HashMap params = new HashMap(); + params.put("id", id); + params.put("unique_id", unique_id); + + return master.send("messages/delete", params); + + } + + /** + * List of received messages + * + * @param String type - eco SMS ECO replies - nd Incoming messages to ND number - ndi Incoming messages to ND number - mms Incoming MMS + * @param array params + * @option String "ndi" Filtering by NDI + * @option String "phone" Filtering by phone + * @option String "date_from" The scope of the initial + * @option String "date_to" The scope of the final + * @option boolean "read" Mark as read + * @option int "page" The number of the displayed page + * @option int "limit" Limit items are displayed on the single page + * @option String "order" asc|desc + * @return array + * @option array "paging" + * @option int "page" The number of current page + * @option int "count" The number of all pages + * @option array "items" + * @option int "id" + * @option String "type" eco|nd|ndi|mms + * @option String "phone" + * @option String "recived" Date of received message + * @option String "message_id" ID of outgoing message (only for ECO SMS) + * @option boolean "blacklist" Is the phone is blacklisted? + * @option String "text" Message + * @option String "to_number" Number of the recipient (for MMS) + * @option String "title" Title of message (for MMS) + * @option array "attachments" (for MMS) + * @option int "id" + * @option String "name" + * @option String "content_type" + * @option String "data" File + * @option array "contact" + * @option String "first_name" + * @option String "last_name" + * @option String "company" + * @option String "phone" + * @option String "email" + * @option String "tax_id" + * @option String "city" + * @option String "address" + * @option String "description" + */ + public String recived(String type, HashMap params) { + + HashMap options = new HashMap(); + options.put("type", type); + + params.putAll(options); + + return master.send("messages/recived", params); + + } + + /** + * Sending a message to an ND/SC + * + * @param String phone Sender phone number + * @param String text Message + * @return array + * @option boolean "success" + */ + public String sendNd(String phone, String text) { + + HashMap params = new HashMap(); + params.put("phone", phone); + params.put("text", text); + + return master.send("messages/send_nd", params); + + } + + /** + * Sending a message to an NDI/SCI + * + * @param String phone Sender phone number + * @param String text Message + * @param String ndi_number Recipient phone number + * @return array + * @option boolean "success" + */ + public String sendNdi(String phone, String text, String ndi_number) { + + HashMap params = new HashMap(); + params.put("phone", phone); + params.put("text", text); + params.put("ndi_number", ndi_number); + + return master.send("messages/send_ndi", params); + + } +} diff --git a/src/SerwerSMS/Payment.java b/src/SerwerSMS/Payment.java new file mode 100644 index 0000000..d0c13c2 --- /dev/null +++ b/src/SerwerSMS/Payment.java @@ -0,0 +1,76 @@ +package SerwerSMS; + +import java.util.HashMap; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Payment { + + private SerwerSMS master = null; + + public Payment(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * List of payments + * + * @return array + * @option array "items" + * @option int "id" + * @option String "number" + * @option String "state" paid|not_paid + * @option float "paid" + * @option float "total" + * @option String "payment_to" + * @option String "url" + */ + public String index() { + + return master.send("payments/index", new HashMap()); + + } + + /** + * View single payment + * + * @param String id + * @return array + * @option int "id" + * @option String "number" + * @option String "state" paid|not_paid + * @option float "paid" + * @option float "total" + * @option String "payment_to" + * @option String "url" + */ + public String view(String id) { + + HashMap params = new HashMap(); + params.put("id", id); + + return master.send("payments/view", params); + + } + + /** + * Download invoice as PDF + * + * @param String id + * @return byte[] + */ + public byte[] invoice(String id) { + + HashMap params = new HashMap(); + params.put("id", id); + + return master.sendByte("payments/invoice", params); + + } +} \ No newline at end of file diff --git a/src/SerwerSMS/Phone.java b/src/SerwerSMS/Phone.java new file mode 100644 index 0000000..3d036c9 --- /dev/null +++ b/src/SerwerSMS/Phone.java @@ -0,0 +1,60 @@ +package SerwerSMS; + +import java.util.HashMap; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Phone { + + private SerwerSMS master = null; + + public Phone(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * Checking phone in to HLR + * + * @param String phone + * @param String id Query ID returned if the processing takes longer than 60 + * seconds + * @return array + * @option String "phone" + * @option String "status" + * @option int "imsi" + * @option String "network" + * @option boolean "ported" + * @option String "network_ported" + */ + public String check(String phone, String id) { + + HashMap params = new HashMap(); + params.put("phone", phone); + params.put("id", id); + + return master.send("phones/check", params); + + } + + /** + * Validating phone number + * + * @param String phone + * @return array + * @option boolean "correct" + */ + public String test(String phone) { + + HashMap params = new HashMap(); + params.put("phone", phone); + + return master.send("phones/test", params); + + } +} \ No newline at end of file diff --git a/src/SerwerSMS/Premium.java b/src/SerwerSMS/Premium.java new file mode 100644 index 0000000..a6caaea --- /dev/null +++ b/src/SerwerSMS/Premium.java @@ -0,0 +1,80 @@ +package SerwerSMS; + +import java.util.HashMap; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Premium { + + private SerwerSMS master = null; + + public Premium(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * List of received SMS Premium + * + * @return array + * @option array "items" + * @option int "id" + * @option String "to_number" Premium number + * @option String "from_number" Sender phone number + * @option String "date" + * @option int "limit" Limitation the number of responses + * @option String "text" Message + */ + public String index() { + + return master.send("premium/index", new HashMap()); + + } + + /** + * Sending replies for received SMS Premium + * + * @param String phone + * @param String text Message + * @param String gate Premium number + * @param int id ID received SMS Premium + * @return array + * @option booleanean "success" + */ + public String send(String phone, String text, String gate, String id) { + + HashMap params = new HashMap(); + params.put("phone", phone); + params.put("text", text); + params.put("gate", gate); + params.put("id", id); + + return master.send("premium/send", params); + + } + + /** + * View quiz results + * + * @param String id + * @return array + * @option int "id" + * @option String "name" + * @option array "items" + * @option int "id" + * @option int "count" Number of response + */ + public String quiz(String id) { + + HashMap params = new HashMap(); + params.put("id", id); + + return master.send("quiz/view", params); + + } +} \ No newline at end of file diff --git a/src/SerwerSMS/SerwerSMS.java b/src/SerwerSMS/SerwerSMS.java new file mode 100644 index 0000000..0ac8463 --- /dev/null +++ b/src/SerwerSMS/SerwerSMS.java @@ -0,0 +1,182 @@ +package SerwerSMS; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.HashMap; +import java.util.Iterator; +import org.json.*; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class SerwerSMS { + + private String username = ""; + private String password = ""; + private String api = "https://api2.serwersms.pl/"; + private String format = "json"; + private static HttpURLConnection http; + + public Message message = null; + public File file = null; + public Blacklist blacklist = null; + public Fault fault = null; + public Stat stat = null; + public Phone phone = null; + public Account account = null; + public Contact contact = null; + public Group group = null; + public Broadcaster broadcaster = null; + public Premium premium = null; + public Template template = null; + public Subaccount subaccount = null; + public Payment payment = null; + + public SerwerSMS(String user, String pass) throws Exception { + + if (user.isEmpty() || pass.isEmpty()) { + throw new Exception("Brak danych autoryzacyjnych"); + } + + username = user; + password = pass; + + message = new Message(this); + file = new File(this); + blacklist = new Blacklist(this); + fault = new Fault(this); + stat = new Stat(this); + phone = new Phone(this); + account = new Account(this); + contact = new Contact(this); + group = new Group(this); + broadcaster = new Broadcaster(this); + premium = new Premium(this); + template = new Template(this); + subaccount = new Subaccount(this); + payment = new Payment(this); + + } + + public String send(String action, HashMap params) { + + StringBuilder response = new StringBuilder(); + + try { + + params.put("username", username); + params.put("password", password); + + request(action, params); + + InputStream inputStream = null; + if (http != null) { + + inputStream = http.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + String line = ""; + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + http.disconnect(); + } + + return response.toString(); + + } catch (Exception e) { + + return ""; + + } + } + + public byte[] sendByte(String action, HashMap params) { + + try { + + params.put("username", username); + params.put("password", password); + + request(action, params); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + InputStream inputStream = null; + if (http != null) { + + inputStream = http.getInputStream(); + + byte[] ba1 = new byte[1024]; + int length; + + while ((length = inputStream.read(ba1)) != -1) { + bos.write(ba1, 0, length); + } + + http.disconnect(); + } + + return bos.toByteArray(); + + } catch (Exception e) { + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + return bos.toByteArray(); + + } + } + + public boolean setFormat(String type) { + + type = type.toLowerCase(); + + if (type == "xml" || type == "json") { + format = type; + return true; + } + + return false; + } + + private void request(String action, HashMap params) throws Exception { + + action += "." + format; + + String requestURL = ""; + requestURL = requestURL.concat(api); + requestURL = requestURL.concat(action); + + URL url = new URL(requestURL); + http = (HttpURLConnection) url.openConnection(); + + http.setUseCaches(false); + http.setDoInput(true); + http.setRequestMethod("POST"); + http.setRequestProperty("Content-Type", "application/json"); + + JSONObject jsonParam = new JSONObject(); + + if (params != null && params.size() > 0) { + + http.setDoOutput(true); + + Iterator paramIterator = params.keySet().iterator(); + while (paramIterator.hasNext()) { + String key = paramIterator.next(); + String value = params.get(key); + jsonParam.put(key, value); + } + + String requestString = jsonParam.toString(); + + OutputStreamWriter writer = new OutputStreamWriter(http.getOutputStream()); + writer.write(requestString); + writer.flush(); + } + + } +} diff --git a/src/SerwerSMS/Stat.java b/src/SerwerSMS/Stat.java new file mode 100644 index 0000000..454e74f --- /dev/null +++ b/src/SerwerSMS/Stat.java @@ -0,0 +1,46 @@ +package SerwerSMS; + +import java.util.HashMap; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Stat { + + private SerwerSMS master = null; + + public Stat(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * Statistics an sending + * + * @param array params + * @option String "type" eco|full|voice|mms + * @option String "begin" Start date + * @option String "end" End date + * @return array + * @option array "items" + * @option int "id" + * @option String "name" + * @option int "delivered" + * @option int "pending" + * @option int "undelivered" + * @option int "unsent" + * @option String "begin" + * @option String "end" + * @option String "text" + * @option String "type" eco|full|voice|mms + */ + public String index(HashMap params) { + + return master.send("stats/index", params); + + } +} \ No newline at end of file diff --git a/src/SerwerSMS/Subaccount.java b/src/SerwerSMS/Subaccount.java new file mode 100644 index 0000000..4216d18 --- /dev/null +++ b/src/SerwerSMS/Subaccount.java @@ -0,0 +1,117 @@ +package SerwerSMS; + +import java.util.HashMap; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Subaccount { + + private SerwerSMS master = null; + + public Subaccount(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * Creating new subaccount + * + * @param String subaccount_username + * @param String subaccount_password + * @param String subaccount_id Subaccount ID, which is template of powers + * @param array params + * @option String "name" + * @option String "phone" + * @option String "email" + * @return type + */ + public String add(String subaccount_username, String subaccount_password, String subaccount_id, HashMap params) { + + HashMap options = new HashMap(); + options.put("subaccount_username", subaccount_username); + options.put("subaccount_password", subaccount_password); + options.put("subaccount_id", subaccount_id); + + params.putAll(options); + + return master.send("subaccounts/add", params); + + } + + /** + * List of subaccounts + * + * @return array + * @option array "items" + * @option int "id" + * @option String "username" + */ + public String index() { + + return master.send("subaccounts/index", new HashMap()); + + } + + /** + * View details of subaccount + * + * @param String id + * @return array + * @option int "id" + * @option String "username" + * @option String "name" + * @option String "phone" + * @option String "email" + */ + public String view(String id) { + + HashMap params = new HashMap(); + params.put("id", id); + + return master.send("subaccounts/view", params); + + } + + /** + * Setting the limit on subaccount + * + * @param String id + * @param String type Message type: eco|full|voice|mms|hlr + * @param String value + * @return array + * @option booleanean "success" + * @option int "id" + */ + public String limit(String id, String type, String value) { + + HashMap params = new HashMap(); + params.put("id", id); + params.put("type", type); + params.put("value", value); + + return master.send("subaccounts/limit", params); + + } + + /** + * Deleting a subaccount + * + * @param String id + * @return array + * @option booleanean "success" + */ + public String delete(String id) { + + HashMap params = new HashMap(); + params.put("id", id); + + return master.send("subaccounts/delete", params); + + } + +} \ No newline at end of file diff --git a/src/SerwerSMS/Template.java b/src/SerwerSMS/Template.java new file mode 100644 index 0000000..e129174 --- /dev/null +++ b/src/SerwerSMS/Template.java @@ -0,0 +1,95 @@ +package SerwerSMS; + +import java.util.HashMap; + +/** + * + * @author SerwerSMS + * @version: 1.0 + * @date 2016-01 + */ +public class Template { + + private SerwerSMS master = null; + + public Template(SerwerSMS object) throws Exception { + + master = object; + + } + + /** + * List of templates + * + * @param array params + * @option String "sort" Values: name + * @option String "order" Values: asc|desc + * @return array + * @option array "items" + * @option int "id" + * @option String "name" + * @option String "text" + */ + public String index(HashMap params) { + + return master.send("templates/index", params); + + } + + /** + * Adding new template + * + * @param String name + * @param String text + * @return array + * @option array + * @option booleanean "success" + * @option int "id" + */ + public String add(String name, String text) { + + HashMap params = new HashMap(); + params.put("name", name); + params.put("text", text); + + return master.send("templates/add", params); + + } + + /** + * Editing a template + * + * @param String id + * @param String name + * @param String text + * @return array + * @option booleanean "success" + * @option int "id" + */ + public String edit(String id, String name, String text) { + + HashMap params = new HashMap(); + params.put("id", id); + params.put("name", name); + params.put("text", text); + + return master.send("templates/edit", params); + + } + + /** + * Deleting a template + * + * @param String id + * @return array + * @option booleanean "success" + */ + public String delete(String id) { + + HashMap params = new HashMap(); + params.put("id", id); + + return master.send("templates/delete", params); + + } +} \ No newline at end of file