Skip to content

Commit

Permalink
Merge pull request #4642 from thirstycode/main
Browse files Browse the repository at this point in the history
feat(poper): New Lead Trigger
  • Loading branch information
kishanprmr committed May 8, 2024
2 parents 6f3f537 + 8e9487c commit fc9226d
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/pieces/community/poper/.eslintrc.json
@@ -0,0 +1,33 @@
{
"extends": [
"../../../../.eslintrc.base.json"
],
"ignorePatterns": [
"!**/*"
],
"overrides": [
{
"files": [
"*.ts",
"*.tsx",
"*.js",
"*.jsx"
],
"rules": {}
},
{
"files": [
"*.ts",
"*.tsx"
],
"rules": {}
},
{
"files": [
"*.js",
"*.jsx"
],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions packages/pieces/community/poper/README.md
@@ -0,0 +1,7 @@
# pieces-poper

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build pieces-poper` to build the library.
4 changes: 4 additions & 0 deletions packages/pieces/community/poper/package.json
@@ -0,0 +1,4 @@
{
"name": "@activepieces/piece-poper",
"version": "0.0.1"
}
38 changes: 38 additions & 0 deletions packages/pieces/community/poper/project.json
@@ -0,0 +1,38 @@
{
"name": "pieces-poper",
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/pieces/community/poper/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": [
"{options.outputPath}"
],
"options": {
"outputPath": "dist/packages/pieces/community/poper",
"tsConfig": "packages/pieces/community/poper/tsconfig.lib.json",
"packageJson": "packages/pieces/community/poper/package.json",
"main": "packages/pieces/community/poper/src/index.ts",
"assets": [
"packages/pieces/community/poper/*.md"
],
"buildableProjectDepsInPackageJsonType": "dependencies",
"updateBuildableProjectDepsInPackageJson": true
}
},
"publish": {
"command": "node tools/scripts/publish.mjs pieces-poper {args.ver} {args.tag}",
"dependsOn": [
"build"
]
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": [
"{options.outputFile}"
]
}
},
"tags": []
}
56 changes: 56 additions & 0 deletions packages/pieces/community/poper/src/index.ts
@@ -0,0 +1,56 @@
import { createPiece, PieceAuth } from '@activepieces/pieces-framework';
import { newLead } from './lib/triggers/new-lead';
import { HttpMethod, HttpRequest, httpClient } from '@activepieces/pieces-common';
import { PieceCategory } from '@activepieces/shared';

export const poperAuth = PieceAuth.SecretText({
displayName: 'API Key',
description:
'Use the Poper API key to authenticate the piece. You can find your API key in the Poper settings.',
required: true,
validate: async ({ auth }) => {
const request: HttpRequest = {
method: HttpMethod.POST,
url: 'https://api.poper.ai/general/v1/ping',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: {
api_key: auth,
},
};

try {
const res = await httpClient.sendRequest(request);

if (res.status === 200) {
return {
valid: true,
};
}

return {
valid: false,
error: 'API Key is invalid',
};
} catch (e) {
return {
valid: false,
error: 'API Key is invalid',
};
}
},
});

export const poper = createPiece({
displayName: 'Poper',
auth: poperAuth,
minimumSupportedRelease: '0.20.0',
categories: [PieceCategory.MARKETING],
description:
'AI Driven Pop-up Builder that can convert visitors into customers,increase subscriber count, and skyrocket sales.',
logoUrl: 'https://cdn.activepieces.com/pieces/poper.png',
authors: ['thirstycode'],
actions: [],
triggers: [newLead],
});
143 changes: 143 additions & 0 deletions packages/pieces/community/poper/src/lib/triggers/new-lead.ts
@@ -0,0 +1,143 @@

import { createTrigger, TriggerStrategy, PiecePropValueSchema, StaticPropsValue, DropdownProperty } from '@activepieces/pieces-framework';
import { DedupeStrategy, Polling, pollingHelper, HttpMethod, HttpRequest, httpClient } from '@activepieces/pieces-common';
import { poperAuth } from './../../';

import { Property } from '@activepieces/pieces-framework';

const buildEmptyList = ({ placeholder }: { placeholder: string }) => {
return {
disabled: true,
options: [],
placeholder,
};
};

const popupsDropdown = Property.Dropdown<string>({
displayName: 'Popup Name',
refreshers: [],
description: "Select the popup name to trigger the action",
required: true,
options: async ({ auth }) => {
if (!auth) {
return buildEmptyList({
placeholder: 'Please select an authentication',
});
}

const request: HttpRequest = {
method: HttpMethod.POST,
url: 'https://api.poper.ai/general/v1/popup/list',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: {
api_key: auth,
}
};

try{

const res = await httpClient.sendRequest(request);

if(res.status === 200){
const popups = res.body.popups;
if (popups.length === 0) {
return buildEmptyList({
placeholder: 'No popups found! Please create a popup.',
});
}

const options = popups.map((p:any) => ({
label: p.name,
value: p.id,
}));

return {
disabled: false,
options,
};
}

return buildEmptyList({
placeholder: 'Not authorized.',
});
} catch(e){
return buildEmptyList({
placeholder: 'Not authorized.',
});
}

},
});

// replace auth with piece auth variable
const polling: Polling< PiecePropValueSchema<typeof poperAuth>, StaticPropsValue<{ popup_id: DropdownProperty<string, false> | DropdownProperty<string, true>; }> > = {
strategy: DedupeStrategy.LAST_ITEM,
items: async ({ auth, propsValue }) => {
// implement the logic to fetch the items
const request: HttpRequest = {
method: HttpMethod.POST,
url: 'https://api.poper.ai/general/v1/popup/responses',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: {
api_key: auth,
popup_id: propsValue.popup_id,
}
};

try{

const res = await httpClient.sendRequest(request);

if(res.status === 200){
const responses = res.body.responses;
if (responses.length === 0) {
return [];
}

return responses.map((response:any) => ({
id: response.id,
data: response,
}));
}

return [];
} catch(e){
return [];
}
},

}

export const newLead = createTrigger({
auth: poperAuth,
name: 'newLead',
displayName: 'New Lead',
description: 'Triggers when a new lead is obtained from popup',
props: {
popup_id: popupsDropdown,
},
sampleData: {},
type: TriggerStrategy.POLLING,
async test(context) {
const { store, auth, propsValue } = context;
return await pollingHelper.test(polling, { store, auth, propsValue });
},
async onEnable(context) {
const { store, auth, propsValue } = context;
await pollingHelper.onEnable(polling, { store, auth, propsValue });
},

async onDisable(context) {
const { store, auth, propsValue } = context;
await pollingHelper.onDisable(polling, { store, auth, propsValue });
},

async run(context) {
const { store, auth, propsValue } = context;
return await pollingHelper.poll(polling, { store, auth, propsValue });
},
});
19 changes: 19 additions & 0 deletions packages/pieces/community/poper/tsconfig.json
@@ -0,0 +1,19 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
}
]
}
11 changes: 11 additions & 0 deletions packages/pieces/community/poper/tsconfig.lib.json
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}
3 changes: 3 additions & 0 deletions tsconfig.base.json
Expand Up @@ -390,6 +390,9 @@
"@activepieces/piece-pipedrive": [
"packages/pieces/community/pipedrive/src/index.ts"
],
"@activepieces/piece-poper": [
"packages/pieces/community/poper/src/index.ts"
],
"@activepieces/piece-postgres": [
"packages/pieces/community/postgres/src/index.ts"
],
Expand Down

0 comments on commit fc9226d

Please sign in to comment.