tourze / wechat-official-account-mass-bundle
微信公众号群发消息功能包,提供文本、语音等消息类型的群发能力
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/wechat-official-account-mass-bundle
Requires
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/doctrine-fixtures-bundle: ^4.0
- doctrine/orm: ^3.0
- doctrine/persistence: ^4.1
- easycorp/easyadmin-bundle: ^4
- knplabs/knp-menu: ^3.7
- monolog/monolog: ^3.1
- nesbot/carbon: ^2.72 || ^3
- psr/log: ^3|^2|^1
- symfony/config: ^7.3
- symfony/console: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/doctrine-bridge: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-kernel: ^7.3
- symfony/property-access: ^7.3
- symfony/security-http: ^7.3
- symfony/yaml: ^7.3
- tourze/bundle-dependency: 1.*
- tourze/doctrine-indexed-bundle: 1.0.*
- tourze/doctrine-snowflake-bundle: 1.1.*
- tourze/doctrine-timestamp-bundle: 1.1.*
- tourze/doctrine-track-bundle: 1.1.*
- tourze/easy-admin-menu-bundle: 1.0.*
- tourze/enum-extra: 1.0.*
- tourze/symfony-cron-job-bundle: 1.1.*
- tourze/symfony-dependency-service-loader: 1.0.*
- tourze/wechat-official-account-bundle: 0.1.*
Requires (Dev)
This package is auto-updated.
Last update: 2025-11-18 14:48:27 UTC
README
A Symfony bundle for WeChat Official Account mass messaging functionality, supporting scheduled broadcasts to all users, specific user groups by tag, or individual users by OpenID.
Table of Contents
- Features
- Requirements
- Installation
- Quick Start
- Configuration
- API Reference
- Advanced Usage
- Security
- Troubleshooting
- Contributing
- License
- References
Features
- Flexible Targeting: Send messages to all users, specific user groups by tag, or individual users by OpenID
- Multiple Message Types: Support for text and voice messages
- Scheduled Broadcasting: Schedule messages to be sent at specific times
- Automatic Sending: Built-in cron task for automatic message dispatch
- Database-backed Task Management: Persistent storage of tasks with status tracking
- WeChat API Integration: Full integration with WeChat Official Account API
- Response Tracking: Stores WeChat API response data (msg_id, msg_data_id)
- Multi-account Support: Handle multiple WeChat Official Accounts
Installation
composer require tourze/wechat-official-account-mass-bundle
Quick Start
1. Enable the Bundle
Register the bundle in your config/bundles.php:
<?php return [ // ... WechatOfficialAccountMassBundle\WechatOfficialAccountMassBundle::class => ['all' => true], ];
2. Configure Database
Update your database schema to create the required tables:
php bin/console doctrine:schema:update --force
3. Create a Mass Task
<?php use WechatOfficialAccountMassBundle\Entity\MassTask; use WechatOfficialAccountMassBundle\Enum\MassType; // Create a text message task $task = new MassTask(); $task->setTitle('Daily Newsletter'); $task->setType(MassType::TEXT); $task->setContent('Hello, this is a mass message!'); $task->setSendTime(new \DateTimeImmutable('+1 hour')); $task->setValid(true); // For all users $task->setTagId(null); $task->setOpenIds([]); // For specific tag $task->setTagId('100'); // For specific users $task->setOpenIds(['openid1', 'openid2']); $entityManager->persist($task); $entityManager->flush();
2. Send Messages via Command
The bundle provides a console command for sending mass messages:
php bin/console wechat:send-mass
This command will:
- Find all valid, unsent tasks where send time has passed
- Send messages via WeChat API
- Update task status and store response data
3. Automatic Sending with Cron
The SendMassCommand is configured to run automatically every minute using the cron job attribute:
#[AsCronTask(expression: '* * * * *')] class SendMassCommand extends Command
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or higher
- Doctrine ORM 3.0 or higher
- WeChat Official Account with API credentials
Configuration
This bundle requires the tourze/wechat-official-account-bundle to be properly configured with your WeChat Official Account credentials.
Required Environment Variables
Configure your WeChat credentials in .env:
# WeChat Official Account Configuration WECHAT_APP_ID=your_app_id WECHAT_APP_SECRET=your_app_secret
API Reference
Entities
MassTask
Main entity for managing mass messaging tasks.
Properties:
title(string): Task name for identificationtype(MassType): Message type (TEXT, VOICE)content(string): Message content for text messagessendTime(DateTimeImmutable): Scheduled send timetagId(string|null): Target tag ID for group messagingopenIds(array): Target user OpenIDs for individual messagingmediaId(string|null): Media ID for voice messagessent(bool): Send status indicatorvalid(bool): Task validity flagaccount(Account|null): Associated WeChat Official AccountmsgTaskId(string|null): WeChat API response task IDmsgDataId(string|null): WeChat API response data ID
Methods:
formatMessage(): Formats message data for WeChat API- Standard getters and setters for all properties
Enums
MassType
Supported message types:
TEXT= '1': Text messagesVOICE= '3': Voice messages
Repositories
MassTaskRepository
Extends Doctrine's ServiceEntityRepository for custom query methods.
Request Classes
SendToAllRequest: Send to all usersSendByTagRequest: Send to users with specific tagSendByOpenIdRequest: Send to specific users by OpenIDPreviewTaskRequest: Preview message before sendingDeleteTaskRequest: Delete scheduled task
Commands
wechat:send-mass
Sends scheduled mass messages. Configured to run automatically every minute via cron.
Usage:
php bin/console wechat:send-mass
Process:
- Queries all valid, unsent tasks where send time has passed
- Marks tasks as sent to prevent duplicate processing
- Determines target audience (all users, by tag, or by OpenID)
- Sends messages via WeChat API
- Stores API response data (msg_task_id, msg_data_id)
Advanced Usage
Voice Message Example
<?php use WechatOfficialAccountMassBundle\Entity\MassTask; use WechatOfficialAccountMassBundle\Enum\MassType; $task = new MassTask(); $task->setTitle('Voice Announcement'); $task->setType(MassType::VOICE); $task->setMediaId('your_uploaded_voice_media_id'); $task->setSendTime(new \DateTimeImmutable('+1 hour')); $task->setValid(true); $entityManager->persist($task); $entityManager->flush();
Multi-account Support
<?php use WechatOfficialAccountBundle\Entity\Account; use WechatOfficialAccountMassBundle\Entity\MassTask; // Assuming you have multiple accounts configured $account = $accountRepository->findOneBy(['name' => 'main_account']); $task = new MassTask(); $task->setAccount($account); // ... other task configuration
Task Status Monitoring
<?php // Check task status $task = $taskRepository->find($taskId); if ($task->isSent()) { echo "Task sent successfully"; echo "WeChat Task ID: " . $task->getMsgTaskId(); echo "WeChat Data ID: " . $task->getMsgDataId(); }
Security
Data Protection
- Sensitive Information: Never store WeChat API credentials in your source code. Always use environment variables or secure configuration management.
- Access Control: Implement proper authentication and authorization for mass messaging functionality to prevent unauthorized usage.
- Input Validation: All user inputs are validated using Symfony's validation constraints to prevent malicious data injection.
Best Practices
- Rate Limiting: Be aware of WeChat API rate limits to avoid account suspension.
- Audit Logging: Consider implementing audit logs for mass messaging activities.
- User Consent: Ensure you have proper consent from users before sending mass messages.
- Content Monitoring: Implement content review processes for mass messages to comply with platform policies.
Vulnerability Reporting
If you discover a security vulnerability, please report it to the project maintainers following responsible disclosure practices.
Troubleshooting
Common Issues
-
Messages not sending
- Ensure cron job is running: Check system cron configuration
- Verify task validity: Task must have
valid = true - Check send time: Must be in the past for immediate sending
-
API Errors
- Verify WeChat credentials in configuration
- Check account permissions for mass messaging
- Ensure media IDs are valid and not expired
-
Database Issues
- Run
doctrine:schema:validateto check entity mappings - Ensure database user has proper permissions
- Run
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for details.
Development Setup
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Running Tests
./vendor/bin/phpunit packages/wechat-official-account-mass-bundle/tests
License
The MIT License (MIT). Please see License File for more information.