Skip to content

Commit

Permalink
Merge pull request #236 from Secreto31126/gas-support
Browse files Browse the repository at this point in the history
GAS support
  • Loading branch information
Secreto31126 authored Sep 24, 2023
2 parents ee7552e + 7427ef6 commit d87c04b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
72 changes: 66 additions & 6 deletions ENVIRONMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const WhatsAppAPI = require("whatsapp-api-js").default;
For each version of Node, you can use the `setup` function to simplify the
process.

- Node 19 and above (using Express):
- Node 19 and above (using Express and it's middleware):

```js
import WhatsAppAPI from "whatsapp-api-js/middleware/express";
Expand Down Expand Up @@ -154,10 +154,70 @@ HTML module example:

## Google App Script

In the transition to v1, Google App Script support was lost, but we are working
on bringing it back.
The library is exported as standalone to Google App Script. You can import it
using the code:

In the meantime, check out Secreto31126/whatsapp-api-google-app-script. It's
outdated, unsuported and not recommended, but it gets the job done.
```
1iTMl1x_CayBWuLYBBZH0DM72eXqjuavZ0nAwSZ6y3jj9ELXOMckcHmW6
```

Unfortunately, type definitions, middlewares and setup methods aren't available,
as the standalone version doesn't include them while bundling.

```js
const { WhatsAppAPI, Text } = WhatsAppScript; // Or whatever name you gave to the library

(I still use it :)
async function fetchPonyfill(url, options = {}) {
if (options?.body) {
options.payload = options.body;
delete options.body;
}

if (options?.headers?.["Content-Type"]) {
options.contentType = options.headers["Content-Type"];
delete options.headers["Content-Type"];
}

if (options?.method) {
options.method = options.method.toLowerCase();
}

const request = UrlFetchApp.fetch(url, {
...options,
muteHttpExceptions: true
});

return {
text: async () => request.getContentText,
json: async () => JSON.parse(request.getContentText())
};
}

const Whatsapp = new WhatsAppAPI({
token: YOUR_TOKEN,
webhookVerifyToken: YOUR_WEBHOOK_VERIFY_TOKEN,
// GAS doesn't support crypto nor getting the headers of a request, there's no way to verify payloads
secure: false,
ponyfill: {
fetch: fetchPonyfill
}
});

Whatsapp.on.message = ({ phoneID, from, name }) => {
Whatsapp.sendMessage(phoneID, from, new Text(`Hello ${name}!`));
};

async function doPost(e) {
const data = JSON.parse(e.postData.contents);

let status_code;
try {
status_code = await Whatsapp.post(data);
} catch (e) {
status_code = e;
}

// GAS doesn't support sending custom status codes
return ContentService.createTextOutput(status_code);
}
```
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ const APP_SECRET = "YOUR_SECRET";
const Whatsapp = new WhatsAppAPI({ token: TOKEN, appSecret: APP_SECRET });

// Assuming post is called on a POST request to your server
function post(e) {
async function post(e) {
// The handlers work with any framework, as long as you pass the correct data
// You can also use one of the middlewares provided in the package, keep reading
return Whatsapp.post(
return await Whatsapp.post(
JSON.parse(e.data),
e.data,
e.headers["x-hub-signature-256"]
Expand Down Expand Up @@ -142,6 +142,7 @@ const Whatsapp = new WhatsAppAPI({
});

// Assuming get is called on a GET request to your server
// You can also use one of the middlewares provided in the package, keep reading
function get(e) {
return Whatsapp.get(e.query);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whatsapp-api-js",
"version": "2.1.0",
"version": "2.1.1",
"author": "Secreto31126",
"description": "A TypeScript server agnostic Whatsapp's Official API framework",
"license": "MIT",
Expand Down

0 comments on commit d87c04b

Please sign in to comment.