-
Notifications
You must be signed in to change notification settings - Fork 129
Feat implement whatsapp with vonage bun #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
loks0n
merged 69 commits into
appwrite:main
from
parthj000:feat-implement-whatsapp-with-vonage-bun
Nov 16, 2023
Merged
Changes from 59 commits
Commits
Show all changes
69 commits
Select commit
Hold shift + click to select a range
ac98126
changed
parthj000 32cfe08
Update main.js
parthj000 c1b544c
Update main.js
parthj000 bc3130c
Update main.js
parthj000 bce9e83
Update main.js
parthj000 6052899
Update main.js
parthj000 80cf258
Create .env
parthj000 e4f200b
Update main.js
parthj000 21b1978
Update .env
parthj000 64eace3
Create README.md
parthj000 a9c27dd
Update README.md
parthj000 e536379
Update README.md
parthj000 51053c4
Create main.ts
parthj000 d772e92
Create READMD.md
parthj000 232ecbd
Create README.md
parthj000 f390360
Create package.json
parthj000 4fbdbfc
Delete bun/starter/whatsapp-with-vonage/READMD.md
parthj000 17fa28d
Create starter
parthj000 2d71a53
Create starter
parthj000 4ba3f84
Update main.js
parthj000 aec80de
Update .env
parthj000 9add5cd
Update README.md
parthj000 65c0bc6
Delete bun/starter/starter
parthj000 4bad318
Delete bun/starter/src/starter
parthj000 fa8def5
Update main.js
parthj000 bfb38d5
Update main.js
parthj000 c42d368
Update README.md
parthj000 82539c2
Delete bun/starter/README.md
parthj000 5b79d93
Delete bun/starter/src/main.ts
parthj000 b9540f4
Update package.json
parthj000 d25ed1a
Delete bun/starter/.gitignore
parthj000 776c134
Delete bun/starter/bun.lockb
parthj000 f28f9cb
Delete bun/starter/package.json
parthj000 4fedd82
Create index.js
parthj000 400ce83
Delete bun/starter/src directory
parthj000 8375b47
Create whatsapp-with-vonage
parthj000 2c0d130
Delete bun/whatsapp-with-vonage
parthj000 c94053f
Create start
parthj000 1a80631
Delete bun/starter/start
parthj000 041385c
Add files via upload
parthj000 88bb65a
Delete bun/starter/starter/whatsapp-with-vonage directory
parthj000 f0a2bae
Create .gitignore
parthj000 e62f6e5
Update .gitignore
parthj000 13284a0
update
parthj000 2e90499
Update main.js
parthj000 3cb0af2
Update main.js
parthj000 c469334
required changes done
parthj000 8c98e3d
change
parthj000 74fe64a
ADDED
parthj000 245c2fd
added changes
parthj000 287d893
updated readme
parthj000 3fff99c
updated
parthj000 c4ec3f2
added payload tampering verification
parthj000 be6cace
added
parthj000 0af2030
updated Readme.md
parthj000 4506f42
updated package.json
parthj000 53dbd0e
added utils.ts
parthj000 a4a5544
added utils.ts
parthj000 419266e
added
parthj000 4549b5d
commited changes
parthj000 2032d5f
commited changes
parthj000 3279b81
small changes
parthj000 66c3f1c
Update main.ts
parthj000 9d5335f
Update utils.ts
parthj000 89b39fc
Update index.html
parthj000 d3ad218
small_changes
parthj000 4be2ecd
small changes
parthj000 df49d81
updated
parthj000 49c2158
updated
parthj000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,111 @@ | ||
| ## 💬 Bun Whatsapp bot with Vonage Function | ||
|
|
||
| Simple bot to answer WhatsApp messages. | ||
|
|
||
| ## 🧰 Usage | ||
|
|
||
| ## GET/ | ||
|
|
||
| HTML form interacting with the function. | ||
|
|
||
| ## POST/ | ||
|
parthj000 marked this conversation as resolved.
Outdated
|
||
|
|
||
| Receives a message, validates its signature, and sends a response back to the sender. | ||
|
|
||
| | Name | Description | Location | Type | Sample Value | | ||
| | ------------- | ---------------------------------- | -------- | ------------------- | -------------------- | | ||
| | Content-Type | Content type of the request | Header | `application/json ` | N/A | | ||
| | Authorization | Webhook signature for verification | Header | String | `Bearer <signature>` | | ||
| | from | Sender's identifier. | Body | String | `user` | | ||
| | text | Text content of the message. | Body | String | `Hello World` | | ||
|
|
||
| **Response:** | ||
|
|
||
| Sample `200` Response: | ||
|
|
||
| ```json | ||
| { | ||
| "ok": true | ||
| } | ||
| ``` | ||
|
|
||
| Sample `400` Response: | ||
|
|
||
| ```json | ||
| { | ||
| "ok": false, | ||
| "error": "can't verify" | ||
|
parthj000 marked this conversation as resolved.
Outdated
|
||
| } | ||
| ``` | ||
|
|
||
| Sample `401` Response: | ||
|
|
||
| ```json | ||
| { | ||
| "ok": false, | ||
| "error": "Payload hash mismatch." | ||
| } | ||
| ``` | ||
|
|
||
| ## POST/ | ||
|
|
||
| Webhooks sent by vonage to update about the status of message | ||
|
|
||
|
loks0n marked this conversation as resolved.
Outdated
|
||
| | Name | Description | Location | Type | Sample Value | | ||
| | ------ | ----------------------------- | -------- | ------ | ------------------------- | | ||
| | Status | showing the status of message | body | string | submitted/read/delievered | | ||
|
|
||
| **Response:** | ||
|
|
||
| ```json | ||
| { | ||
| "ok": 200, | ||
| "status": "delievered/submitted/read" | ||
|
parthj000 marked this conversation as resolved.
Outdated
|
||
| } | ||
| ``` | ||
|
|
||
| ## VONAGE_API_KEY | ||
|
|
||
| API Key to use the Vonage API. | ||
|
|
||
| | Question | Answer | | ||
| | ------------ | --------- | | ||
| | Required | Yes | | ||
| | Sample Value | `P2...97` | | ||
|
|
||
| ## VONAGE_WHATSAPP_NUMBER | ||
|
|
||
| Vonage WhatsApp number to send messages from. | ||
|
|
||
| | Question | Answer | | ||
| | ------------ | ------------ | | ||
| | Required | Yes | | ||
| | Sample Value | `1234567890` | | ||
|
|
||
| ## VONAGE_ACCOUNT_SECRET | ||
|
|
||
| ACCOUNT_SECRET to use the Vonage API. | ||
|
|
||
| | Question | Answer | | ||
| | ------------ | ----------- | | ||
| | Required | Yes | | ||
| | Sample Value | `p524...97` | | ||
|
|
||
| ## VONAGE_SIGNATURE_SECRET | ||
|
|
||
| Secret to verify the webhooks sent by Vonage. | ||
|
|
||
| | Question | Answer | | ||
| | ------------ | ------------------ | | ||
| | Required | Yes | | ||
| | Sample Value | `ZACYi3...ZXGjsch` | | ||
|
|
||
| ## CONFIGURATION | ||
|
|
||
| | Setting | Value | | ||
| | ----------------- | -------------- | | ||
| | Runtime | Bun (1.0) | | ||
| | Entrypoint | `src/main.ts` | | ||
| | Build Commands | `bun install ` | | ||
| | Permissions | `any` | | ||
| | Timeout (Seconds) | 15 | | ||
This file contains hidden or 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,65 @@ | ||
| import jwt from "jsonwebtoken"; | ||
| import { fetch } from "undici"; | ||
| import { getStaticFiles } from "./utils"; | ||
|
|
||
| export default async ({ req, res, log, error }) => { | ||
|
parthj000 marked this conversation as resolved.
Outdated
|
||
| if (req.method === "GET") { | ||
| return res.send(getStaticFiles, 200, { | ||
| "Content-Type": "text/html; charset=utf-8", | ||
| }); | ||
|
parthj000 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| const hasher = new Bun.CryptoHasher("sha256"); | ||
| const hashedBuffer = hasher.update(JSON.stringify(req.body)).digest("hex"); | ||
| const hashedValue = hashedBuffer.toString(); | ||
|
|
||
| const authHeader = req.headers.authorization.split(" ")[1]; | ||
| const decodedToken = jwt.verify(authHeader, Bun.env.VONAGE_SIGNATURE_SECRET, { | ||
| algorithms: ["HS256"], | ||
| }); | ||
|
|
||
| if (hashedValue != decodedToken["payload_hash"]) { | ||
| return res.json( | ||
| { | ||
| ok: false, | ||
| error: "Payload hash mismatched", | ||
| }, | ||
| 401 | ||
| ); | ||
| } | ||
|
parthj000 marked this conversation as resolved.
Outdated
|
||
|
|
||
| const secret = `${Bun.env.VONAGE_API_KEY}:${Bun.env.VONAGE_ACCOUNT_SECRET}`; | ||
|
parthj000 marked this conversation as resolved.
Outdated
|
||
| const basicAuthToken = btoa(secret); | ||
|
|
||
| if (req.body.text == null) { | ||
| return res.json( | ||
| { | ||
| ok: true, | ||
| status: req.body.status, | ||
| }, | ||
| 200 | ||
| ); | ||
| } | ||
|
|
||
| await fetch(`https://messages-sandbox.nexmo.com/v1/messages`, { | ||
| method: "POST", | ||
| body: JSON.stringify({ | ||
| from: Bun.env.VONAGE_WHATSAPP_NUMBER, | ||
| to: req.body.from, | ||
| message_type: "text", | ||
| text: `You sent me: ${req.body.text}`, | ||
| channel: "whatsapp", | ||
| }), | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: `Basic ${basicAuthToken}`, | ||
| }, | ||
| }); | ||
|
|
||
| return res.json( | ||
| { | ||
| ok: true, | ||
| }, | ||
| 200 | ||
| ); | ||
|
parthj000 marked this conversation as resolved.
Outdated
|
||
| }; | ||
This file contains hidden or 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,5 @@ | ||
| export async function getStaticFiles() { | ||
|
parthj000 marked this conversation as resolved.
Outdated
|
||
| const fileName = Bun.resolveSync("../static/index.html", import.meta.dir); | ||
| const html = await Bun.file(fileName).text(); | ||
| return html; | ||
| } | ||
This file contains hidden or 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,34 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>WhatsApp Bot with Vonage</title> | ||
|
|
||
| <link rel="stylesheet" href="https://unpkg.com/@appwrite.io/pink" /> | ||
| <link rel="stylesheet" href="https://unpkg.com/@appwrite.io/pink-icons" /> | ||
| </head> | ||
| <body> | ||
| <main class="main-content"> | ||
| <div class="top-cover u-padding-block-end-56"> | ||
| <div class="container"> | ||
| <div | ||
| class="u-flex u-gap-16 u-flex-justify-center u-margin-block-start-16" | ||
| > | ||
| <h1 class="heading-level-1">WhatsApp Bot with Vonage</h1> | ||
| <code class="u-un-break-text"></code> | ||
| </div> | ||
| <p | ||
| class="body-text-1 u-normal u-margin-block-start-8" | ||
| style="max-width: 50rem" | ||
| > | ||
| This function listens to incoming webhooks from Vonage regarding | ||
| WhatsApp messages, and responds to them. To use the function, send | ||
| message to the WhatsApp user provided by Vonage. | ||
| </p> | ||
| </div> | ||
| </div> | ||
| </main> | ||
| </body> | ||
| </html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.