How to Use Extensions with SDK
Enable in Dashboard
Login to CometChat Dashboard, select your app, then go to Chat & Messaging → Features and enable the extension.
Configure settings (if required)
Some extensions require API keys or additional configuration. Open the extension settings in the Dashboard.
Implement SDK methods
Use the code examples below to call extension APIs and extract extension data from messages.
Bitly
Shorten long URLs in messages using Bitly.Shorten URL
- Kotlin
- Java
val url = "/v1/shorten"
val body = JSONObject()
body.put("text", "Your message with URL https://yourdomain.com/very/very/long/url")
CometChat.callExtension("url-shortener-bitly", "POST", url, body,
object : CometChat.CallbackListener<JSONObject>() {
override fun onSuccess(jsonObject: JSONObject) {
// minifiedText from the extension
}
override fun onError(e: CometChatException) {
// Some error occurred
}
}
)
String URL = "/v1/shorten";
JSONObject body = new JSONObject();
body.put("text", "Your message with URL https://yourdomain.com/very/very/long/url");
CometChat.callExtension("url-shortener-bitly", "POST", URL, body,
new CometChat.CallbackListener<JSONObject>() {
@Override
public void onSuccess(JSONObject jsonObject) {
// minifiedText from the extension
}
@Override
public void onError(CometChatException e) {
// Some error occured
}
});
Link Preview
Extract link preview metadata from messages.Extract Link Preview Data
- Kotlin
- Java
val metadata = message.metadata
if (metadata != null) {
val injectedObject = metadata.getJSONObject("@injected")
if (injectedObject != null && injectedObject.has("extensions")) {
val extensionsObject = injectedObject.getJSONObject("extensions")
if (extensionsObject != null && extensionsObject.has("link-preview")) {
val linkObject = extensionsObject.getJSONObject("link-preview")
val linkArray = linkObject.getJSONArray("links")
val linkPreviewObject = linkArray.getJSONObject(0)
if (linkPreviewObject.has("description")) {
val description = linkPreviewObject.getString("description")
}
// ... other fields
}
}
}
JSONObject metadata = message.getMetadata();
if (metadata != null) {
JSONObject injectedObject = metadata.getJSONObject("@injected");
if (injectedObject != null && injectedObject.has("extensions")) {
JSONObject extensionsObject = injectedObject.getJSONObject("extensions");
if (extensionsObject != null && extensionsObject.has("link-preview")){
JSONObject linkObject = extensionsObject.getJSONObject("link-preview");
JSONArray linkArray = linkObject.getJSONArray("links");
JSONObject linkPreviewObject = linkArray.getJSONObject(0);
if (linkPreviewObject.has("description"))
String description = linkPreviewObject.getString("description");
// ... other fields
}
}
}
Message Shortcuts
Send predefined messages using shortcuts (e.g.,!hb expands to “Happy birthday!”).
Fetch All Shortcuts
- Kotlin
- Java
CometChat.callExtension("message-shortcuts", "GET", "/v1/fetch", null,
object : CometChat.CallbackListener<JSONObject>() {
override fun onSuccess(responseObject: JSONObject) {
// Shortcuts received here.
}
override fun onError(e: CometChatException) {
// Some error occurred.
}
}
)
CometChat.callExtension("message-shortcuts", "GET", "/v1/fetch", null,
new CometChat.CallbackListener<JSONObject>() {
@Override
public void onSuccess(JSONObject responseObject) {
// Shortcuts received here.
}
@Override
public void onError(CometChatException e) {
// Some error occured.
}
});
Modify Shortcuts
- Kotlin
- Java
val body = JSONObject()
val shortcuts = JSONObject()
shortcuts.put("!hbd", "Happy birthday! Have fun!")
body.put("shortcuts", shortcuts)
CometChat.callExtension("message-shortcuts", "POST", "/v1/update", body,
object : CometChat.CallbackListener<JSONObject>() {
override fun onSuccess(responseObject: JSONObject) {
// Shortcuts updated successfully
}
override fun onError(e: CometChatException) {
// Some error occurred
}
}
)
JSONObject body = new JSONObject();
JSONObject shortcuts = new JSONObject();
shortcuts.put("!hbd", "Happy birthday! Have fun!");
body.put("shortcuts", shortcuts);
CometChat.callExtension("message-shortcuts", "POST", "/v1/update", body,
new CometChat.CallbackListener<JSONObject>() {
@Override
public void onSuccess(JSONObject responseObject) {
// Shortcuts updated successfully
}
@Override
public void onError(CometChatException e) {
// Some error occured
}
});
Pin Message
Pin important messages in conversations.Pin a Message
- Kotlin
- Java
val body = JSONObject()
body.put("msgId", 280)
CometChat.callExtension("pin-message", "POST", "/v1/pin", body,
object : CometChat.CallbackListener<JSONObject>() {
override fun onSuccess(responseObject: JSONObject) {
// { success: true }
}
override fun onError(e: CometChatException) {
// Error occurred
}
}
)
JSONObject body = new JSONObject();
body.put("msgId", 280);
CometChat.callExtension("pin-message", "POST", "/v1/pin", body,
new CometChat.CallbackListener<JSONObject>() {
@Override
public void onSuccess(JSONObject responseObject) {
// { success: true }
}
@Override
public void onError(CometChatException e) {
// Error occurred
}
});
Unpin a Message
- Kotlin
- Java
val body = JSONObject()
body.put("msgId", 111)
body.put("receiverType", "group")
body.put("receiver", "cometchat-guid-1")
CometChat.callExtension("pin-message", "DELETE", "/v1/unpin", body,
object : CometChat.CallbackListener<JSONObject>() {
override fun onSuccess(responseObject: JSONObject) {
// { success: true }
}
override fun onError(e: CometChatException) {
// Error occurred
}
}
)
JSONObject body = new JSONObject();
body.put("msgId", 111);
body.put("receiverType", "group");
body.put("receiver", "cometchat-guid-1");
CometChat.callExtension("pin-message", "DELETE", "/v1/unpin", body,
new CometChat.CallbackListener<JSONObject>() {
@Override
public void onSuccess(JSONObject responseObject) {
// { success: true }
}
@Override
public void onError(CometChatException e) {
// Error occurred
}
});
Fetch Pinned Messages
- Kotlin
- Java
val receiverType = "group"
val receiver = "cometchat-guid-1"
val url = "/v1/fetch?receiverType=$receiverType&receiver=$receiver"
CometChat.callExtension("pin-message", "GET", url, null,
object : CometChat.CallbackListener<JSONObject>() {
override fun onSuccess(responseObject: JSONObject) {
// { pinnedMessages: [] }
}
override fun onError(e: CometChatException) {
// Error occurred
}
}
)
String receiverType = "group";
String receiver = "cometchat-guid-1";
String URL = "/v1/fetch?receiverType=" + receiverType + "&receiver=" + receiver;
CometChat.callExtension("pin-message", "GET", URL, null,
new CometChat.CallbackListener<JSONObject>() {
@Override
public void onSuccess(JSONObject responseObject) {
// { pinnedMessages: [] }
}
@Override
public void onError(CometChatException e) {
// Error occurred
}
});
Rich Media Preview
Extract rich media preview metadata from messages using Iframely.Extract Rich Media Data
- Kotlin
- Java
val metadata = message.metadata
if (metadata != null) {
val injectedObject = metadata.getJSONObject("@injected")
if (injectedObject != null && injectedObject.has("extensions")) {
val extensionsObject = injectedObject.getJSONObject("extensions")
if (extensionsObject != null && extensionsObject.has("rich-media")) {
val richMediaObject = extensionsObject.getJSONObject("rich-media")
// Use richMediaObject data
}
}
}
JSONObject metadata = message.getMetadata();
if (metadata != null) {
JSONObject injectedObject = metadata.getJSONObject("@injected");
if (injectedObject != null && injectedObject.has("extensions")) {
JSONObject extensionsObject = injectedObject.getJSONObject("extensions");
if (extensionsObject != null && extensionsObject.has("rich-media")) {
JSONObject richMediaObject = extensionsObject.getJSONObject("rich-media");
// Use richMediaObject data
}
}
}
Save Message
Save important messages for later access.Save a Message
- Kotlin
- Java
val body = JSONObject()
body.put("msgId", 111)
CometChat.callExtension("save-message", "POST", "/v1/save", body,
object : CometChat.CallbackListener<JSONObject>() {
override fun onSuccess(responseObject: JSONObject) {
// { success: true }
}
override fun onError(e: CometChatException) {
// Error occurred
}
}
)
JSONObject body = new JSONObject();
body.put("msgId", 111);
CometChat.callExtension("save-message", "POST", "/v1/save", body,
new CometChat.CallbackListener<JSONObject>() {
@Override
public void onSuccess(JSONObject responseObject) {
// { success: true }
}
@Override
public void onError(CometChatException e) {
// Error occurred
}
});
Unsave a Message
- Kotlin
- Java
val body = JSONObject()
body.put("msgId", 111)
CometChat.callExtension("save-message", "DELETE", "/v1/unsave", body,
object : CometChat.CallbackListener<JSONObject>() {
override fun onSuccess(responseObject: JSONObject) {
// { success: true }
}
override fun onError(e: CometChatException) {
// Error occurred
}
}
)
JSONObject body = new JSONObject();
body.put("msgId", 111);
CometChat.callExtension("save-message", "DELETE", "/v1/unsave", body,
new CometChat.CallbackListener<JSONObject>() {
@Override
public void onSuccess(JSONObject responseObject) {
// { success: true }
}
@Override
public void onError(CometChatException e) {
// Error occurred
}
});
Fetch Saved Messages
- Kotlin
- Java
CometChat.callExtension("save-message", "GET", "/v1/fetch", null,
object : CometChat.CallbackListener<JSONObject>() {
override fun onSuccess(responseObject: JSONObject) {
// { savedMessages: [] }
}
override fun onError(e: CometChatException) {
// Error occurred
}
}
)
CometChat.callExtension("save-message", "GET", "/v1/fetch", null,
new CometChat.CallbackListener<JSONObject>() {
@Override
public void onSuccess(JSONObject responseObject) {
// { savedMessages: [] }
}
@Override
public void onError(CometChatException e) {
// Error occurred
}
});
Thumbnail Generation
Extract thumbnail URLs from image and video messages.Extract Thumbnail Data
- Kotlin
- Java
val metadata = message.metadata
if (metadata != null) {
val injectedObject = metadata.getJSONObject("@injected")
if (injectedObject != null && injectedObject.has("extensions")) {
val extensionsObject = injectedObject.getJSONObject("extensions")
if (extensionsObject != null && extensionsObject.has("thumbnail-generation")) {
val thumbnailObject = extensionsObject.getJSONObject("thumbnail-generation")
val attachments = thumbnailObject.getJSONArray("attachments")
for (i in 0 until attachments.length()) {
val attachment = attachments.getJSONObject(i)
if (!attachment.has("error")) {
val data = attachment.getJSONObject("data")
val thumbnails = data.getJSONObject("thumbnails")
val urlSmall = thumbnails.getString("url_small")
val urlMedium = thumbnails.getString("url_medium")
val urlLarge = thumbnails.getString("url_large")
// Use the URLs accordingly
}
}
}
}
}
JSONObject metadata = message.getMetadata();
if (metadata != null) {
JSONObject injectedObject = metadata.getJSONObject("@injected");
if (injectedObject != null && injectedObject.has("extensions")) {
JSONObject extensionsObject = injectedObject.getJSONObject("extensions");
if (extensionsObject != null && extensionsObject.has("thumbnail-generation")) {
JSONObject thumbnailObject = extensionsObject.getJSONObject("thumbnail-generation");
JSONArray attachments = thumbnailObject.getJSONArray("attachments");
for (int i = 0; i < attachments.length(); i++) {
JSONObject attachment = attachments.getJSONObject(i);
if (!attachment.has("error")) {
JSONObject data = attachment.getJSONObject("data");
JSONObject thumbnails = data.getJSONObject("thumbnails");
String urlSmall = thumbnails.getString("url_small");
String urlMedium = thumbnails.getString("url_medium");
String urlLarge = thumbnails.getString("url_large");
// Use the URLs accordingly
}
}
}
}
}
TinyURL
Shorten long URLs in messages using TinyURL.Shorten URL
- Kotlin
- Java
val body = JSONObject()
body.put("text", "Your message with URL https://yourdomain.com/very/very/long/url")
CometChat.callExtension("url-shortener-tinyurl", "POST", "/v1/shorten", body,
object : CometChat.CallbackListener<JSONObject>() {
override fun onSuccess(jsonObject: JSONObject) {
// minifiedText from the extension
}
override fun onError(e: CometChatException) {
// Some error occurred
}
}
)
JSONObject body = new JSONObject();
body.put("text", "Your message with URL https://yourdomain.com/very/very/long/url");
CometChat.callExtension("url-shortener-tinyurl", "POST", "/v1/shorten", body,
new CometChat.CallbackListener<JSONObject>() {
@Override
public void onSuccess(JSONObject jsonObject) {
// minifiedText from the extension
}
@Override
public void onError(CometChatException e) {
// Some error occured
}
});
Voice Transcription
Extract transcribed text from audio messages.Extract Transcription Data
- Kotlin
- Java
val metadata = message.metadata
if (metadata != null) {
val injectedObject = metadata.getJSONObject("@injected")
if (injectedObject != null && injectedObject.has("extensions")) {
val extensionsObject = injectedObject.getJSONObject("extensions")
if (extensionsObject != null && extensionsObject.has("voice-transcription")) {
val voiceTranscriptionObject = extensionsObject.getJSONObject("voice-transcription")
val transcribedMessage = voiceTranscriptionObject.getString("transcribed_message")
// Use the transcribed message
}
}
}
JSONObject metadata = message.getMetadata();
if (metadata != null) {
JSONObject injectedObject = metadata.getJSONObject("@injected");
if (injectedObject != null && injectedObject.has("extensions")) {
JSONObject extensionsObject = injectedObject.getJSONObject("extensions");
if (extensionsObject != null && extensionsObject.has("voice-transcription")) {
JSONObject voiceTranscriptionObject = extensionsObject.getJSONObject("voice-transcription");
String transcribedMessage = voiceTranscriptionObject.getString("transcribed_message");
// Use the transcribed message
}
}
}