Send message

Plain text messages

The simplest message that can be sent is plain text.

You can use the MessageBuilder interface to build your message. This abstracts away the complexities of JSON formatting.

ES6

import { MessageBuilder } from "@comapi/sdk-js-foundation"

let message = new MessageBuilder().withText("Hello world");

sdk.services.appMessaging.sendMessageToConversation(channelDetails.id, message)
    .then(result => {
        console.log("sendMessageToConversation() succeeded", result);
    })
    .catch(error => {
        console.error("sendMessageToConversation() failed", result);
    });

Classic

var message = new COMAPI.MessageBuilder().withText("Hello world");

sdk.services.appMessaging.sendMessageToConversation(channelDetails.id, message)
    .then(function(result){
        console.log("sendMessageToConversation() succeeded", result);
    })
    .catch(function(error){
        console.error("sendMessageToConversation() failed", result);
    });

Message parts

A message consists of an array of message parts. In the above sample, there is a single part of type text/plain. You can have as many parts as you like and each part can be of the format of your choosing.

The underlying structure of a message part is as follows. Each of the properties is optional so you can use only those you need.

export interface IMessagePart {
    /**
     * The message part name
     */
    name?: string;
    /**
     * The message Part Type
     */
    type?: string;
    /**
     * The message URL
     */
    url?: string;
    /**
     * Te message data
     */
    data?: string;
    /**
     * The size of the data 
     */
    size?: number;
}

There are two helper methods available to allow more complex parts to be adde

withData

Here we create a message with 2 parts, the first part being plain text, and the second an image attachment.

The data type field is completely up to the integrator to use as they want. We’ve used Mime types for these samples


    // some data uri for an image ...
    let data = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="

    let message = new MessageBuilder()
        .withText("Check out this image ...")
        .withData("image/png", data);

withPart

If you want to manually create the parts:


    let textPart = {
        data: "Check out this image ...",
        type: "text/plain",
        size: 24 // Note:  size is optional, use as required
    };

    let imagePart = {        
        data: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==",
        type: "image/png",
    };

    let message = new MessageBuilder()
        .withPart(textPart)
        .withPart(imagePart);


Push notifications

If you want to have a push notification sent with the message, you can specify a generic message for both FCM and APNS and further customise the platform specific payloads.

Generic settings

    let message = new MessageBuilder()
        .withText("Hello world")
        .withPush("Hi there");

Platform specific overrides


let apnsAlert = {
    badge: 1,
    sound: "ping.aiff",
    alert: "hello"
};

let fcmAlert = {
    notification: {
        body: ";-)",
        title: "hello"
    }
};    

let message = new MessageBuilder()
    .withText("Hello world")
    .withPush("Hi there")
    .withFcmAlert(fcmAlert)
    .withApnsAlert(apnsAlert);


Metadata

You can also send metadata along with the message. This can be any object..

let message = new MessageBuilder()
    .withText("Hello world")
    .withMetadata({prop1: "val1", prop2: 2});

Events

EventDetails
conversationMessage.sentA message has been sent to the conversation.
conversationMessage.deliveredA participant has marked a message as delivered.
conversationMessage.readA participant has marked a message as read.