Skip to content
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

Update README, clarify license, reorganize project structure, and add WhatsApp Cloud API stub #25

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ possible.
## Pull Requests
We actively welcome your pull requests.

1. Fork the repo and create your branch from `master`.
1. Fork the repo and create your branch from `main`.
1. If you're adding a new sample application in a different programming language than our existing samples, please create the sample using the folder structure:
1. The Facebook Platform (e.g. `instagram-subscriptions`).
1. The Cloud platform to which the sample is specific, if relevant (e.g., `heroku`, `aws`).
1. The primary programming language of the application (e.g., `nodejs`).
1. The sample appliation name, ideally describing its purpose. If there are auxiliary languages such as shell scripts, or Infrastructure-as-Code tools like Terraform or AWS Cloud Development Kit, these could be included in the sample name. For example, an example that used AWS CDK to build an Instagram Subscritpions Webhook using AWS API Gateway and Lambda could be named `instagram-subscriptions/aws/javascript/cdk-api-gateway-lambda`.
1. If you've changed APIs, update the documentation.
1. If you haven't already, complete the Contributor License Agreement ("CLA").

Expand Down
17 changes: 0 additions & 17 deletions LICENSE

This file was deleted.

24 changes: 24 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(c) Meta Platforms, Inc. and affiliates.

The source code in this repository is available as Open Source under the MIT license.

# License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
# Graph API Webhooks Samples
# Webhook Sample Applications for Graph API Developers

These are sample clients for Facebook's [Webhooks](https://developers.facebook.com/docs/graph-api/webhooks/) product and Instagram's [Subscriptions API](https://www.instagram.com/developer/subscriptions/).
**Summary**: When you build an App on Facebook Platforms, the platform may notify your App of an event using Webhooks, a type of HTTPS API. This repository contains sample code of applications that implement Webhook servers.

## Introduction to Graph API and Webhooks

When you [develop an Application](https://developers.facebook.com/docs/development/) (or App) using platforms built on Facebook's [Graph API](https://developers.facebook.com/docs/graph-api/), such as [Facebook Messenger Platform](https://developers.facebook.com/docs/messenger-platform/) or [WhatsApp Business Platform's Cloud API](https://developers.facebook.com/docs/whatsapp/cloud-api/), your applications often need to receive events from those platforms.

While the Graph API is an HTTPS interface by which your App can make API calls to Facebook, [Webhooks](https://developers.facebook.com/docs/graph-api/webhooks/) are a technology that allows platforms to send events as HTTPS requests back to your App.

For example, in Facebook Messenger Platform or WhatsApp Cloud API, Webhooks are essential for receiving replies to messages that you've sent, or learning that messages have been read, couldn't be delivered, etc. While not all platforms use Webhooks for event notification, many do.

# Sample Applications

These are sample applications that you can use to get started developing Apps that use Webhooks with various Facebook Platforms.

Currently, they include Facebook's [Webhooks](https://developers.facebook.com/docs/graph-api/webhooks/) product and Instagram's [Subscriptions API](https://www.instagram.com/developer/subscriptions/).

1. [Heroku](heroku) - A sample client that receives Webhook events.
1. [Hubot](hubot) - A script that messages a chat room when a Facebook Page post is published using Webhooks.

## Other Sample Applications with Webhooks

The following samples repositories also incorporate Webhook usage as part of the sample:

1. [fbsamples/messenger-bot-samples](https://github.com/fbsamples/messenger-bot-samples): a sample bot for Messenger Platform. The Webhook code is part of [`webhooks.js`](https://github.com/fbsamples/messenger-bot-samples/blob/master/account-linking/routes/webhooks.js)

## License

The sample code is available under the MIT license, as documented in [`LICENSE.md`](LICENSE.md).

## Terms of Service

As with any software that integrates with the Facebook platform, your use of this software is subject to the [Facebook Developer Principles and Policies](http://developers.facebook.com/policy/).

## Copyright

(c) Meta Platforms, Inc. and affiliates.
File renamed without changes.
20 changes: 0 additions & 20 deletions heroku/README.md

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ app.get('/', function(req, res) {
res.send('<pre>' + JSON.stringify(received_updates, null, 2) + '</pre>');
});

<<<<<<< HEAD
app.get(['/facebook', '/instagram'], function(req, res) {
if (
req.query['hub.mode'] == 'subscribe' &&
req.query['hub.verify_token'] == token
) {
res.send(req.query['hub.challenge']);
=======
app.get(['/facebook', '/instagram'], function(req, res) {
if (
req.param('hub.mode') == 'subscribe' &&
req.param('hub.verify_token') == token
) {
res.send(req.param('hub.challenge'));
>>>>>>> f71ddd1 (Move NodeJS source under ./nodejs)
} else {
res.sendStatus(400);
}
Expand Down
File renamed without changes.
94 changes: 94 additions & 0 deletions whatsapp-platform/nodejs/graph-api-webhook-server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Starter Project for WhatsApp Echo Bot Tutorial
*
* Remix this as the starting point for following the WhatsApp Echo Bot tutorial
*
*/

"use strict";

// Access token for your app
// (copy token from DevX getting started page
// and save it as environment variable into the .env file)
const token = process.env.WHATSAPP_TOKEN;

// Imports dependencies and set up http server
const request = require("request"),
express = require("express"),
body_parser = require("body-parser"),
axios = require("axios").default,
app = express().use(body_parser.json()); // creates express http server

// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log("webhook is listening"));

// Accepts POST requests at /webhook endpoint
app.post("/webhook", (req, res) => {
// Parse the request body from the POST
let body = req.body;

// Check the Incoming webhook message
console.log(JSON.stringify(req.body, null, 2));

// info on WhatsApp text message payload: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages
if (req.body.object) {
if (
req.body.entry &&
req.body.entry[0].changes &&
req.body.entry[0].changes[0] &&
req.body.entry[0].changes[0].value.messages &&
req.body.entry[0].changes[0].value.messages[0]
) {
let phone_number_id =
req.body.entry[0].changes[0].value.metadata.phone_number_id;
let from = req.body.entry[0].changes[0].value.messages[0].from; // extract the phone number from the webhook payload
let msg_body = req.body.entry[0].changes[0].value.messages[0].text.body; // extract the message text from the webhook payload
axios({
method: "POST", // Required, HTTP method, a string, e.g. POST, GET
url:
"https://graph.facebook.com/v12.0/" +
phone_number_id +
"/messages?access_token=" +
token,
data: {
messaging_product: "whatsapp",
to: from,
text: { body: "Ack: " + msg_body },
},
headers: { "Content-Type": "application/json" },
});
}
res.sendStatus(200);
} else {
// Return a '404 Not Found' if event is not from a WhatsApp API
res.sendStatus(404);
}
});

// Accepts GET requests at the /webhook endpoint. You need this URL to setup webhook initially.
// info on verification request payload: https://developers.facebook.com/docs/graph-api/webhooks/getting-started#verification-requests
app.get("/webhook", (req, res) => {
/**
* UPDATE YOUR VERIFY TOKEN
*This will be the Verify Token value when you set up webhook
**/
const verify_token = process.env.VERIFY_TOKEN;

// Parse params from the webhook verification request
let mode = req.query["hub.mode"];
let token = req.query["hub.verify_token"];
let challenge = req.query["hub.challenge"];

// Check if a token and mode were sent
if (mode && token) {
// Check the mode and token sent are correct
if (mode === "subscribe" && token === verify_token) {
// Respond with 200 OK and challenge token from the request
console.log("WEBHOOK_VERIFIED");
res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}
}
});
Loading