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!
npm install amio-sdk-js --save
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 */})
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)
}
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). |
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 inamioWebhookRouter.onMessageReceived()
)
- 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)
- 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)
})
- Implement event handlers.
amioWebhookRouter.onMessageReceived((data, timestamp) => {
console.log('a new message from contact ${data.contact.id} was received!')
})
Facebook:
Viber:
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 => {
// ...
})