mirror of
https://github.com/oceanprotocol/webtasks
synced 2025-01-06 20:05:40 +01:00
create endpoint for newsletter and handle subscription
This commit is contained in:
parent
701561cbdb
commit
580471f003
26
README.md
26
README.md
@ -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
|
||||
|
@ -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",
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user