npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bcm-one/messaging-sdk

v1.0.3

Published

Wrapper to easily manage connections with Messaging platform

Downloads

10

Readme

BCM One Messaging SDK

Installation and initialization

  • In an empty folder initialize npm
npm init
  • Install the npm Messaging package
npm i @bcm-one/messaging-sdk
  • Create a js file
touch main.js

Now edit your main.js file:

  • Import messaging from @bcm-one/messaging-sdk
import { messaging } from '@bcm-one/messaging-sdk';

messaging is an utility function that accepts a username and a password as arguments and return an instance of the Messaging class.

  • Initialize the Messaging instance

const myUsername = 'username';
const myPassword = 'password';

const msg = messaging(username, password)

Congratulations! Now you can send messages!

Sending Messages

Currently, messaging package, allows to send SMS. Other functionality are currently in development.

Send SMS

Once the messaging instance is initialized you can simply send SMS in the form of a JavaScript object:

const sms = {
    from: '12345678909',
    to: '12345678908',
    message: 'Hello world!'
}
  • from: a string representing a valid and registered DID. This DID must be associated with the username and password provided during the initialization of the messaging instance.

  • to: a string representing the DID number of recipient.

  • message: a string representing the content of the message.

in order to send the SMS, just call sendSMS method on the messaging instance, passing the sms payload as argument.

msg.sendSMS(sms)

SMS Responses

sendSMS method can return a MessageResponse or an ErrorResponse in the form of a Promise. To resolve the promise you can use the modern JS syntax with async and await, enclosing sendSMS in an asynchronous function.

// In this case, sendSMS is enclosed in a self calling async anonymous function 
;(async () => {
  try {
    const response = await msg.sendSMS(sms)
    console.log(response)
  } catch (error) {
    console.log(error)
  }
})()

Or you can use then and catch syntax

msg
  .sendSMS(sms)
  .then(response => {
    console.log(response)
  })
  .catch(err => {
    console.log(err)
  })

Successful responses

Successful responses have a statusCode of 202 on the resolved response object. They also report the sender (from), the recipient (to), the message sent (message) along with a unique identifier.

This is an example of a successful MessageResponse:

{
  from: '14707343234',
  to: '19171212121',
  message: 'Hello world',
  id: '473ebf4f-0997-430c-92ca-051725bc9c60',
  statusCode: 202
}

Error responses

An error response has a statusCode that is different from 202. Note that all the HTTP errors are returned as a simple response without the need to try and catch them because messaging handles them behind the hood.

An Error response provides more information about how the error generated on the detail key of the ErrorResponse object.

This is an example of an error response:

{
  detail: 'Resource not found\n' +
    '\n' +
    '        You are trying to send a message from a not registered DID\n' +
    'Error: from',
  statusCode: 404
}

Since HTTP errors are returned on the same response you can differentiate them comparing the status code.

;(async () => {
    const response = await msg.sendSMS(sms)
    if (response.statusCode === 202){
        console.log(response)
    } else {
      console.log('In this case an error occurred')
      console.log('On response.detail you can find more details about the error')
      console.log(response.detail)
    }
})()

This provides a much more cleaner way to handle your HTTP response rather than using try and catch.

Using TypeScript

Messaging provides type definitions for developers that want to use TypeScript.

Just make sure to create a custom type guard to define the response datatype. Here an example

import { messaging, SMS, SMSResponse } from '@bcm-one/messaging-sdk';

const myPass = "password"
const myuser = "username"

const msg = messaging(myuser, myPass)

const sms: SMS = {
    from: "12345678901",
    to: "12343212345",
    message: "Hello world!"
}


const isSuccessfulResponse = (response: any): response is SMSResponse =>{
    return response.statusCode === 202;
}

msg.sendSMS(sms).then(response => {
    if(isSuccessfulResponse(response)){
        console.log("Got a successful response!")
        console.log("MESSAGE: ", response.message)
        console.log("FROM: ", response.from)
        console.log("TO: ", response.to)
        console.log("ID: ", response.id)
    } else {
        console.log("Type guard exclude SMS response")
        console.log("Error status code: ", response.statusCode)
        console.log("Error detail: ", response.detail)
    }
})

In this example, isSuccessfulResponse act as a type guard. If the status code is 202 that mean that the response is SMSResponse, while if not is an ErrorResponse.