Skip to content
Merged
Show file tree
Hide file tree
Changes from 59 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
ac98126
changed
parthj000 Oct 31, 2023
32cfe08
Update main.js
parthj000 Oct 31, 2023
c1b544c
Update main.js
parthj000 Oct 31, 2023
bc3130c
Update main.js
parthj000 Oct 31, 2023
bce9e83
Update main.js
parthj000 Oct 31, 2023
6052899
Update main.js
parthj000 Oct 31, 2023
80cf258
Create .env
parthj000 Oct 31, 2023
e4f200b
Update main.js
parthj000 Oct 31, 2023
21b1978
Update .env
parthj000 Oct 31, 2023
64eace3
Create README.md
parthj000 Oct 31, 2023
a9c27dd
Update README.md
parthj000 Oct 31, 2023
e536379
Update README.md
parthj000 Oct 31, 2023
51053c4
Create main.ts
parthj000 Oct 31, 2023
d772e92
Create READMD.md
parthj000 Oct 31, 2023
232ecbd
Create README.md
parthj000 Oct 31, 2023
f390360
Create package.json
parthj000 Oct 31, 2023
4fbdbfc
Delete bun/starter/whatsapp-with-vonage/READMD.md
parthj000 Oct 31, 2023
17fa28d
Create starter
parthj000 Oct 31, 2023
2d71a53
Create starter
parthj000 Oct 31, 2023
4ba3f84
Update main.js
parthj000 Oct 31, 2023
aec80de
Update .env
parthj000 Oct 31, 2023
9add5cd
Update README.md
parthj000 Oct 31, 2023
65c0bc6
Delete bun/starter/starter
parthj000 Oct 31, 2023
4bad318
Delete bun/starter/src/starter
parthj000 Oct 31, 2023
fa8def5
Update main.js
parthj000 Oct 31, 2023
bfb38d5
Update main.js
parthj000 Oct 31, 2023
c42d368
Update README.md
parthj000 Oct 31, 2023
82539c2
Delete bun/starter/README.md
parthj000 Oct 31, 2023
5b79d93
Delete bun/starter/src/main.ts
parthj000 Oct 31, 2023
b9540f4
Update package.json
parthj000 Oct 31, 2023
d25ed1a
Delete bun/starter/.gitignore
parthj000 Oct 31, 2023
776c134
Delete bun/starter/bun.lockb
parthj000 Oct 31, 2023
f28f9cb
Delete bun/starter/package.json
parthj000 Oct 31, 2023
4fedd82
Create index.js
parthj000 Oct 31, 2023
400ce83
Delete bun/starter/src directory
parthj000 Oct 31, 2023
8375b47
Create whatsapp-with-vonage
parthj000 Oct 31, 2023
2c0d130
Delete bun/whatsapp-with-vonage
parthj000 Oct 31, 2023
c94053f
Create start
parthj000 Oct 31, 2023
1a80631
Delete bun/starter/start
parthj000 Oct 31, 2023
041385c
Add files via upload
parthj000 Oct 31, 2023
88bb65a
Delete bun/starter/starter/whatsapp-with-vonage directory
parthj000 Oct 31, 2023
f0a2bae
Create .gitignore
parthj000 Oct 31, 2023
e62f6e5
Update .gitignore
parthj000 Oct 31, 2023
13284a0
update
parthj000 Oct 31, 2023
2e90499
Update main.js
parthj000 Oct 31, 2023
3cb0af2
Update main.js
parthj000 Nov 3, 2023
c469334
required changes done
parthj000 Nov 3, 2023
8c98e3d
change
parthj000 Nov 3, 2023
74fe64a
ADDED
parthj000 Nov 4, 2023
245c2fd
added changes
parthj000 Nov 6, 2023
287d893
updated readme
parthj000 Nov 6, 2023
3fff99c
updated
parthj000 Nov 6, 2023
c4ec3f2
added payload tampering verification
parthj000 Nov 6, 2023
be6cace
added
parthj000 Nov 6, 2023
0af2030
updated Readme.md
parthj000 Nov 7, 2023
4506f42
updated package.json
parthj000 Nov 7, 2023
53dbd0e
added utils.ts
parthj000 Nov 7, 2023
a4a5544
added utils.ts
parthj000 Nov 7, 2023
419266e
added
parthj000 Nov 7, 2023
4549b5d
commited changes
parthj000 Nov 9, 2023
2032d5f
commited changes
parthj000 Nov 9, 2023
3279b81
small changes
parthj000 Nov 12, 2023
66c3f1c
Update main.ts
parthj000 Nov 12, 2023
9d5335f
Update utils.ts
parthj000 Nov 12, 2023
89b39fc
Update index.html
parthj000 Nov 15, 2023
d3ad218
small_changes
parthj000 Nov 16, 2023
4be2ecd
small changes
parthj000 Nov 16, 2023
df49d81
updated
parthj000 Nov 16, 2023
49c2158
updated
parthj000 Nov 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions bun/whatsapp-with-vonage/README.md
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/
Comment thread
parthj000 marked this conversation as resolved.
Outdated

HTML form interacting with the function.

## POST/
Comment thread
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"
Comment thread
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

Comment thread
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"
Comment thread
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 |
65 changes: 65 additions & 0 deletions bun/whatsapp-with-vonage/src/main.ts
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 }) => {
Comment thread
parthj000 marked this conversation as resolved.
Outdated
if (req.method === "GET") {
return res.send(getStaticFiles, 200, {
"Content-Type": "text/html; charset=utf-8",
});
Comment thread
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
);
}
Comment thread
parthj000 marked this conversation as resolved.
Outdated

const secret = `${Bun.env.VONAGE_API_KEY}:${Bun.env.VONAGE_ACCOUNT_SECRET}`;
Comment thread
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
);
Comment thread
parthj000 marked this conversation as resolved.
Outdated
};
5 changes: 5 additions & 0 deletions bun/whatsapp-with-vonage/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export async function getStaticFiles() {
Comment thread
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;
}
34 changes: 34 additions & 0 deletions bun/whatsapp-with-vonage/static/index.html
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>