unicodeveloper / novu
Novu PHP SDK
1.2.0
2024-01-08 12:51 UTC
Requires
- php: ^7.2|^8.0
- ext-json: *
- guzzlehttp/guzzle: ^6.5.7|^7.4.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.2.1
- pestphp/pest: ^1.20.0
- phpstan/phpstan: ^1.1.1
- symfony/var-dumper: ^5.3.10
This package is auto-updated.
Last update: 2024-08-29 21:10:36 UTC
README
PHP SDK
PHP Novu SDK 和包提供了一个流畅且表达性强的接口,用于与 Novu API 交互和管理通知。
安装
需要 PHP 7.2+ 和 Composer。
要获取 Novu PHP SDK 的最新版本,只需要求它
composer require unicodeveloper/novu
内容
用法
要与 Novu SDK 交互,您可以使用只包含 API 密钥或包含 API 密钥和自定义基本 URI 的配置数组进行实例化。
仅使用 API 密钥
use Novu\SDK\Novu; $novu = new Novu('YOUR_API_KEY_HERE'); // Sign up on https://web.novu.co and grab your API key from https://web.novu.co/settings
使用配置数组
如果您需要指定自定义基本 URI(例如,如果您正在指向预发布环境或本地开发设置),可以传递包含 apiKey
和 baseUri
的数组
use Novu\SDK\Novu; $config = [ 'apiKey' => 'YOUR_API_KEY_HERE', 'baseUri' => 'https://custom-api-url.com/v1/' ]; $novu = new Novu($config); // Get started with self-hosted Novu here https://docs.novu.co/overview/docker-deploy
创建 Novu
实例后,您可以使用它执行 Novu API 提供的所有操作。
事件
触发 事件 - 向订阅者发送通知
$response = $novu->triggerEvent([ 'name' => '<REPLACE_WITH_TEMPLATE_NAME_FROM_ADMIN_PANEL>', 'payload' => ['customVariables' => 'Hello'], 'to' => [ 'subscriberId' => '<SUBSCRIBER_IDENTIFIER_FROM_ADMIN_PANEL>', 'phone' => '07983882186' ] ])->toArray();
批量触发 事件
$response = $novu->bulkTriggerEvent([ [ 'name' => '<REPLACE_WITH_TEMPLATE_NAME_FROM_ADMIN_PANEL>', 'to' => '<SUBSCRIBER_IDENTIFIER_FROM_ADMIN_PANEL>', 'payload' => ['customVariables' => 'Hello'] ], [ 'name' => '<REPLACE_WITH_TEMPLATE_NAME_FROM_ADMIN_PANEL>', 'to' => '<SUBSCRIBER_IDENTIFIER_FROM_ADMIN_PANEL>', 'payload' => ['customVariables' => 'World'] ], [ 'name' => '<REPLACE_WITH_TEMPLATE_NAME_FROM_ADMIN_PANEL>', 'to' => '<SUBSCRIBER_IDENTIFIER_FROM_ADMIN_PANEL>', 'payload' => ['customVariables' => 'Again'] ] ])->toArray();
触发 事件 - 向主题发送通知
$response = $novu->triggerEvent([ 'name' => '<event_name>', 'payload' => ['customVariables' => 'Hello'], 'to' => [ [ 'type' => 'Topic', 'topicKey' => $topicKey ], [ 'type' => 'Topic', 'topicKey' => $topicSecondKey ] ] ])->toArray();
广播 事件到所有现有订阅者
$response = $novu->broadcastEvent([ 'name' => '<REPLACE_WITH_EVENT_NAME_FROM_ADMIN_PANEL>', 'payload' => ['customVariables' => 'Hello'], 'transactionId' => '<REPLACE_WITH_TRANSACTION_ID>' ])->toArray();
取消 触发事件。在事件触发期间使用先前生成的 transactionId,此操作将取消任何活动或挂起的流程
$response = $novu->cancelEvent($transactionId);
订阅者
// Get list of subscribers $subscribers = $novu->getSubscriberList(); // Create subscriber & get the details of the recently created subscriber returned. $subscriber = $novu->createSubscriber([ 'subscriberId' => 'YOUR_SYSTEM_USER_ID>', 'email' => '<insert-email>', // optional 'firstName' => '<insert-firstname>', // optional 'lastName' => '<insert-lastname>', // optional 'phone' => '<insert-phone>', //optional 'avatar' => '<insert-avatar>', // optional ])->toArray(); // Bulk create subscribers $response = $novu->bulkCreateSubscribers([ [ 'subscriberId' => 'SUBSCRIBER_IDENTIFIER>', 'email' => '<insert-email>', // optional 'firstName' => '<insert-firstname>', // optional 'lastName' => '<insert-lastname>', // optional 'avatar' => '<insert-avatar>', // optional ], [ 'subscriberId' => 'SUBSCRIBER_IDENTIFIER>', 'email' => '<insert-email>', // optional 'firstName' => '<insert-firstname>', // optional 'lastName' => '<insert-lastname>', // optional 'avatar' => '<insert-avatar>', // optional ], ]); // Get subscriber $subscriber = $novu->getSubscriber($subscriberId)->toArray(); // Update subscriber $subscriber = $novu->updateSubscriber($subscriberId, [ 'email' => '<insert-email>', // optional 'firstName' => '<insert-firstname>', // optional 'lastName' => '<insert-lastname>', // optional 'phone' => '<insert-phone>', //optional 'avatar' => '<insert-avatar>', // optional ])->toArray(); // Delete subscriber $novu->deleteSubscriber($subscriberId); // Update subscriber credentials $response = $novu->updateSubscriberCredentials($subscriberId, [ 'providerId' => '<insert-providerId>', 'credentials' => '<insert-credentials>' ])->toArray(); // Update subscriber online status $isOnlineStatus = true; // or false $response = $novu->updateSubscriberOnlineStatus($subscriberId, $isOnlineStatus)->toArray(); // Get subscriber preferences $preferences = $novu->getSubscriberPreferences($subscriberId)->toArray(); // Update subscriber preference $novu->updateSubscriberPreference($subscriberId, $templateId, [ 'channel' => 'insert-channel', 'enabled' => 'insert-boolean-value' // optional ]); // Get a notification feed for a particular subscriber $feed = $novu->getNotificationFeedForSubscriber($subscriberId); // Get the unseen notification count for subscribers feed $count = $novu->getUnseenNotificationCountForSubscriber($subscriberId); // Mark a subscriber feed message as seen $novu->markSubscriberFeedMessageAsSeen($subscriberId, $messageId, []); // Mark message action as seen $novu->markSubscriberMessageActionAsSeen($subscriberId, $messageId, $type, []);
主题
// Create a Topic $novu->createTopic([ 'key' => 'frontend-users', 'name' => 'All frontend users' ]); // Fetch all topics $novu->getTopics(); // Get a topic $novu->topic($topicKey); // Add subscribers to a topic $subscribers = [ '63e271488c028c44fd3a64e7', '3445' ]; $novu->topic($topicKey)->addSubscribers($subscribers); // Remove subscribers from a topic $subscribers = [ '63e271488c028c44fd3a64e7', '3445' ]; $novu->topic($topicKey)->removeSubscribers($subscribers); // Rename a topic $novu->topic($topicKey)->rename($topicName);
活动
// Get activity feed $feed = $novu->getActivityFeed(); // Get activity statistics $stats = $novu->getActivityStatistics()->toArray(); // Get activity graph statistics $graphStats = $novu->getActivityGraphStatistics()->toArray();
集成
// Get integrations $novu->getIntegrations()->toArray(); // Create integration $novu->createIntegration([ 'providerId' => '<insert->provider->id>', 'channel' => '<insert->channel>', 'credentials' => [ // insert all the fields ], 'active' => true, 'check' => true ])->toArray(); // Get active integrations $novu->getActiveIntegrations()->toArray(); // Get webhook support status for provider $novu->getWebhookSupportStatusForProvider($providerId)->toArray(); // Update integration $novu->updateIntegration($integrationId, [ 'active' => true, 'credentials' => [ // insert all the fields ], 'check' => true ])->toArray(); // Delete integration $novu->deleteIntegration($integrationId);
布局
// filter layouts $novu->filterLayouts(['pageSize' => 1])->toArray(); // Create layout $novu->createLayout([ 'name' => '<insert-name-of-layout>', 'identifier' => '<insert-identifier>', 'content' => '<insert-html-content>', ])->toArray(); // Get a layout $novu->getLayout('<insert-layout-id>')->toArray(); // Set Layout as default $novu->setLayoutAsDefault('<insert-layout-id>'); // Update layout $novu->updateLayout('<insert-layout-id>', [ 'name' => '<insert-name-of-layout>', 'identifier' => '<insert-identifier>', 'content' => '<insert-html-content>', ])->toArray(); // Delete layout $novu->deleteLayout('<insert-layout-id>');
通知
// Get all notifications $novu->getNotifications()->toArray(); // Get all notifications with query parameters $queryParams = [ 'page' => 3 ]; $novu->getNotifications($queryParams)->toArray(); // Get one notification $novu->getNotification($notificationId)->toArray(); // Get notification stats $novu->getNotificationStats()->toArray(); // Get Notification graph stats $novu->getNotificationGraphStats()->toArray(); // Get Notification graph stats with query parameters $queryParams = [ 'days' => 5 ]; $novu->getNotificationGraphStats($queryParams)->toArray();
通知模板
// Get notification templates $novu->getNotificationTemplates()->toArray(); // Create notification template $novu->createNotificationTemplate([ "name" => "name", "notificationGroupId" => "notificationGroupId", "tags" => ["tags"], "description" => "description", "steps" => ["steps"], "active" => true, "draft" => true, "critical" => true, "preferenceSettings" => preferenceSettings ])->toArray(); // Update notification template $novu->updateNotificationTemplate($templateId, [ "name" => "name", "tags" => ["tags"], "description" => "description", "identifier" => "identifier", "steps" => ["steps"], "notificationGroupId" => "notificationGroupId", "active" => true, "critical" => true, "preferenceSettings" => preferenceSettings ])->toArray(); // Delete notification template $novu->deleteNotificationTemplate($templateId); // Get notification template $novu->getANotificationTemplate($templateId); // Update notification template status $novu->updateNotificationTemplateStatus($templateId, [ 'active' => true ])
通知组
// Create Notification group $novu->createNotificationGroup([ 'name' => '<insert-name>' ]); // Get Notification groups $novu->getNotificationGroups()->toArray();
更改
// Get changes $novu->getChanges(); // Get changes count $novu->getChangesCount()->toArray(); // Apply changes $novu->applyBulkChanges([ 'changeIds' = [ '<insert-all-the-change-ids>' ] ])->toArray(); // Apply change $novu->applyChange($changeId, []);
环境
// Get current environment $novu->getCurrentEnvironment()->toArray(); // Create environment $novu->createEnvironment([ 'name' => '<insert-name>', 'parentId' => '<insert-parent-id>' // optional ])->toArray(); // Get environments $novu->getEnvironments()->toArray(); // Update environment by id $novu->updateEnvironment($envId, [ "name" => "name", "identifier" => "identifier", "parentId" => "parentId" ]); // Get API KEYS $novu->getEnvironmentsAPIKeys()->toArray(); // Regenerate API KEYS $key = $novu->regenerateEnvironmentsAPIKeys()->toArray(); // Update Widget Settings $novu->updateWidgetSettings([ 'notificationCenterEncryption' => true ]);
源
// Create feed $novu->createFeed([ 'name' => '<insert-name-for-feed>' ]); // Get feeds $novu->getFeeds()->toArray(); // Delete feed $novu->deleteFeed();
消息
// Get messages $novu->getMessages([ 'page' => 1, 'channel' => ['<insert-channel>'], ]); // Delete message $novu->deleteMessage();
执行详情
// Get execution details $novu->getExecutionDetails([ 'notificationId' => '<insert-notification-id>', 'subscriberId' => '<insert-subscriber-id>' ])->toArray();
租户
// Create tenant $novu->createTenant([ 'identifier' => '<identifier>', 'name' => '<name>', ]); // Get tenants $novu->getTenants()->toArray();
验证入站解析功能的 MX 记录设置
// Validate MX Record for Inbound Parse $novu->validateMXRecordForInboundParse()->toArray();
有关这些方法和其参数的更多信息,请参阅 API 文档。
贡献
欢迎功能请求、错误报告和拉取请求。请创建 问题。
支持和反馈
请务必访问 Novu 官方 文档网站 获取有关 SDK 的更多信息。如需进一步帮助,请加入我们的 Discord 服务器 这里。
许可证
Novu PHP SDK 由 Prosper Otemuyiwa 在 MIT 许可证 下创建。