Skip to content

Commit

Permalink
Merge pull request #70 from kuceram/async
Browse files Browse the repository at this point in the history
Async
  • Loading branch information
kuceram authored Jan 25, 2019
2 parents e105b71 + ba918ea commit 2c85f78
Show file tree
Hide file tree
Showing 18 changed files with 689 additions and 184 deletions.
86 changes: 75 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# amio-sdk-js
Server-side library implementing [Amio](https://amio.io/) API for instant messengers. It covers API calls and webhooks.

[![CircleCI](https://circleci.com/gh/amio-io/amio-sdk-js.svg?style=shield)](https://circleci.com/gh/amio-io/amio-sdk-js) [![npm version](https://badge.fury.io/js/amio-sdk-js.svg)](https://badge.fury.io/js/amio-sdk-js)

Let us know how to improve this library. We'll be more than happy if you report any issues or even create pull requests. ;-)
Server-side library implementing [Amio API](https://docs.amio.io/v1.0/reference) for instant messengers. It supports API calls as well as webhooks.

> Let us know how to improve this library. We'll be more than happy if you report any issues or even create pull requests. ;-)
- [Installation](#installation)
- [API](#api)
- [Quickstart](#quickstart)
- [send a message](#send-a-message)
- [receive a message](#receive-a-message)
- [SDK API](#sdk-api)
- [setup & usage](#api---setup--usage)
- [error handling](#api---error-handling)
- [methods](#api---methods)
Expand All @@ -15,38 +18,84 @@ Let us know how to improve this library. We'll be more than happy if you report
- [setup & usage](#webhooks---setup--usage)
- [event types](#webhooks---event-types)
- [Missing a feature?](#missing-a-feature)

## Prerequisities

You need to [create an account](https://app.amio.io/signup) before you can use this library.
## Prerequisities
[Signup to Amio](https://app.amio.io/signup) and create a channel before using this library.

## Installation

```bash
npm install amio-sdk-js --save
```

## API
## Quickstart

#### Send a message
```js
const AmioApi = require('amio-sdk-js').AmioApi

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

async function sendMessage() {
const message = await amioApi.messages.send({
channel: {id: '{CHANNEL_ID}'},
contact: {id: '{CONTACT_ID}'},
content: content
})

return message
}
```

#### Receive a message
```js
const express = require('express')
const router = express.Router()
const WebhookRouter = require('amio-sdk-js').WebhookRouter

const amioWebhookRouter = new WebhookRouter({
secrets: {
'{CHANNEL_ID}':'{SECRET}'
}
})

amioWebhookRouter.onMessageReceived(function(data) {
console.log('new message received from contact ' + data.contact.id + 'with content ' + data.content)
})

router.post('/webhooks/amio', function (req, res) {
amioWebhookRouter.handleEvent(req, res)
})
```

## SDK API

### API - setup & usage

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

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

// example request
// request with async/await
const message = await amioApi.messages.send({/* message */})

// request with a promise
amioApi.messages.send({/* message */})
.then(message => ...)
```

### API - error handling
Amio API errors keep the structure described in the [docs](https://docs.amio.io/reference#errors).

##### With async/await
```js
try{
// ...
const message = await amioApi.messages.send({/* message */})
} catch(err){
if (err.amioApiError) {
console.error(err.jsonify(), err)
Expand All @@ -57,6 +106,21 @@ try{
}
```

##### With promises
```js
amioApi.messages.send({/* message */})
.then(message => ...)
.catch(err => {
if (err.amioApiError) {
console.error(err.jsonify(), err)
return
}

console.error(err)
})
```


### API - methods
amioApi.* | Description | Links
-|-|:-:
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports.WebhookRouter = require('./lib/webhook/webhook-router')
module.exports.attachRawBody = require('./lib/util/attach-raw-body')
module.exports.AmioApi = require('./lib/connector')
module.exports.AmioApi = require('./lib/amio-api')
module.exports.amioHttpClient = require('./lib/http/amio.http-client')
module.exports.xHubSignatureUtils = require('./lib/webhook/x-hub-signature.utils')
4 changes: 2 additions & 2 deletions lib/connector.js → lib/amio-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Notification = require('./api/notification.request')
const Settings = require('./api/settings.request')
const contentBuilder = require('./util/content/content-builder')

class AmioConnector {
class AmioApi {

constructor (opts = {}) {
const accessToken = opts.accessToken || null
Expand All @@ -24,4 +24,4 @@ class AmioConnector {

}

module.exports = AmioConnector
module.exports = AmioApi
3 changes: 1 addition & 2 deletions lib/api/request.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const last = require('ramda/src/last')
const prop = require('ramda/src/prop')

class Request {

constructor(httpClient){
constructor(httpClient) {
this.httpClient = httpClient
}

Expand Down
Loading

0 comments on commit 2c85f78

Please sign in to comment.