torgodly/messenger-bot
最新稳定版本:v2.0.0
Composer 安装命令:
composer require torgodly/messenger-bot
包简介
Facebook Messenger webhooks and Page comments for Laravel (BotMan-style API, no BotMan dependency).
README 文档
README
Facebook Messenger webhooks for Laravel (messages, postbacks, quick replies, feed comments). API similar to BotMan (hears, payload, onComment) without BotMan.
PHP 8.3+ · Laravel 12 or 13
Install
composer require torgodly/messenger-bot php artisan vendor:publish --tag=messenger-bot-config php artisan messenger-bot:install
One Facebook Page (default): the command above writes the usual .env keys, validates the token when possible, and subscribes webhooks. You do not configure tenant resolution.
Several Pages (multi-tenant): use the same install command with flags so .env is filled for you—no custom resolver class is required:
php artisan messenger-bot:install --tenant --model=App\Models\MessengerConnection
(--model= is optional in interactive mode; you can add MESSENGER_BOT_TENANCY_CONNECTION_MODEL later.) The package resolves incoming webhooks by loading that Eloquent model where facebook_page_id matches the Page ID (override column with MESSENGER_BOT_TENANCY_PAGE_ID_COLUMN). Set MESSENGER_BOT_TENANCY_RESOLVER only if you need a fully custom lookup.
Configure
| Env / setting | Purpose |
|---|---|
APP_URL |
Public base URL (no trailing slash). |
MESSENGER_BOT_APP_ID, MESSENGER_BOT_APP_SECRET |
Meta app. |
MESSENGER_BOT_VERIFY_TOKEN |
Same string in Meta → Webhooks → Verify Token. |
MESSENGER_BOT_PAGE_ACCESS_TOKEN |
Optional; leave empty and use OAuth for production. |
MESSENGER_BOT_TENANCY_ENABLED, MESSENGER_BOT_TENANCY_CONNECTION_MODEL |
Multi-tenant mode and the Eloquent model used for Page → tenant mapping (see install --tenant). |
Meta — Facebook Login: add redirect URI (exact match):
https://YOUR-DOMAIN/messenger-bot/oauth/facebook/callback
(or MESSENGER_BOT_OAUTH_REDIRECT_URI if set)
Meta — Webhooks (Page): callback https://YOUR-DOMAIN/webhook/messenger (or MESSENGER_BOT_WEBHOOK_PATH), same verify token.
Webhook and OAuth routes are registered outside the web middleware group (no CSRF 419 on Meta POSTs). Set MESSENGER_BOT_AUTO_REGISTER_ROUTES=false only if you register the webhook route yourself.
Usage — handlers
Register once at boot (e.g. App\Providers\AppServiceProvider::boot() or routes/web.php):
use MessengerBot\Facades\MessengerBot; MessengerBot::hears('hi', fn ($bot, $message) => $bot->reply('Hello!')); // Case-insensitive exact match (default). Regex: pattern wrapped in /.../ MessengerBot::hears('/^order\\s+#\\d+$/i', fn ($bot) => $bot->reply('Got your order.')); MessengerBot::payload('GET_STARTED', fn ($bot, $postback) => $bot->reply('Welcome!')); MessengerBot::payload('MENU_PRODUCTS', fn ($bot) => $bot->reply('Here are products…')); // Quick reply payloads use the same payload() registration MessengerBot::onComment(function ($bot, $comment) { $bot->replyToComment($comment->id, 'Thanks!'); }); // Only comments on specific Graph post IDs MessengerBot::onComment(fn ($bot, $c) => $bot->replyToComment($c->id, 'Hi!'), onlyForPostIds: '123456789'); MessengerBot::fallback(fn ($bot, $message) => $bot->reply('Say hi or use the menu.'));
Priority (higher runs first): MessengerBot::hears('pattern', $handler, priority: 10);
Get Started: register payload() for MESSENGER_BOT_GET_STARTED_PAYLOAD or set MESSENGER_BOT_GET_STARTED_REPLY for a default when no handler matches.
Usage — OAuth (single Page)
Open in browser (as Page admin):
https://YOUR-DOMAIN/messenger-bot/oauth/facebook
Token is cached; profile sync runs on install. Clear with php artisan messenger-bot:clear-page-token.
Usage — multi-tenant
-
Database row per Page — an Eloquent model implements
MessengerBot\Contracts\MessengerConnectableand usesMessengerBot\Laravel\Concerns\InteractsWithMessengerConnection. By default it readstenant_id, primary key (oridcolumn) for the connection id, andfacebook_page_idfor the Graph Page id. OverridemessengerTenantIdColumn(),messengerConnectionKeyColumn(), ormessengerFacebookPageIdColumn()if your columns differ. -
Turn it on —
php artisan messenger-bot:install --tenant --model=Your\\Modelor setMESSENGER_BOT_TENANCY_ENABLED=trueandMESSENGER_BOT_TENANCY_CONNECTION_MODEL=Your\\Model. Webhooks then load that model withwhere(facebook_page_id, <Page id from Meta>). If no row matches, legacy behaviour depends onMESSENGER_BOT_TENANCY_FALLBACK_LEGACY/MESSENGER_BOT_TENANCY_SKIP_UNRESOLVED(see config comments). -
OAuth — after the user is logged into your app, send them to Facebook with the connectable model so the callback can store the token for that row:
use MessengerBot\Facades\MessengerOAuth; return MessengerOAuth::redirectToFacebook($store);
- Posts and background jobs — same object everywhere:
use MessengerBot\Kernel\Contracts\SyncsFacebookPagePosts; use MessengerBot\Kernel\Posts\PostsSyncOptions; use MessengerBot\Kernel\Posts\PostsSyncRequest; use MessengerBot\Laravel\Jobs\RefreshPostsCacheJob; $posts = app(SyncsFacebookPagePosts::class); $result = $posts->sync(PostsSyncRequest::forConnectable($store), new PostsSyncOptions); RefreshPostsCacheJob::forConnectable($store, bypassCache: false);
- Inside webhook handlers — optional active resolution:
use MessengerBot\Laravel\MessengerCurrentConnection; $pageContext = app(MessengerCurrentConnection::class)->resolution(); // ?TenantResolution
MESSENGER_BOT_OAUTH_DUAL_WRITE_LEGACY (default true) also fills the legacy global token cache for tools that still expect a single Page. MESSENGER_BOT_OAUTH_REQUIRE_MT_SIGNATURE=false is for local debugging only. Swap ConnectionTokenRepository in the container if you store tokens in the database instead of cache.
Usage — multi-tenant (advanced)
MESSENGER_BOT_TENANCY_RESOLVER— bind a class implementingMessengerBot\Kernel\Contracts\TenantResolverwhen the default Eloquent lookup is not enough.MessengerBot\Laravel\Tenancy\EloquentMessengerTenantResolver— optional base class if you prefer a dedicated resolver class over config-onlyconnection_model(seestubs/eloquent_messenger_tenant_resolver.php.stub).- Raw OAuth query params —
tenant_id,connection_id,mt_sigwheremt_sigishash_hmac('sha256', $tenantId."\n".$connectionId, config('messenger-bot.app_secret')); the package signs these for you viaMessengerOAuth.
Usage — Page posts (Graph)
Inject MessengerBot\Kernel\Contracts\SyncsFacebookPagePosts.
use MessengerBot\Kernel\Contracts\SyncsFacebookPagePosts; use MessengerBot\Kernel\Posts\PostsSyncOptions; use MessengerBot\Kernel\Posts\PostsSyncRequest; use MessengerBot\Kernel\Tenancy\ConnectionId; use MessengerBot\Kernel\Tenancy\TenantId; $posts = app(SyncsFacebookPagePosts::class); // From a MessengerConnectable model (multi-tenant or single-tenant with opaque keys): $result = $posts->sync(PostsSyncRequest::forConnectable($store), new PostsSyncOptions(/* … */)); // Or explicit value objects: $result = $posts->sync( new PostsSyncRequest( tenantId: new TenantId('tenant-ulid'), connectionId: new ConnectionId('connection-ulid'), pageId: '1234567890', since: new \DateTimeImmutable('2026-05-01T00:00:00Z'), until: new \DateTimeImmutable('now'), maxPosts: 500, fields: ['id', 'message', 'created_time', 'permalink_url'], ), new PostsSyncOptions( limitPerRequest: 25, maxApiCalls: 50, maxDurationMs: 0, bypassCache: false, useStampedeLock: true, cacheTtlSeconds: 300, ), );
Single-tenant: tenancy off; forConnectable() still works with any object implementing MessengerConnectable (stable keys + Page id). Legacy token must match pageId when a legacy page_id is cached.
Invalidate post list cache:
app(SyncsFacebookPagePosts::class)->bumpPostsCacheVersion(new ConnectionId($store->messengerConnectionKey()));
Background refresh (raw keys or connectable):
use MessengerBot\Laravel\Jobs\RefreshPostsCacheJob; RefreshPostsCacheJob::forConnectable($store); RefreshPostsCacheJob::dispatch(tenantId: '…', connectionId: '…', pageId: '…');
Example DB migration stub: stubs/migrations/create_mb_connections_table.php.stub.
Artisan
| Command | Purpose |
|---|---|
messenger-bot:install |
Publish config, .env keys, token check, subscribe, menu. |
messenger-bot:install --tenant [--model=] |
Same as above plus multi-tenant .env defaults (MESSENGER_BOT_TENANCY_*). |
messenger-bot:sync-page |
Re-subscribe webhook fields, persistent menu. |
messenger-bot:token-status |
Cached token / expiry hint. |
messenger-bot:clear-page-token |
Clear legacy cached Page token. |
--skip-token-check on install/sync skips Graph GET /me.
Events
WebhookReceived, MessageReceived, PostbackReceived, CommentCreated, PostsSynced, PostsCacheHit, PostsCacheMiss, plus outgoing send events.
References
- Messenger templates (use methods on
Bot). - README-EXTERNAL-RULES.md — store tenant rules and menus in your app (examples).
- CHANGELOG.md
403 webhook: wrong app secret vs Meta, or leave MESSENGER_BOT_SIGNATURE_ENABLED=false only in local tests.
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-04