PHP code example of qiuxiang / webot

1. Go to this page and download the library: Download qiuxiang/webot 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/ */

    

qiuxiang / webot example snippets


$webot = new Wechat\Webot('token');

// 建立 hello => world 规则
$webot->rules->add('hello', 'world');

// 使用正则表达式
$webot->rules->add('(c|course).*(\d+)', 'some course');

// 如果 handler 是数组,则随机回复一条消息
$webot->rules->add('rand', array('1', '2', '3'));

// 订阅事件处理
$webot->on('event.subscribe', function ($depends) {
  $depends->response->->text('welcome');
});

$webot->run();

// 回复单图文消息
$webot->rules->add('what', array('news' => array(
  'title' => '标题',
  'content' => '描述',
  'picture' => 'http://example.com/picture.jpg',
  'url' => 'http://example.com',
)));

// 回复多图文消息
$webot->rules->add('what', array('news' => array(
  array(
    'title' => '标题1',
    'picture' => 'http://example.com/picture1.jpg',
    'url' => 'http://example.com/1',
  ),
  array(
    'title' => '标题2',
    'picture' => 'http://example.com/picture2.jpg',
    'url' => 'http://example.com/2',
  ),
));

// 回复文本消息
$webot->rules->add('hello', function () {
  return 'world';
});

// 回复图文消息
$webot->rules->add('what', function () {
  return array(
    'news' => array(
      'title' => '标题',
      'content' => '描述',
      'picture' => 'http://example.com/picture.jpg',
      'url' => 'http://example.com',
    )
  );
});

// 当 pattern 为正则表达式时,matchs 会作为函数的参数传入
$webot->rules->add('(1)+(2)', function ($matchs) {
  return $matchs[1] + $matchs[2]; // 3
});

$webot->rules->loadPhp('rules.php');
$webot->rules->loadJson('rules.json');
$webot->rules->loadYaml('rules.yml');

// 同时加载多个文件
$webot->rules->loadJson(array(
  'rules1.json',
  'rules2.json',
));

// glob 表达式
$webot->rules->loadYaml('rules/yaml/*.yml');



return array(
  'pattern' => 'h1',
  'test' => array(
    'news' => array(
      'title' => 'hello',
      'content' => 'world',
    ),
  ),
  'p(.*)3' => function () {
    return 'hello';
  }
);

$webot->$menus->add('新闻', array('news' => array(
  array(
    'title' => '标题1',
    'picture' => 'http://example.com/1.jpg',
    'url' => 'http://example.com/news/1',
  ),
  array(
    'title' => '标题2',
    'picture' => 'http://example.com/2.jpg',
    'url' => 'http://example.com/news/2',
  ),
)));

$webot->$menus->loadPhp('menus.php');

$webot->on('event.click', function ($depends) {
  // $request->eventKey 事件代码
  // $request->fromUserName 用户 OpenID
  $depends->response->text('hello');
});