PHP code example of tourze / wechat-official-account-mass-bundle

1. Go to this page and download the library: Download tourze/wechat-official-account-mass-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / wechat-official-account-mass-bundle example snippets




return [
    // ...
    WechatOfficialAccountMassBundle\WechatOfficialAccountMassBundle::class => ['all' => true],
];



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();

#[AsCronTask(expression: '* * * * *')]
class SendMassCommand extends Command



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();



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



// 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();
}
bash
php bin/console wechat:send-mass
bash
php bin/console wechat:send-mass