1
0
mirror of https://github.com/oceanprotocol/webtasks synced 2024-11-27 21:00:07 +01:00

Merge pull request #3 from oceanprotocol/feature/zoho-proxy

Zoho proxy for Campaigns API
This commit is contained in:
Matthias Kretschmann 2018-06-22 13:22:29 +02:00 committed by GitHub
commit c8903af71a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 134 additions and 21 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules
package-lock.json
yarn.lock
.env

View File

@ -1,4 +1,6 @@
# Webtasks
[![banner](https://raw.githubusercontent.com/oceanprotocol/art/master/github/repo-banner%402x.png)](https://oceanprotocol.com)
<h1 align="center">webtasks</h1>
> 🐬 Ocean Protocol's webtasks doing automatic things for us via webtask.io
@ -7,36 +9,75 @@
[![Build Status](https://travis-ci.com/oceanprotocol/webtasks.svg?token=3psqw6c8KMDqfdGQ2x6d&branch=master)](https://travis-ci.com/oceanprotocol/webtasks)
[![js ascribe](https://img.shields.io/badge/js-ascribe-39BA91.svg)](https://github.com/ascribe/javascript)
## Tasks
## Table of Contents
**`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:
```
http://localhost:8080/MEDIUM_USERNAME
```
When published as a web task, append the taskname followed by the Medium username at the end:
```
https://TASK_URL/TASK_NAME/MEDIUM_USERNAME
```
- [Tasks](#tasks)
- [Medium](#medium)
- [YouTube](#youtube)
- [Zoho](#zoho)
- [Campaigns API](#campaigns-api)
- [Development](#development)
- [Deployment](#deployment)
- [Authors](#authors)
- [License](#license)
---
**`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
## 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:
```bash
http://localhost:8080/:medium_username
# when published on webtask.io
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.
Construct your request url like so, e.g. locally:
```
http://localhost:8080/YOUTUBE_PLAYLIST_ID
```bash
http://localhost:8080/:youtube_playlist_id
# when published on webtask.io
https://TASK_URL/TASK_NAME/:youtube_playlist_id
```
Add the task name when published on webtask.io:
### 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:
* `ZOHO_CAMPAIGNS_TOKEN`
* `ZOHO_CAMPAIGNS_LIST_KEY`
* `ZOHO_CRM_TOKEN`
#### Campaigns API
* `/newsletter/:data`: subscribes the given email address to the newsletter list on Zoho Campaigns.
The data needs to be in `json` format in the following pattern:
```
https://TASK_URL/TASK_NAME/YOUTUBE_PLAYLIST_ID
{Contact Email:info@oceanprotocol.com}
```
Construct your request url like so, e.g. locally:
```bash
http://localhost:8080/newsletter/:data
# when published on webtask.io
https://TASK_URL/TASK_NAME/newsletter/:data
```
## Development

View File

@ -3,10 +3,12 @@
"version": "0.1.0",
"private": true,
"scripts": {
"start": "wt serve webtask-medium.js",
"start": "wt serve webtask-zoho.js",
"test": "eslint ./{src,public}/**/*.js"
},
"dependencies": {
"body-parser": "^1.18.3",
"cors": "^2.8.4",
"express": "^4.16.3",
"request": "^2.85.0",
"webtask-tools": "^3.2.0"

69
webtask-zoho.js Normal file
View File

@ -0,0 +1,69 @@
const express = require('express')
const Webtask = require('webtask-tools')
const cors = require('cors')
const bodyParser = require('body-parser')
const request = require('request')
const server = express()
server.listen(4430)
server.use(bodyParser.json())
//
// Allow requests from these domains only
//
const corsOptions = {
origin: ['https://oceanprotocol.com', /\.oceanprotocol\.com$/],
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
server.use(cors(corsOptions))
//
// Zoho APIs
//
const apiUrlZohoCampaigns = 'https://campaigns.zoho.com/api/'
const apiUrlZohoCRM = 'https://www.zohoapis.com/crm/v2/'
const sendRequest = (options, res) => {
request(options, (error, response, body) => {
if (error) res.send(error)
// just pass through whatever we get from the APIs
// as the response
res.send(body)
res.sendStatus(response.statusCode)
})
}
//
// 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=${decodeURIComponent(data)}` // eslint-disable-line max-len
}
sendRequest(options, res)
})
//
// Create a new lead via Zoho CRM API
// https://www.zoho.com/crm/help/api/v2/#create-specify-records
//
server.get('/crm/:data', (req, res) => {
const { ZOHO_CRM_TOKEN } = req.webtaskContext.secrets
const { data } = req.params
const options = {
url: `${apiUrlZohoCRM}Leads`, // eslint-disable-line max-len
headers: { 'Authorization': `Zoho-oauthtoken ${ZOHO_CRM_TOKEN}` },
method: 'POST',
formData: data
}
sendRequest(options, res)
})
module.exports = Webtask.fromExpress(server)