Giter Site home page Giter Site logo

cmdotcom / text-sdk-javascript Goto Github PK

View Code? Open in Web Editor NEW
7.0 13.0 10.0 624 KB

Javascript SDK to send messages with CM.com

Home Page: https://www.cm.com/communications-platform/

License: MIT License

TypeScript 100.00%
messaging text sms push whatsapp viber wechat business-messaging telegram conversational

text-sdk-javascript's Introduction

@cmdotcom/text-sdk: A helper library to send text messages.

Published on NPM Node.js CI Codecov

Want to send text-messages in your Node.js application? Then you are at the right place.

The package is currently limited in features compared to the CM gateway, but if you want to get all the functionalities, go to: CM.com API Docs

Usage

Node.JS

First, run npm install @cmdotcom/text-sdk. Then, in your source file:

const messagingApi = require("@cmdotcom/text-sdk");

// Get your product token at CM.com.
const yourProductToken = "";
const myMessageApi = new messagingApi.MessageApiClient(yourProductToken);

const result = myMessageApi.sendTextMessage(["00316012345678"], "TestSender", "Hello world?!");

result.then((result) => {
    console.log(result);
}).catch((error) => {
    console.log(error);
});

or send multiple

const result = myMessageApi.sendTextMessage(["00316012345678","003160000000"], "TestSender", "Hello world?!");

send rich messages using the message builder

const richMessage = {
    media: {
        mediaName: "cm.com",
        mediaUri: "https://avatars3.githubusercontent.com/u/8234794?s=200&v=4"
    },
    text: "Our logo!"
};

const suggestion = {
    action: "openUrl",
    label: "Click me",
    url: "https://www.cm.com"
};

const response = client.createMessage()
    .setMessage(["00316012345678"], "TestSender", "Hello world?!")
    .setAllowedChannels(["Viber"])
    .setConversation([richMessage])
    .setSuggestion([suggestion])
    .send();

response.then((result) => {
    console.log(result);
}).catch((error) => {
    console.log(error);
});

or send whatsapp template messages using the message builder

const template = {
    whatsapp: {
        elementName: 'template-name',
        language: {
            code: 'en',
            policy: 'deterministic'
        },
        namespace: 'the-namespace-of-template',
        components: [{
            type: 'body',
            parameters: [{
                type: 'text',
                text: 'firstname'
            }]
        }]
    }
};

const response = client.createMessage()
    .setMessage(["00316012345678"], "TestSender", "Hello world?!")
    .setAllowedChannels(["WhatsApp"])
    .setTemplate(template)
    .send();

response.then((result) => {
    console.log(result);
}).catch((error) => {
    console.log(error);
});

or send whatsapp interactive messages using the message builder

const whatsAppInteractiveContent = {
        type: 'list',
        header: {
            type: "text",
            text: "your-header-content"
        },
        body: {
            text: "your-text-message-content"
        },
        footer: {
            text: "your-footer-content"
        },
        action: {
            button: "cta-button-content",
            sections: [{
                title: "your-section-title1",
                rows: [{
                    id: "unique-row-identifier1",
                    title: "row-title-content",
                    description: "row-description-content"
                }]
            },
            {
                title: "your-section-title2",
                rows: [{
                    id: "unique-row-identifier2",
                    title: "row-title-content",
                    description: "row-description-content"
                }]
            }
            ]
        }
};

const response = client.createMessage()
    .setMessage(["00316012345678"], "TestSender", "Hello world?!")
    .setAllowedChannels(["WhatsApp"])
    .setInteractive(whatsAppInteractiveContent)
    .send();

response.then((result) => {
    console.log(result);
}).catch((error) => {
    console.log(error);
});

License

@cmdotcom/text-sdk is under the MIT license. See LICENSE file.

text-sdk-javascript's People

Contributors

conorshirren avatar dependabot[bot] avatar ericsmekens avatar joepb avatar jwraats avatar michaelvdnet avatar robinvoogt avatar soufyanbreda avatar soufyanbreda2 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

text-sdk-javascript's Issues

Throwing response instead of error

The SDK is throwing the response when a request fails, while an error is expected.

const send = async () => {
  try {
    const client = new CM.MessageApiClient('token')

    const result = await client.createMessage().send()
  } catch (e) {
    console.log(e) // this is a http response

    throw new Error(e.body.messages[0].messageErrorCode, e.body.messages[0].messageDetails)
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.