0.0.20 • Published 8 months ago

notificamehubsdk v0.0.20

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

NotificaMeHub SDK for Node.js

This SDK for Node.js was created based on the NotificaMe API.

Table of Contents

1.1.0

  • Changed
    • API endpoint to v1

Features

  • Text message content
  • File message content
  • Location message content
  • Contacts message content
  • Template message content
  • Email message content
  • Replyable text message content
  • Subscription handling

Prerequisites

Installation

npm install notificamehubsdk

Basic Usage

// ES5
const { Cliente } = require('notificamehubsdk');

// ES6 or Typescript
import * as Ciente from 'notificamehubsdk';

// Initialization with your API token (x-api-token)
const client = new Client('YOUR_API_TOKEN');

// Choosing the channel
const whatsapp = client.setChannel('whatsapp');

// Creating a text content
const content = new TextContent('some text message here');

// ES6
whatsapp.sendMessage('sender-identifier', 'recipient-identifier', content)
.then(response => {
  // do something here
})
.catch(error => {
  // handle error here
});

// ES8 or Typescript. NodeJS 7.6.0 or higher
try {
  const response = await whatsapp.sendMessage('sender-identifier', 'recipient-identifier', content);
  // do something here
} catch (error) {
  // handle error here
}

Getting Started

Examples not listed on this section can be found here.

Sending your first message

The content types that can be sent are:

NameDescription
TextContentUsed to send text messages to your customer.
FileContentUsed to send file messages to your customer.
LocationContentUsed to send location messages to your customer.
ContactsContentUsed to send contacts messages to your customer.
TemplateContentUsed to send template messages to your customer.
EmailContentUsed to send e-mail messages to your customer.
ReplyableTextContentUsed to send replyable text messages to your customer.

The channels that can be used to send the content are:

ChannelTextContentFileContentLocationContentContactsContentTemplateContentEmailContentCardContentCarouselContentReplyableTextContent
SMSXX
RCSXXXXXX
WhatsAppXXXXX
FacebookXXXXX
InstagramXXXXX
EmailX
GBMXXXXX
TelegramXXXX

Use the sendMessage method to messages to your customers.

// Text message using the SMS channel
const { Client } = require('notificamehubsdk');
const client = new Client('YOUR_API_TOKEN');
const sms = client.setChannel('sms');
const content = new TextContent('some text message');
const response = await sms.sendMessage('sender-identifier', 'recipient-identifier', content);

The response can be an IMessage object when successful or an IError object when an error occurs.

Subscribe for messages

Use the createSubscription method to create a MessageSubscription object for message subscriptions.

const { Client } = require('notificamehubsdk');
const client = new Client('YOUR_API_TOKEN');
const subscription = new MessageSubscription({
  url: 'https://your-webhook.company.com'
},
{
  channel: 'YOUR_API_CHANNEL'
});
const response = await client.createSubscription(subscription);

The response can be an ISubscription object when successful or an IError object on errors.

Subscribe for message status

Use the createSubscription method to create a MessageStatusSubscription object for message status subscriptions.

const { Client } = require('notificamehubsdk');
const client = new Client('YOUR_API_TOKEN');
const subscription = new MessageStatusSubscription({
  url: 'https://your-webhook.company.com'
},
{
  channel: 'YOUR_API_CHANNEL'
});
const response = await client.createSubscription(subscription);

The response can be an ISubscription object when successful or an IError object when an error occurs.

Receiving message events and message status events

Use the WebhookController class to create your webhook so you can receive message events and message status events. The default port is 3000.

If you inform the client, url, and channel fields, a subscription will be created, unless a subscription matching these configuration already exists.

In the messageEventHandler field you will receive the message events and in the messageStatusEventHandler field you will receive the message status events.

const { Client, WebhookController } = require('notificamehubsdk');
const client = new Client(process.env.HUB_API_TOKEN);
const webhook = new WebhookController({
  messageEventHandler: (messageEvent) => {
    console.log('Message event:', messageEvent);
  },
  messageStatusEventHandler: (messageStatusEvent) => {
    console.log('Message status event:', messageStatusEvent);
  },
  client,
  url: 'https://my-webhook.company.com',
  channel: 'YOUR_API_CHANNEL',
});
webhook.init();

To receive events running the example on your machine, we suggest ngrok.

Getting message reports

To get information on how many messages you've sent or have received during a period of time, use the getEntries method to list IReportMessagesEntry objects as shown below.

const { Client } = require('notificamehubsdk');
const client = new Client('YOUR_API_TOKEN');
const reportClient = client.getMessagesReportClient();
const response = await reportClient.getEntries({
  startDate: '2020-01-10',
  endDate: '2020-01-15',
});

The response can be an array of IReportMessagesEntry objects when successful or an IError object when an error occurs.

Getting flow reports

In order to get information about the current state of sessions (executions) of flows in a period of time, use the getEntries method to list IReportFlowEntry objects as shown below.

const { Client } = require('notificamehubsdk');
const client = new Client('YOUR_API_TOKEN');
const reportClient = client.getFlowReportClient();
const response = await reportClient.getEntries({ startDate: '2020-01-10' });

The response can be an array of IReportFlowEntry objects when successful or an IError object when an error occurs.

Listing your templates

You can execute CRUD operations on templates. For example, use the listTemplates method to list an ITemplate object.

const { Client } = require('notificamehubsdk');
const client = new Client('YOUR_API_TOKEN');
const response = await client.listTemplates();

The response will be an array of ITemplate object.

Contributing

Pull requests are always welcome!

Please consult the Contributors' Guide for more information on contributing.

License

MIT

0.0.20

8 months ago

0.0.16

11 months ago

0.0.17

11 months ago

0.0.18

11 months ago

0.0.19

11 months ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.10

1 year ago

0.0.11

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.5

1 year ago

0.0.6

1 year ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago