PHP code example of tourze / wechat-work-intercept-rule-bundle

1. Go to this page and download the library: Download tourze/wechat-work-intercept-rule-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-work-intercept-rule-bundle example snippets


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

use WechatWorkInterceptRuleBundle\Entity\InterceptRule;
use WechatWorkInterceptRuleBundle\Enum\InterceptType;

// Create a new intercept rule
$rule = new InterceptRule();
$rule->setName('Marketing Keywords');
$rule->setWordList(['spam', 'promotion', 'advertisement']);
$rule->setInterceptType(InterceptType::WARN);

use WechatWorkInterceptRuleBundle\Request\GetInterceptRuleListRequest;
use WechatWorkInterceptRuleBundle\Request\GetInterceptRuleDetailRequest;
use WechatWorkInterceptRuleBundle\Request\AddInterceptRuleRequest;
use WechatWorkInterceptRuleBundle\Request\UpdateInterceptRuleRequest;
use WechatWorkInterceptRuleBundle\Request\DeleteInterceptRuleRequest;

// Get list of intercept rules
$request = new GetInterceptRuleListRequest();
$request->setAgent($agent);
$response = $workService->request($request);

// Get detailed rule information
$detailRequest = new GetInterceptRuleDetailRequest();
$detailRequest->setAgent($agent);
$detailRequest->setRuleId('rule_id');
$detail = $workService->request($detailRequest);

use WechatWorkInterceptRuleBundle\Repository\InterceptRuleRepository;

// Inject the repository
public function __construct(
    private InterceptRuleRepository $ruleRepository
) {}

// Find rules by corporation
$rules = $this->ruleRepository->findBy(['corp' => $corp]);

// Find rule by remote rule ID
$rule = $this->ruleRepository->findOneBy([
    'corp' => $corp,
    'ruleId' => 'remote_rule_id'
]);

use WechatWorkInterceptRuleBundle\EventSubscriber\InterceptRuleListener;

use WechatWorkInterceptRuleBundle\Entity\InterceptRule;

class CustomRuleProcessor
{
    public function processRule(InterceptRule $rule): bool
    {
        // Custom processing logic
        $wordList = $rule->getWordList();
        $interceptType = $rule->getInterceptType();
        
        // Implement your custom rule processing
        return $this->applyCustomLogic($wordList, $interceptType);
    }
}

use WechatWorkInterceptRuleBundle\Repository\InterceptRuleRepository;
use Doctrine\ORM\EntityManagerInterface;

class BulkRuleManager
{
    public function __construct(
        private InterceptRuleRepository $repository,
        private EntityManagerInterface $entityManager
    ) {}
    
    public function bulkUpdate(array $rules): void
    {
        foreach ($rules as $ruleData) {
            $rule = $this->repository->findOneBy(['ruleId' => $ruleData['id']]);
            if ($rule) {
                $rule->setWordList($ruleData['wordList']);
                $rule->setInterceptType($ruleData['interceptType']);
            }
        }
        
        $this->entityManager->flush();
    }
}

use WechatWorkInterceptRuleBundle\Entity\InterceptRule;

class ExternalFilterIntegration
{
    public function syncWithExternalSystem(InterceptRule $rule): void
    {
        $externalData = [
            'rule_id' => $rule->getRuleId(),
            'keywords' => $rule->getWordList(),
            'action' => $rule->getInterceptType()->value,
        ];
        
        // Send to external system
        $this->externalClient->updateRule($externalData);
    }
}

#[AsCronTask(expression: '*/10 * * * *')]
bash
php bin/console wechat-work:sync-intercept-rule