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

89 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-06-21 08:29:35 +02:00
const express = require('express')
const Webtask = require('webtask-tools')
const cors = require('cors')
const bodyParser = require('body-parser')
const request = require('request')
2018-06-21 08:29:35 +02:00
const server = express()
server.listen(4430)
server.use(bodyParser.json())
2018-06-21 15:28:57 +02:00
2018-06-22 13:05:58 +02:00
//
// 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))
2018-06-21 17:54:41 +02:00
//
2018-06-21 18:09:22 +02:00
// Zoho APIs
2018-06-21 17:54:41 +02:00
//
const apiUrlZohoCampaigns = 'https://campaigns.zoho.com/api/'
const apiUrlZohoCRM = 'https://www.zohoapis.com/crm/v2/'
2018-06-21 15:28:57 +02:00
2018-06-21 18:09:22 +02:00
const sendRequest = (options, res) => {
request(options, (error, response, body) => {
if (error) res.send(error)
2018-06-22 13:05:58 +02:00
// just pass through whatever we get from the APIs
// as the response
2018-06-21 18:09:22 +02:00
res.send(body)
})
}
2018-06-26 13:50:38 +02:00
server.get('/', (req, res) => {
res.send(`
<h3>Please provide one of these endpoints:</h3>
- <code>/newsletter/:data</code><br />
- <code>/crm/:data</code>
`)
})
//
// Subscribe to newsletter via Zoho Campaigns API
// https://www.zoho.com/campaigns/newhelp/api/contact-subscribe.html
//
2018-06-26 13:50:38 +02:00
server.get('/newsletter', (req, res) => {
res.send(`
<h3>Please pass your data in the following format</h3>
- <code>/newsletter/{Contact Email:info@oceanprotocol.com}</code><br />
`)
})
server.get('/newsletter/:data', (req, res) => {
2018-06-21 15:28:57 +02:00
const { ZOHO_CAMPAIGNS_TOKEN, ZOHO_CAMPAIGNS_LIST_KEY } = req.webtaskContext.secrets
const { data } = req.params
const options = {
2018-06-27 20:52:40 +02:00
url: `${apiUrlZohoCampaigns}json/listsubscribe?authtoken=${ZOHO_CAMPAIGNS_TOKEN}&scope=CampaignsAPI&resfmt=JSON&listkey=${ZOHO_CAMPAIGNS_LIST_KEY}&contactinfo=${decodeURIComponent(data)}`
}
2018-06-21 18:09:22 +02:00
sendRequest(options, res)
})
2018-06-21 17:54:41 +02:00
//
// Create a new lead via Zoho CRM API
// https://www.zoho.com/crm/help/api/v2/#create-specify-records
//
2018-06-26 13:50:38 +02:00
server.get('/crm', (req, res) => {
res.send(`<h3>Please pass your data in the following format</h3>
2018-06-27 20:52:40 +02:00
- <code>/crm/{First Name:First Name, Last Name:Last Name,Contact Email:info@oceanprotocol.com}</code>`)
2018-06-26 13:50:38 +02:00
})
2018-06-21 17:54:41 +02:00
server.get('/crm/:data', (req, res) => {
const { ZOHO_CRM_TOKEN } = req.webtaskContext.secrets
2018-06-21 17:54:41 +02:00
const { data } = req.params
2018-06-21 17:54:41 +02:00
const options = {
2018-06-25 16:45:26 +02:00
url: `${apiUrlZohoCRM}Leads`,
2018-06-21 17:54:41 +02:00
headers: { 'Authorization': `Zoho-oauthtoken ${ZOHO_CRM_TOKEN}` },
method: 'POST',
formData: data
}
2018-06-21 18:09:22 +02:00
sendRequest(options, res)
2018-06-21 17:54:41 +02:00
})
2018-06-21 08:29:35 +02:00
module.exports = Webtask.fromExpress(server)