diff --git a/.gitignore b/.gitignore index f846c68..a4aee7e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules package-lock.json yarn.lock +.env diff --git a/README.md b/README.md index dbaf1fe..2a01f1c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# Webtasks +[![banner](https://raw.githubusercontent.com/oceanprotocol/art/master/github/repo-banner%402x.png)](https://oceanprotocol.com) + +

webtasks

> 🐬 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 diff --git a/package.json b/package.json index 09a4075..3ec5c61 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/webtask-zoho.js b/webtask-zoho.js new file mode 100644 index 0000000..8cc0f77 --- /dev/null +++ b/webtask-zoho.js @@ -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)