Objects

This document will cover all the nodejs methods related to objects

Objects represent non-user entities such as organizations, teams, roles, and projects and are used to send notification to channels or groups belonging to such entities or group of users who are subscribed to these entities. Understand more about objects here.

Create or update an object

This is an upsert operation which creates or updates the object.

import org.json.JSONObject;
import suprsend.Suprsend;

public class ObjectEdit {
  public static void main(String[] args) throws Exception {
    objectEditInstance();
  }

  public static void getSuprClient() throws Exception {
    Suprsend suprsendClient = new Suprsend("_workspace_key_", "_workspace_secret_");
    return suprsendClient;
  }

  public static void objectEditInstance() throws Exception {
    Suprsend suprsendClient = getSuprClient();
    // Instiantiate object
    String objectType = "__objecttype__";
    String objectId = "__objectid__";

    // Object Payload to upsert properties and communication channels
    JSONObject payload = new JSONObject()
      .put("name", "John Doe");
    .put("$email", "[email protected]");

    JSONObject object = suprClient.objects.upsert(objectType, objectId, payload);
    System.out.println(object);
  }
}

Edit an object (build edit payload yourself)

Use this to modify an object, typically for removing channels or unsetting properties. The payload will follow the same structure as the Object Edit API.

import org.json.JSONObject;
import suprsend.Suprsend;


public class ObjectEdit {

  public static void objectEditInstance() throws Exception {
    Suprsend suprsendClient = getSuprClient();
    
    // Instiantiate object
    String objectType = "__objecttype__";
    String objectId = "__objectid__";

    // Object Payload
    JSONObject payload = new JSONObject().put("operations",
				new JSONObject[] { new JSONObject().put("$remove", new JSONObject().put("name", "John Doe")) });
    
		JSONObject object = suprClient.objects.edit(objectType, objectId, payload);
		System.out.println(object);
  }
}

Edit an object (using helper methods) [Recommended]

It is possible to use the SDK's helper methods to perform edit operations on an object.

Create Object Instance

For this, first create object instance then call any of the helper methods mentioned below and finally save the changes.

import org.json.JSONObject;
import suprsend.Suprsend;
import suprsend.ObjectEdit;

public class ObjectEdit {

  public static void objectEditInstance() throws Exception {
    Suprsend suprsendClient = getSuprClient();

    //Create Object Instance
    String objectType = "__objecttype__";
    String objectId = "__objectid__";
    ObjectEdit object_ins = suprClient.objects.getInstance(objectType, objectId);

    // Add channels
    object_ins.addEmail("[email protected]");
    object_ins.addSms("+919999999999");

    // Call edit api
    JSONObject res = suprClient.objects.edit(object_ins);
    System.out.println(res);
  }
}

Add channels

Use object_ins.add_* method(s) to add channels.

// Add Email
object_ins.addEmail("[email protected]");

//Add SMS
object_ins.addSms("+919999999999");

//Add Whatsapp
object_ins.addWhatsapp("+919999999999");

// Add Androidpush token.Pass the vendor as 2nd param.
object_ins.addAndroidpush("androidpush_fcm_token__","fcm");

// Add iospush token 
object_ins.addIospush("__iospush_apns_token__");

// Add Slack using user email id
JSONObject slackIdent = new JSONObject()
  .put("access_token", "xoxb-XXXXXXXX")
  .put("email", "[email protected]");
object_ins.addSlack(slackIdent);

// Add Slack using member_id of the user if known
JSONObject slackIdent = new JSONObject()
  .put("access_token", "xoxb-XXXXXXXX")
  .put("user_id", "U03XXXXXXXX");
object_ins.addSlack(slackIdent);

// Add Slack channel_id
JSONObject slackIdent = new JSONObject()
  .put("access_token", "xoxb-XXXXXXXX")
  .put("channel_id", "C04XXXXXXXX");
object_ins.addSlack(slackIdent);

// Add Slack incoming webhook
JSONObject slackIdent = new JSONObject()
  .put("incoming_webhook", new JSONObject().put("url", "https://hooks.slack.com/services/TXXXXXXXXX/BXXXXXXXX/XXXXXXXXXXXXXXXXXXX"))
object_ins.addSlack(slackIdent); 

// Add Webpush token json (VAPID)
JSONObject webpush = new JSONObject()
  .put("endpoint", "__end_point__")
  .put("expirationTime", "")
  .put("keys", new JSONObject()
       .put("p256dh", "__p256dh__")
       .put("auth", "__auth_key__"));
object_ins.addWebpush(webpush, "vapid");

Remove channels

Use object_ins.remove_* method(s) to remove channels from an object.

// Remove Email
object_ins.removeEmail("[email protected]");

//Remove SMS
object_ins.removeSms("+919999999999");

//Remove Whatsapp
object_ins.removeWhatsapp("+919999999999");

// Remove Androidpush token.Pass the vendor as 2nd param.
object_ins.removeAndroidpush("androidpush_fcm_token__","fcm");

// Remove iospush token 
object_ins.removeIospush("__iospush_apns_token__");

// Remove Slack using user email id
JSONObject slackIdent = new JSONObject()
  .put("access_token", "xoxb-XXXXXXXX")
  .put("email", "[email protected]");
object_ins.removeSlack(slackIdent);

// Remove Slack using member_id of the user if known
JSONObject slackIdent = new JSONObject()
  .put("access_token", "xoxb-XXXXXXXX")
  .put("user_id", "U03XXXXXXXX");
object_ins.removeSlack(slackIdent);

// Remove Slack channel_id
JSONObject slackIdent = new JSONObject()
  .put("access_token", "xoxb-XXXXXXXX")
  .put("channel_id", "C04XXXXXXXX");
object_ins.removeSlack(slackIdent);

// Remove Slack incoming webhook
JSONObject slackIdent = new JSONObject()
  .put("incoming_webhook", new JSONObject().put("url", "https://hooks.slack.com/services/TXXXXXXXXX/BXXXXXXXX/XXXXXXXXXXXXXXXXXXX"))
  object_ins.removeSlack(slackIdent); 

// Remove Webpush token json (VAPID)
JSONObject webpush = new JSONObject()
  .put("endpoint", "__end_point__")
  .put("expirationTime", "")
  .put("keys", new JSONObject()
       .put("p256dh", "__p256dh__")
       .put("auth", "__auth_key__"));
object_ins.removeWebpush(webpush, "vapid");


Remove channels in bulk

If you need to delete/unset all emails (or any other channel data) of an object, you can call object_ins.unset() method. The method accepts the channel key/s (a single key or list of keys).

object_ins.unset("$email");
object_ins.unset(Arrays.asList(new String[] { "$sms", "$email" }));

// available channel keys - 
//{"$email","$whatsapp","$sms","$androidpush","$iospush","$webpush","$slack","$ms_teams"}

Set preferred language

If you want to send notification in object's preferred language, you can set it by passing language code in object_ins.setPreferredLanguage() method. This is useful especially for the applications which offer vernacular or multi-lingual support.

object_ins.setPreferredLanguage("es")

Set timezone

You can set timezone of user using object_ins.setPreferredLanguage() method. Value for timezone must be from amongst the IANA timezones.

object_ins.setTimezone("America/Los_Angeles");

Set

Set any custom property using this method. It will shallow merge existing properties with new values. Key shouldn't start with $ or ss_.

//set single property
object_ins.set("prop1", "val1");

//set multiple properties
JSONObject objectsProperties = new JSONObject()
  .put("prop2", "val2")
  .put("number", 20)
object_ins.set(objectsProperties);

Unset

Use this to unset existing channels or properties.

//unset single channel or property
object_ins.unset("$email");

//unset multiple channels or properties
object_ins.unset(Arrays.asList(new String[] { "$sms", "wishlist" }));

Append

Use this to append item to an array based property.

//append single property
object_ins.append("array", "k1");

//append multiple properties
JSONObject objectsProperties = new JSONObject()
  .put("wishlist", "iphone12")
  .put("products", "tenancy");
object_ins.append(objectsProperties);

Remove

Use this to remove an item from array based property.

//append single property
object_ins.remove("array", "k1");

//append multiple properties
JSONObject objectsProperties = new JSONObject()
  .put("wishlist", "iphone12")
  .put("products", "tenancy");
object_ins.remove(objectsProperties);

Set once

Use this to add new properties which are not overridden, such as first_login_at.

//single property
object_ins.setOnce("first_login", "2021-11-02");

//multiple properties
JSONObject objectsProperties = new JSONObject()
  .put("first_login", "2021-11-02")
  .put("DOB", "1991-10-02");
object_ins.setOnce(objectsProperties);

Increment

Use on numeric values to increment/decrement. Provide a negative value for decrement.

//single property
object_ins.increment("login_count", 1);

//multiple properties
JSONObject objectsProperties = new JSONObject()
  .put("login_count", 1)
  .put("remaining_credits", -1);
object_ins.increment(objectsProperties);

List objects

List all objects of an object type.

import org.json.JSONObject;
import suprsend.Suprsend;

public class ObjectAPI {

  public static void ObjectList() throws Exception {
    Suprsend suprsendClient = getSuprClient();

    String objectType = "__objecttype__";

    // optional params to pass limit (default=10) and cursor pointer for fetching the next set of results
    HashMap<String, Object> opts = new HashMap<String, Object>() {
      {
        put("limit", 10);
        put("after", "01HFS04E4J29KHPYRK7HT3YQQ5");
      }
    };

    JSONObject res = suprClient.objects.list(objectType, opts);
    System.out.println(res);
  }
}

You can pass optional query parameters -limit,before,after


Get Object details

import org.json.JSONObject;
import suprsend.Suprsend;

public class ObjectAPI {

  public static void ObjectGet() throws Exception {
    Suprsend suprsendClient = getSuprClient();

    String objectType = "__objecttype__";
    String objectId = "__objectid__";

    JSONObject res = suprClient.objects.get(objectType, objectId);
    System.out.println(res);
  }
}

Delete object

Delete action will take into immediate effect.

import org.json.JSONObject;
import suprsend.Suprsend;

public class ObjectAPI {

  public static void ObjectDelete() throws Exception {
    Suprsend suprsendClient = getSuprClient();

    String objectType = "__objecttype__";
    String objectId = "__objectid__";

    JSONObject res = suprClient.objects.delete(objectType, objectId);
    System.out.println(res);
  }
}

Bulk delete objects

import org.json.JSONObject;
import suprsend.Suprsend;

public class ObjectAPI {

  public static void ObjectBulkDelete() throws Exception {
    Suprsend suprsendClient = getSuprClient();

    String objectType = "__objecttype__";
    JSONObject payload = new JSONObject().put("object_ids", Arrays.asList("__objectid__"));

    JSONObject res = suprClient.objects.bulkDelete(objectType, payload);
    System.out.println(res);
  }
}

Create subscriptions to object

import org.json.JSONObject;
import suprsend.Suprsend;

public class ObjectAPI {

  public static void ObjectCreateSubscriptions() throws Exception {
    Suprsend suprsendClient = getSuprClient();

    String objectType = "__objecttype__";
		String objectId = "__objectid__";
		JSONObject payload = new JSONObject().put("recipients",
				Arrays.asList("__recipient_id_1__", new JSONObject().put("distinct_id", "__recipient_id_2__"),
						new JSONObject().put("object_type", "__objecttype__").put("id", "__objectid_2_")));
		
    JSONObject res = suprClient.objects.createSubscriptions(objectType, objectId, payload);
		System.out.println(res);
  }
}

List subscriptions of object

import org.json.JSONObject;
import suprsend.Suprsend;

public class ObjectAPI {

  public static void ObjectGetSubscriptions() throws Exception {
    Suprsend suprsendClient = getSuprClient();

    String objectType = "__objecttype__";
		String objectId = "__objectid__";
    
    // optional params to pass limit (default=10) and cursor pointer for fetching the next set of results
    HashMap<String, Object> opts = new HashMap<String, Object>() {
			{
				put("limit", 10);
				put("after", "01HFS04E4J29KHPYRK7HT3YQQ5");
			}
		};
		
    JSONObject res = suprClient.objects.getSubscriptions(objectType, objectId, opts);
		System.out.println(res);
  }
}

You can pass optional query parameters -limit,before,after.


Delete subscriptions in object

import org.json.JSONObject;
import suprsend.Suprsend;

public class ObjectAPI {

  public static void ObjectDeleteSubscriptions() throws Exception {
    Suprsend suprsendClient = getSuprClient();

    String objectType = "__objecttype__";
		String objectId = "__objectid__";
    JSONObject payload = new JSONObject().put("recipients",
				Arrays.asList("__recipient_id_1__", new JSONObject().put("distinct_id", "__recipient_id_2__"),
						new JSONObject().put("object_type", "__objecttype__").put("id", "__objectid_2_")));
		
    JSONObject res = suprClient.objects.deleteSubscriptions(objectType, objectId, payload);
		System.out.println(res);
  }
}

Get list of objects subscribed by object

An object can subscribe to other objects. Use this method to get the list of all objects that the current object has subscribed to.

import org.json.JSONObject;
import suprsend.Suprsend;

public class ObjectAPI {

  public static void GetObjectsSubscribedTo() throws Exception {
    Suprsend suprsendClient = getSuprClient();

    String objectType = "__objecttype__";
    String objectId = "__objectid__";

    // optional params to pass limit (default=10) and cursor pointer for fetching the next set of results
    HashMap<String, Object> opts = new HashMap<String, Object>() {
      {
        put("limit", 10);
        put("after", "01HFS04E4J29KHPYRK7HT3YQQ5");
      }
    };

    JSONObject res = suprClient.objects.getObjectsSubscribedTo(objectType, objectId, opts);
    System.out.println(res);
  }
}