This package provides a set of classes to handle webhook events from the Facebook (Meta) Messenger Platform. It allows you to parse, identify, and work with different types of events such as messages, postbacks, deliveries, reactions, and more.
- Parse and verify Messenger webhook events
- Identify various event types like messages, postbacks, reactions, etc.
- Handle multiple message types such as text, quick replies, attachments, and more
- Support for Messenger-specific features like account linking, handover protocol, and policy enforcement
Add the following to your pubspec.yaml
:
dependencies:
messenger_webhook_package: latest_version
Install it via flutter pub get
or dart pub get
.
You can use the WebhookEvent.verifyToken
method to handle the webhook token verification step from Messenger:
String? verifyResult = WebhookEvent.verifyToken(
token: 'received_token',
verifyToken: 'your_verify_token',
challenge: 'challenge_string',
);
if (verifyResult != null) {
print('Token verified successfully: $verifyResult');
} else {
print('Token verification failed');
}
You can create a WebhookEvent
from the JSON payload sent by Messenger:
final Map<String, dynamic> jsonPayload = {};
final WebhookEvent event = WebhookEvent.fromJson(jsonPayload);
After parsing the event, you can use the identifyEvent
method to determine what kind of event occurred:
Event identifiedEvent = WebhookEvent.identifyEvent(event);
if (identifiedEvent is MessageEvent) {
print('Received a message: ${identifiedEvent.message.text}');
} else if (identifiedEvent is PostbackEvent) {
print('Received a postback: ${identifiedEvent.postback.payload}');
} else {
print('Unknown event type');
}
You can also identify the type of message received, such as text, quick replies, attachments, etc.:
MessageType messageType = WebhookEvent.identifyMessageType(event.entry[0].messaging[0].message);
if (messageType is TextMessage) {
print('Received text: ${messageType.text}');
} else if (messageType is QuickReplyMessage) {
print('Received quick reply with payload: ${messageType.quickReply.payload}');
}
The package supports the following event types:
MessageEvent
PostbackEvent
DeliveryEvent
ReactionEvent
EchoEvent
PolicyEnforcementEvent
PassThreadControlEvent
TakeThreadControlEvent
RequestThreadControlEvent
ReferralEvent
OptinEvent
MessageEditEvent
- And more...
For unknown or unsupported event types, the UnknownEvent
class is used.
Contributions are welcome! Feel free to submit a PR or open an issue for any bugs, features, or improvements.
This package is licensed under the MIT License.
Paurini Taketakehikuroa Wiringi