PHPackages                             qiuxiang/webot - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. qiuxiang/webot

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

qiuxiang/webot
==============

wechat robot

v0.1.0(11y ago)015MITPHPPHP &gt;=5.3.0

Since May 15Pushed 11y ago1 watchersCompare

[ Source](https://github.com/qiuxiang/webot)[ Packagist](https://packagist.org/packages/qiuxiang/webot)[ Docs](http://github.com/qiuxiang/webot)[ RSS](/packages/qiuxiang-webot/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (5)Used By (0)

微信开发开发框架
========

[](#微信开发开发框架)

Webot 旨在提供简单的方式搭建灵活的、易于扩展的微信公众平台，其灵感来自于 [weixin-robot](https://github.com/node-webot/weixin-robot)。

基本用法
----

[](#基本用法)

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

添加规则
----

[](#添加规则)

`rules->add(string $pattern, mixed $handler)`

### 回复更多形式的消息

[](#回复更多形式的消息)

```
// 回复单图文消息
$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
});
```

### 从 PHP、YAML 或 JSON 文件加载规则

[](#从-phpyaml-或-json-文件加载规则)

```
$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');
```

rules.php

```
