# message **Repository Path**: feixiacode/message ## Basic Information - **Project Name**: message - **Description**: 一个统一的消息通知系统,支持ThinkPHP框架,可以方便地发送短信、钉钉、微信等多种消息。 - **Primary Language**: PHP - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2026-04-13 - **Last Updated**: 2026-04-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Message A unified message notification system that supports ThinkPHP framework, enabling convenient sending of SMS, DingTalk, WeChat, and other types of messages. ## Requirements - PHP >= 7.2 - ThinkPHP >= 6.0 ## Installation Install via Composer: ```bash composer require xibeicity/message ``` ## Configuration 1. Copy the configuration file to your project's config directory: ```bash cp vendor/xibeicity/message/config/message.php config/message.php ``` 2. Configure the related parameters in your .env file: ```env # Default message driver MESSAGE_DRIVER=sms # Aliyun SMS configuration ALIYUN_ACCESS_KEY_ID=your-access-key-id ALIYUN_ACCESS_KEY_SECRET=your-access-key-secret ALIYUN_SMS_SIGN_NAME=your-sign-name ALIYUN_SMS_TEMPLATE_CODE=your-template-code ALIYUN_REGION_ID=cn-hangzhou # DingTalk configuration DINGTALK_APP_KEY=your-app-key DINGTALK_APP_SECRET=your-app-secret DINGTALK_AGENT_ID=your-agent-id # DingTalk Robot configuration DINGTALK_ROBOT_WEBHOOK=your-webhook-url DINGTALK_ROBOT_SECRET=your-secret # WeChat Official Account configuration WECHAT_OFFICIAL_ACCOUNT_APP_ID=your-app-id WECHAT_OFFICIAL_ACCOUNT_APP_SECRET=your-app-secret WECHAT_OFFICIAL_ACCOUNT_TOKEN=your-token WECHAT_OFFICIAL_ACCOUNT_AES_KEY=your-aes-key # WeChat Mini Program configuration WECHAT_MINI_PROGRAM_APP_ID=your-app-id WECHAT_MINI_PROGRAM_APP_SECRET=your-app-secret # WeChat Work Robot configuration WECHAT_ROBOT_WEBHOOK=your-webhook-url ``` 3. Run database migrations (for message logging feature): ```bash php think migrate:run ``` ## Basic Usage ### Aliyun SMS ```php use Xibeicity\Message\MessageManager; $message = new MessageManager(); // Send regular SMS $message->driver('sms')->send(['13800138000'], 'Your verification code is: 123456'); // Send SMS using template $message->driver('sms')->sendTemplate(['13800138000'], 'SMS_123456789', [ 'code' => '123456' ], [ 'sign_name' => 'Your Sign Name' ]); // Batch send SMS $message->driver('sms')->batchSend([ ['phone' => '13800138000', 'content' => 'Code: 123456'], ['phone' => '13800138001', 'content' => 'Code: 654321'] ]); // Get sending status $result = $message->driver('sms')->getStatus(); if (!$result['success']) { echo $message->driver('sms')->getError(); } ``` ### DingTalk Work Notification ```php use Xibeicity\Message\MessageManager; $message = new MessageManager(); // Send regular message $message->driver('dingtalk')->send(['user123'], 'This is a test message'); // Send message with link $message->driver('dingtalk')->send(['user123'], 'Please check details', [ 'link' => 'https://example.com', 'title' => 'Notification Title' ]); // Batch send messages $message->driver('dingtalk')->batchSend([ ['user_id' => 'user123', 'content' => 'Message 1'], ['user_id' => 'user456', 'content' => 'Message 2'] ]); ``` ### DingTalk Group Robot ```php use Xibeicity\Message\MessageManager; $message = new MessageManager(); // Send text message $message->driver('dingtalk_robot')->send(['@all'], 'This is a test message', [ 'at' => ['13800138000'], 'at_all' => false ]); // Send markdown message $message->driver('dingtalk_robot')->send(['@all'], '# Title\nContent', [ 'msg_type' => 'markdown' ]); // Send message with link $message->driver('dingtalk_robot')->send(['@all'], 'Please check details', [ 'msg_type' => 'link', 'title' => 'Notification Title', 'link' => 'https://example.com' ]); ``` ### WeChat Official Account ```php use Xibeicity\Message\MessageManager; $message = new MessageManager(); // Send regular template message $message->driver('wechat_official_account')->send(['OPENID'], 'You have a new notification'); // Send complete template message $message->driver('wechat_official_account')->sendTemplate(['OPENID'], 'template-id', [ 'first' => 'You have a new notification', 'keyword1' => 'Notification content', 'keyword2' => '2024-01-01 12:00:00', 'remark' => 'Please check promptly' ], [ 'url' => 'https://example.com' ]); // Batch send template messages $message->driver('wechat_official_account')->batchSend([ ['openid' => 'OPENID1', 'content' => 'Notification 1'], ['openid' => 'OPENID2', 'content' => 'Notification 2'] ]); ``` ### WeChat Mini Program ```php use Xibeicity\Message\MessageManager; $message = new MessageManager(); // Send subscription message $message->driver('wechat_mini_program')->send(['OPENID'], 'Product shipped', [ 'template_id' => 'template-id', 'page' => 'pages/index/index', 'data' => [ 'thing1' => ['value' => 'Product Name'], 'amount2' => ['value' => '89.90'], 'date3' => ['value' => '2024-01-01 12:00:00'] ] ]); // Send subscription message using preset template $message->driver('wechat_mini_program')->sendTemplate(['OPENID'], 'template-id', [ 'thing1' => 'Product Name', 'amount2' => '89.90', 'date3' => '2024-01-01 12:00:00' ], [ 'page' => 'pages/index/index' ]); ``` ### WeChat Work Robot ```php use Xibeicity\Message\MessageManager; $message = new MessageManager(); // Send text message $message->driver('wechat_robot')->send(['@all'], 'This is a test message', [ 'mentioned_list' => ['wangwu', 'lisi'], 'mentioned_mobile_list' => ['13800138000'] ]); // Send markdown message $message->driver('wechat_robot')->send(['@all'], '# Title\nContent', [ 'msg_type' => 'markdown' ]); // Send image message $message->driver('wechat_robot')->send(['@all'], '', [ 'msg_type' => 'image', 'base64' => 'base64 encoded image content', 'md5' => 'md5 value of the image content' ]); ``` ## Error Handling All drivers support error handling: ```php use Xibeicity\Message\MessageManager; $message = new MessageManager(); $result = $message->driver('sms')->send(['13800138000'], 'Code: 123456'); if (!$result['success']) { // Get error message $error = $message->driver('sms')->getError(); // Handle error echo "Send failed: {$error}"; } ``` ## Advanced Features ### Custom Message Driver 1. Create a custom driver class implementing MessageInterface: ```php use Xibeicity\Message\Contracts\MessageInterface; class CustomDriver implements MessageInterface { public function send(array $to, string $content, array $options = []): array { // Implement sending logic return [ 'success' => true, 'message_id' => 'xxx' ]; } public function sendTemplate(array $to, string $templateCode, array $templateParams = [], array $options = []): array { // Implement template message sending logic return [ 'success' => true, 'message_id' => 'xxx' ]; } public function batchSend(array $messages): array { // Implement batch sending logic return [ 'success' => true, 'message_id' => 'xxx' ]; } public function getStatus(): array { // Return sending status return [ 'success' => true, 'message_id' => 'xxx' ]; } public function getError(): ?string { // Return error message return null; } } ``` 2. Register the custom driver: ```php use Xibeicity\Message\MessageManager; $message = new MessageManager(); $message->extend('custom', function () { return new CustomDriver(); }); // Use custom driver to send message $message->driver('custom')->send(['user123'], 'This is a test message'); ```