Unofficial SDK of Facebook Messenger Platform API
messenger platform official doc
Add repository
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://www.jitpack.io</url>
</repository>
</repositories>
Add dependency
<dependency>
<groupId>com.github.gerenvip</groupId>
<artifactId>messenger-platform-sdk</artifactId>
<version>1.0.1</version>
</dependency>
Use in Code
Start with FMClient instance, get FMClient instance in FMClicent.getInstance().
FMClient fmClient = FMClient.getInstance();
Use with* methods set parameters to FMClient instance.
//new PostBack Handler to receive the postback message from facebook
public class PostbackHandler extends FMMessagePostBackHandler {
@Override
public void handle(FMReceiveMessage.Messaging message) {
log.debug("PostbackHandler handlePostBack, sender -> {}, postback -> {}", message.getSender(), message);
}
}
//get FMClient instance, and set token, secret, handler paramter.
FMClient fmClient = FMClient.getInstance();
PostbackHandler postbackHandler = new PostbackHandler();
fmClient.withAccessToken("your_token")
.withAccessSecret("your_secret")
.withFmMessagePostBackHandler(postbackHandler);
Use signature method to valid the payload.
String xHubSignature = request.getHeader("X-Hub-Signature");
StringBuilder buffer = new StringBuilder();
BufferedReader reader = request.getReader();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String payload = buffer.toString();
boolean signature = fmClient.signature(payload, xHubSignature);
if(signature){
//do
} else {
//forbid
}
Use dispatch method to dispatch payload message.
fmClient.dispatch(payload);
https://www.facebook.com/pages/create
https://developers.facebook.com/
https://developers.facebook.com/apps
- In the sidebar of your app settings under 'PRODUCTS', click '+ Add Product'.
- Hover over 'Messenger' to display options.
- Click the 'Set Up' button.
- In the 'Webhooks' section of the Messenger settings console, click the 'Setup Webhooks' button.
- In the 'Callback URL' field, enter the public URL for your webhook.
- In the 'Verify Token' field, enter the verify token for your webhook. Under 'Subscription Fields', select the webhook events you want delivered to you webhook. At a minimum, we recommend you choose messages and messaging_postbacks to get started.
- Click the 'Verify and Save' button.
- In the 'Token Generation' section of the Messenger settings console, click the 'Select a Page' dropdown and select the Facebook Page you want to subscribe this app to. This is the Page that you want your webhook to receive events for when people on Messenger chat with it.
- Copy the token that appears in the 'Page Access Token' field. You will use this token later to make API requests.
- In the 'Webhook' section of the Messenger settings console, click the 'Select a Page' dropdown and select the same Facebook Page you generated a Page access token for. This will subscribe your app to receive webhook events for the Page.
- Click the 'Subscribe' button next to the dropdown.
To test that your app set up was successful, send a message to your Page from facebook.com or in Messenger. If your webhook receives a webhook event, you have fully set up your app!
Copyright 2018 gerenvip
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.