Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4639 from LevwTech/main
- Loading branch information
Showing
11 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"extends": [ | ||
"../../../../.eslintrc.base.json" | ||
], | ||
"ignorePatterns": [ | ||
"!**/*" | ||
], | ||
"overrides": [ | ||
{ | ||
"files": [ | ||
"*.ts", | ||
"*.tsx", | ||
"*.js", | ||
"*.jsx" | ||
], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": [ | ||
"*.ts", | ||
"*.tsx" | ||
], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": [ | ||
"*.js", | ||
"*.jsx" | ||
], | ||
"rules": {} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "@activepieces/piece-whatsapp", | ||
"version": "0.0.1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
70
packages/pieces/community/whatsapp/src/lib/actions/send-media.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
43
packages/pieces/community/whatsapp/src/lib/actions/send-message.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters