Git Product home page Git Product logo

amio-sdk-js's Introduction

amio-sdk-js

CircleCI npm version

Server-side library implementing Amio API for instant messengers. It covers API calls and webhooks.

We'll be more than happy if you report any issues or even create pull requests ;-). Let us know how to improve this lib, thank you!

Installation

npm install amio-sdk-js --save

API

API - setup & usage

const AmioApi = require('amio-sdk-js').AmioApi

const amioApi = new AmioApi({
    accessToken: 'get access token from https://app.amio.io/administration/settings/api'
})

// example request
amioApi.messages.send({/* message */})

API - error handling

Amio API errors keep the structure described in the docs.

try{
  // ...
} catch(err){
    if (err.amioApiError) {
      console.error(err.jsonify(), err) 
      return
    }
    
    console.error(err) 
}

API - methods

REST js Description
POST /v1/messages messages.send(message) Send a message to a contact. There are different message types for every platform (FB, Viber).

Webhooks

Central logic to handle webhooks coming from Amio is WebhookRouter. What does it do?

  • It responds OK 200 to Amio .
  • It verifies X-Hub-Signature.
  • It routes events to handlers (e.g. event message_received to a method registered in amioWebhookRouter.onMessageReceived())

Webhooks - setup & usage

  1. Setup WebhookRouter.
const WebhookRouter = require('amio-sdk-js').WebhookRouter

const amioWebhookRouter = new WebhookRouter({
    secretToken: 'get secret at https://app.amio.io/administration/channels/{{CHANNEL_ID}}/webhook'
})

// error handling, e.g. x-hub-signature is not correct
amioWebhookRouter.onError(error => console.error(error))

// assign event handlers 
amioWebhookRouter.onMessageReceived(handleMessageReceived)
amioWebhookRouter.onMessagesDelivered(handleMessageDelivered)
amioWebhookRouter.onMessagesRead(handleMessagesRead)
amioWebhookRouter.onMessageEcho(handleMessageEcho)
  1. Route incoming requests to WebhookRouter. You will probably NOT want to use a common error handler!!!
// example with Express.js 4
const express = require('express')
const router = express.Router()


router.post('/webhooks/amio-communicator', async (req, res) => {
    await amioWebhookHandler.handleEvent(req, res)
})
  1. Implement event handlers.
amioWebhookRouter.onMessageReceived((data, timestamp) => {
    console.log('a new message from contact ${data.contact.id} was received!')
})

Webhooks - event types

Facebook:

Viber:

Missing a feature?

File an issue or create a pull request. If you need a quick solution, use the prepared axios http client:

const amioHttpClient = require('amio-sdk-js').amioHttpClient

amioHttpClient.get('/v1/messages')
    .then(response => {
      // ...
    })

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.