Skip to content

Commit

Permalink
Added consumer to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasnakano committed May 6, 2019
1 parent c4c662f commit 464a3cc
Show file tree
Hide file tree
Showing 10 changed files with 1,573 additions and 0 deletions.
5 changes: 5 additions & 0 deletions configurations/config.dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ONESIGNAL_URL": "https://onesignal.com/api/v1/notifications",
"ONESIGNAL_KEY": "",
"ONESIGNAL_TOKEN": ""
}
5 changes: 5 additions & 0 deletions configurations/config.prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ONESIGNAL_URL": "https://onesignal.com/api/v1/notifications",
"ONESIGNAL_KEY": "",
"ONESIGNAL_TOKEN": ""
}
16 changes: 16 additions & 0 deletions main/sendPush/converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports.messageToOneSignal = (message) => {
let payload = {
'include_player_ids': message.provider_id,
'app_id': process.env.ONESIGNAL_KEY,
'contents': { 'en': message.message },
'url': message.deep_link,
'data': { 'uri': message.deep_link },
'headings': { 'en': message.title },
}

if (Object.keys(message).includes('image_url')) {
payload['ios_attachments'] = { 'thumb': message.image_url }
}

return payload
}
25 changes: 25 additions & 0 deletions main/sendPush/lambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const {OneSignal} = require('./pushService.js')
const {messageToOneSignal} = require('./converter.js')
const toParseBody = (record) => JSON.parse(record.body)

module.exports.handler = async (event, context) => {

console.log(`Received event ${JSON.stringify(event)}`)

const token = process.env.ONESIGNAL_TOKEN
const url = process.env.ONESIGNAL_URL

const sender = new OneSignal(token, url)

const push = (message) => sender.send(message)

const requests = event
.Records
.map(toParseBody)
.map(messageToOneSignal)
.map(push)

await Promise.all(requests)

return {}
}
60 changes: 60 additions & 0 deletions main/sendPush/pushService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const fetch = require('node-fetch')

class OneSignalError extends Error {}

class OneSignal {
constructor(token, url) {
this._token = token
this._url = url
}

async send(payload) {
const response = await this._sendOneSignal(payload)
const body = await response.json()
const isSuccessResponse = this._isSuccess(response, body)

console.log(`Onesignal response with ${response.status} and with body ${JSON.stringify(body)}.`)

if(!isSuccessResponse) {
const textPayload = JSON.stringify(payload)
const textResponseBody = JSON.stringify(body)

console.log(`[SEND_PUSH][ONE_SIGNAL][FAIL] Cannot send this payload ${textPayload} to onesignal. Retrive ${response.status} as status code and ${textResponseBody} as body`)

throw new OneSignalError(`[SEND_PUSH][ONE_SIGNAL][FAIL] Cannot send this payload ${textPayload} to onesignal. Retrive ${response.status} as status code and ${textResponseBody} as body`)
}

console.log('Success Finish')

return response
}

_isSuccess(response, body) {
if (response.ok) {
const hasErrorsField = Object.keys(body).includes('errors')

return !hasErrorsField
}

return false
}

_sendOneSignal(payload) {
const request = {
method: 'POST',
headers: {
"Content-type": "application/json; charset=UTF-8",
"Authorization": `Basic ${this._token}`
},

body: JSON.stringify(payload)
}

console.log(`Request onesignal in this url '${this._url}' with this configuration ${JSON.stringify(request)}`)

return fetch(this._url, request)
}
}

module.exports.OneSignal = OneSignal
module.exports.OneSignalError = OneSignalError
Loading

0 comments on commit 464a3cc

Please sign in to comment.