create endpoint for newsletter and handle subscription

This commit is contained in:
Matthias Kretschmann 2018-06-21 16:52:16 +02:00
parent 701561cbdb
commit 580471f003
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 58 additions and 24 deletions

View File

@ -9,6 +9,8 @@
## Tasks
### Medium
**`webtask-medium.js`**: Generic task to fetch and reconstruct items from any medium publication.
Requires the Medium username appended at the end of the url, e.g. locally:
@ -23,7 +25,7 @@ When published as a web task, append the taskname followed by the Medium usernam
https://TASK_URL/TASK_NAME/MEDIUM_USERNAME
```
---
### YouTube
**`webtask-youtube.js`**: Generic task to fetch and reconstruct items from any YouTube account. For now, only fetches a playlist. YouTube API key is provided via [secret environment variable](https://webtask.io/docs/issue_parameters) `YOUTUBE_API_KEY` setup in web editor of webtask.io
@ -39,6 +41,28 @@ Add the task name when published on webtask.io:
https://TASK_URL/TASK_NAME/YOUTUBE_PLAYLIST_ID
```
### Zoho
**`webtask-zoho.js`**: Generic task to subscribe users into lists on Zoho Campaigns & Zoho CRM. Credentials are provided via [secret environment variables](https://webtask.io/docs/issue_parameters), setup in web editor of webtask.io.
The user input data needs to be in `json` format like so:
```
{Contact Email:info@oceanprotocol.com}
```
To subscribe a user to the newsletter, construct your request url like so, e.g. locally:
```
http://localhost:8080/newsletter/DATA
```
Add the task name when published on webtask.io:
```
https://TASK_URL/TASK_NAME/newsletter/DATA
```
## Development
```bash

View File

@ -7,6 +7,7 @@
"test": "eslint ./{src,public}/**/*.js"
},
"dependencies": {
"body-parser": "^1.18.3",
"cors": "^2.8.4",
"express": "^4.16.3",
"http-proxy-middleware": "^0.18.0",

View File

@ -2,44 +2,53 @@ const express = require('express')
const Webtask = require('webtask-tools')
const cors = require('cors')
const proxy = require('http-proxy-middleware')
const bodyParser = require('body-parser')
const request = require('request')
const server = express()
server.use(cors())
server.listen(4430)
server.use(bodyParser.json())
const apiUrlZohoCampaigns = 'https://campaigns.zoho.com/api/'
const apiUrlZohoCRM = 'https://www.zohoapis.com/crm/v2/'
//
// Subscribe to newsletter via Zoho Campaigns API
// https://www.zoho.com/campaigns/newhelp/api/contact-subscribe.html
//
server.get('/newsletter/:data', (req, res) => {
const { ZOHO_CAMPAIGNS_TOKEN, ZOHO_CAMPAIGNS_LIST_KEY } = req.webtaskContext.secrets
const { data } = req.params
const options = {
url: `${apiUrlZohoCampaigns}json/listsubscribe?authtoken=${ZOHO_CAMPAIGNS_TOKEN}&scope=CampaignsAPI&resfmt=JSON&listkey=${ZOHO_CAMPAIGNS_LIST_KEY}&contactinfo=${data}` // eslint-disable-line max-len
}
request(options, (error, response, body) => { // eslint-disable-line consistent-return
if (error) {
res.send(error)
}
res.send(body)
res.sendStatus(200)
})
})
const onProxyReqZohoCRM = (proxyReq, req) => {
const { ZOHO_CRM_TOKEN } = req.webtaskContext.secrets
proxyReq.setHeader('Authorization', `Zoho-oauthtoken ${ZOHO_CRM_TOKEN}`)
}
const onProxyReqZohoCampaigns = (proxyReq, req) => {
const { ZOHO_CAMPAIGNS_TOKEN, ZOHO_CAMPAIGNS_LIST_KEY } = req.webtaskContext.secrets
//
// Auth via query params
// https://www.zoho.com/campaigns/newhelp/api/contact-subscribe.html
//
proxyReq.path += `?authtoken=${ZOHO_CAMPAIGNS_TOKEN}&scope=CampaignsAPI&resfmt=JSON&listkey=${ZOHO_CAMPAIGNS_LIST_KEY}&contactinfo={First Name:mac,Last Name:Last Name,Contact Email:jai@zoho.com}` // eslint-disable-line max-len
}
const configZohoCRM = {
target: 'https://www.zohoapis.com/crm/v2/',
target: apiUrlZohoCRM,
pathRewrite: { '^/zoho/crm/': '/' },
changeOrigin: true,
onProxyReq: onProxyReqZohoCRM
}
const configZohoCampaigns = {
target: 'https://campaigns.zoho.com/api/',
pathRewrite: { '^/zoho/campaigns/': '/' },
changeOrigin: true,
onProxyReq: onProxyReqZohoCampaigns
}
server.use(cors())
// server.use('*', proxy(config))
server.use(proxy('/zoho/crm/**', configZohoCRM))
server.use(proxy('/zoho/campaigns/**', configZohoCampaigns))
server.listen(4430)
module.exports = Webtask.fromExpress(server)