Skip to content

Commit

Permalink
Merge pull request #4639 from LevwTech/main
Browse files Browse the repository at this point in the history
  • Loading branch information
abuaboud committed May 9, 2024
2 parents b5c8db2 + 90ed969 commit 9c04609
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/pieces/community/whatsapp/.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/whatsapp/README.md
@@ -0,0 +1,7 @@
# pieces-whatsapp

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

## Building

Run `nx build pieces-whatsapp` to build the library.
4 changes: 4 additions & 0 deletions packages/pieces/community/whatsapp/package.json
@@ -0,0 +1,4 @@
{
"name": "@activepieces/piece-whatsapp",
"version": "0.0.1"
}
38 changes: 38 additions & 0 deletions packages/pieces/community/whatsapp/project.json
@@ -0,0 +1,38 @@
{
"name": "pieces-whatsapp",
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/pieces/community/whatsapp/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": [
"{options.outputPath}"
],
"options": {
"outputPath": "dist/packages/pieces/community/whatsapp",
"tsConfig": "packages/pieces/community/whatsapp/tsconfig.lib.json",
"packageJson": "packages/pieces/community/whatsapp/package.json",
"main": "packages/pieces/community/whatsapp/src/index.ts",
"assets": [
"packages/pieces/community/whatsapp/*.md"
],
"buildableProjectDepsInPackageJsonType": "dependencies",
"updateBuildableProjectDepsInPackageJson": true
}
},
"publish": {
"command": "node tools/scripts/publish.mjs pieces-whatsapp {args.ver} {args.tag}",
"dependsOn": [
"build"
]
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": [
"{options.outputFile}"
]
}
},
"tags": []
}
47 changes: 47 additions & 0 deletions packages/pieces/community/whatsapp/src/index.ts
@@ -0,0 +1,47 @@

import { createPiece, PieceAuth, Property } from "@activepieces/pieces-framework";
import { sendMessage } from "./lib/actions/send-message";
import { sendMedia } from "./lib/actions/send-media";

const markdown = `
To Obtain a Phone Number ID and a Permanent System User Access Token, follow these steps:
1. Go to https://developers.facebook.com/
2. Make a new app, Select Other for usecase.
3. Choose Business as the type of app.
4. Add new Product -> WhatsApp.
5. Navigate to WhatsApp Settings > API Setup.
6. Select a phone number and copy its Phone number ID.
7. Login to your [Meta Business Manager](https://business.facebook.com/).
8. Click on Settings.
9. Create a new System User with access over the app and copy the access token.
`;

export const whatsappAuth = PieceAuth.CustomAuth({
required: true,
description: markdown,
props: {
access_token: PieceAuth.SecretText({
displayName: 'System User Access Token',
description: 'The system user access token of your WhatsApp business account',
required: true,
}),
phoneNumberId: Property.ShortText({
displayName: 'Phone Number ID',
description: 'The phone number ID of your WhatsApp business account',
required: true,
}),
},
});


export const whatsapp = createPiece({
displayName: "WhatsApp Business",
description: 'Manage your WhatsApp business account',
auth: whatsappAuth,
minimumSupportedRelease: '0.20.0',
logoUrl: "https://cdn.activepieces.com/pieces/whatsapp.png",
authors: ["LevwTech"],
actions: [sendMessage, sendMedia],
triggers: [],
});
70 changes: 70 additions & 0 deletions packages/pieces/community/whatsapp/src/lib/actions/send-media.ts
@@ -0,0 +1,70 @@
import { createAction, Property } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { whatsappAuth } from '../..';
import { supportedMediaTypes, capitalizeFirstLetter, mediaTypeSupportsCaption } from '../common/utils';

export const sendMedia = createAction({
auth: whatsappAuth,
name: 'sendMedia',
displayName: 'Send Media',
description: 'Send a media message through WhatsApp',
props: {
to: Property.ShortText({
displayName: 'To',
description: 'The recipient of the message',
required: true,
}),
type: Property.Dropdown({
displayName: 'Type',
description: 'The type of media to send',
required: true,
options: async () => {
return {
options: supportedMediaTypes.map((type) => ({
label: capitalizeFirstLetter(type),
value: type,
})),
};
},
refreshers: []
}),
media: Property.ShortText({
displayName: 'Media URL',
description: 'The URL of the media to send',
required: true,
}),
caption: Property.LongText({
displayName: 'Caption',
description: 'A caption for the media',
required: false,
}),
filename: Property.LongText({
displayName: 'Filename',
description: 'Filename of the document to send',
required: false,
}),
},
async run(context) {
const { to, caption, media, type, filename } = context.propsValue;
const { access_token, phoneNumberId } = context.auth;
const body = {
messaging_product: "whatsapp",
recipient_type: "individual",
to,
type,
[type]: {
link: media,
}
}
if (caption && mediaTypeSupportsCaption(type)) (body[type] as any).caption = caption;
if (filename && type === 'document') (body[type] as any).filename = filename;
return await httpClient.sendRequest({
method: HttpMethod.POST,
url: `https://graph.facebook.com/v17.0/${phoneNumberId}/messages`,
headers: {
Authorization: 'Bearer ' + access_token,
},
body,
});
},
});
43 changes: 43 additions & 0 deletions packages/pieces/community/whatsapp/src/lib/actions/send-message.ts
@@ -0,0 +1,43 @@
import { createAction, Property } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { whatsappAuth } from '../..';


export const sendMessage = createAction({
auth: whatsappAuth,
name: 'sendMessage',
displayName: 'Send Message',
description: 'Send a text message through WhatsApp',
props: {
to: Property.ShortText({
displayName: 'To',
description: 'The recipient of the message',
required: true,
}),
text: Property.LongText({
displayName: 'Message',
description: 'The message to send',
required: true,
}),
},
async run(context) {
const { to, text } = context.propsValue;
const { access_token, phoneNumberId } = context.auth;
return await httpClient.sendRequest({
method: HttpMethod.POST,
url: `https://graph.facebook.com/v17.0/${phoneNumberId}/messages`,
headers: {
Authorization: 'Bearer ' + access_token,
},
body: {
messaging_product: "whatsapp",
recipient_type: "individual",
to,
type: "text",
text: {
body: text
}
}
});
},
});
3 changes: 3 additions & 0 deletions packages/pieces/community/whatsapp/src/lib/common/utils.ts
@@ -0,0 +1,3 @@
export const supportedMediaTypes = ['image', 'audio', 'document', 'sticker', 'video'];
export const capitalizeFirstLetter = (word: string) => word.charAt(0).toUpperCase() + word.slice(1);
export const mediaTypeSupportsCaption = (type: string) => ['image', 'video', 'document'].includes(type);
19 changes: 19 additions & 0 deletions packages/pieces/community/whatsapp/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/whatsapp/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", "src/lib/actions/common/utils.ts"]
}
3 changes: 3 additions & 0 deletions tsconfig.base.json
Expand Up @@ -556,6 +556,9 @@
"@activepieces/piece-whatsable": [
"packages/pieces/community/whatsable/src/index.ts"
],
"@activepieces/piece-whatsapp": [
"packages/pieces/community/whatsapp/src/index.ts"
],
"@activepieces/piece-woocommerce": [
"packages/pieces/community/woocommerce/src/index.ts"
],
Expand Down

0 comments on commit 9c04609

Please sign in to comment.