PHP 微信机器人 Vbot 结合 Laravel 基础入门及实例

新来了项目,需要能监听指定微信群的消息并进行转发。基于 PHP 7 的 web 微信机器人 Vbot 可以满足需求。Vbot 本质上就是实现了登录网页版微信来进行自动回复、群管理等等操作。

github 地址:https://github.com/hanson/vbot,官网地址:http://create.hanc.cc/vbot/

安装

环境要求:

PHP >= 7.0.0

PHP fileinfo 扩展

PHP gd 扩展

PHP SimpleXML 扩展

 安装命令:

composer require hanson/vbot

基本使用

接下来是干货了。由于项目需求较简单,没有涉及到高深的东西,读者各取所需就行。

项目框架是 Laravel,从指定群中监听消息,如果符合格式,则自动转发到目标群中。

Vbot 的使用一般分为四步:初始化 Vbot 实例;设置消息处理器;设置监听器;启动 Vbot 服务。

初始化 Vbot 实例

Vbot 初始化配置只是修改指定了下载、日志、缓存文件等等的存储路径。

config/vbot.conf:

 1 <?php
 2 
 3 $path = storage_path('wechat');
 4 return [
 5     'path' => $path,
 6     /*
 7      * swoole 配置项(执行主动发消息命令必须要开启,且必须安装 swoole 插件)
 8      */
 9     'swoole' => [
10         'status' => false,
11         'ip' => '127.0.0.1',
12         'port' => '8866',
13     ],
14     /*
15      * 下载配置项
16      */
17     'download' => [
18         'image' => true,
19         'voice' => true,
20         'video' => true,
21         'emoticon' => true,
22         'file' => true,
23         'emoticon_path' => $path . '/emoticons', // 表情库路径(PS:表情库为过滤后不重复的表情文件夹)
24     ],
25     /*
26      * 输出配置项
27      */
28     'console' => [
29         'output' => true, // 是否输出
30         'message' => true, // 是否输出接收消息 (若上面为 false 此处无效)
31     ],
32     /*
33      * 日志配置项
34      */
35     'log' => [
36         'level' => 'debug',
37         'permission' => 0777,
38         'system' => $path . '/log', // 系统报错日志
39         'message' => $path . '/log', // 消息日志
40     ],
41     /*
42      * 缓存配置项
43      */
44     'cache' => [
45         'default' => 'file', // 缓存设置 (支持 redis 或 file)
46         'stores' => [
47             'file' => [
48                 'driver' => 'file',
49                 'path' => $path . '/cache',
50             ],
51             'redis' => [
52                 'driver' => 'redis',
53                 'connection' => 'default',
54             ],
55         ],
56     ],
57     /*
58      * 拓展配置
59      * ==============================
60      * 如果加载拓展则必须加载此配置项
61      */
62     'extension' => [
63         // 管理员配置(必选),优先加载 remark(备注名)
64         'admin' => [
65             'remark' => '',
66             'nickname' => '',
67         ],
68         // 'other extension' => [ ... ],
69     ],
70 ];
View Code

app/Console/Commands/SendVbot.php:

1 public function handle()
2 {
3     $vbot = new Vbot(config('vbot_conf'));
4 }
View Code

设置消息处理器

app/Console/Commands/SendVbot.php:

 1 public function handle()
 2 {
 3     ...
 4     $myvbot = app(MyVbot::class);
 5 
 6     // 获取消息处理器实例
 7     $messageHandler = $vbot->messageHandler;
 8 
 9     // 收到消息时触发
10     $messageHandler->setHandler([$myvbot, 'messageHandler']);
11 }
View Code

app/Handlers/MyVbot:

 1 <?php
 2 
 3 namespace AppHandlers;
 4 
 5 use HansonVbotMessageText;
 6 use IlluminateSupportCollection;
 7 
 8 class MyVbot
 9 {
10     public function messageHandler(Collection $message)
11     {
12         // 消息发送者类型
13         $fromType = $message['fromType'] ?? null;
14         // 消息类型
15         $type = $message['type'] ?? null;
16         // 经过处理显示在控制台的消息
17         $content = $message['content'] ?? null;
18         // 转格式后的消息
19         $message_in = $message['message'] ?? null;
20         // 发送者的 Username,当为群消息时此值为 sender 的 username
21         $username = $message['username'] ?? null;
22 
23         // 消息来源
24         $fromUserName = $message['from']['UserName'] ?? null;
25         $fromNickName = $message['from']['NickName'] ?? null;
26 
27         // 群消息发送者
28         $senderUserName = $message['sender']['UserName'] ?? null;
29         $senderNickName = $message['sender']['NickName'] ?? null;
30 
31         ...
32         
33         vbot('console')->log("【转发消息】:{$content}");
34         Text::send($group_username, $content);
35 
36         ...
37     }
38 }
View Code

设置监听器

app/Console/Commands/SendVbot.php:

 1 public function handle()
 2 {
 3     ...
 4     $myobserver = app(MyObserver::class);
 5 
 6     // 获取监听器实例
 7     $observer = $vbot->observer;
 8 
 9     // 二维码监听器
10     $observer->setQrCodeObserver([$myobserver, 'setQrCodeObserver']);
11 
12     $observer->setLoginSuccessObserver([$myobserver, 'setLoginSuccessObserver']);
13 
14     $observer->setExitObserver([$myobserver, 'setExitObserver']);
15 }
View Code

app/Observers/MyObserver.php:

 1 <?php
 2 
 3 namespace AppObservers;
 4 
 5 use AppRepositoriesDing2Repository;
 6 
 7 class MyObserver
 8 {
 9     protected $ding2Repository;
10     protected $uri;
11     protected $console;
12 
13     public function __construct(Ding2Repository $ding2Repository)
14     {
15         $this->ding2Repository = $ding2Repository;
16         $this->console =  vbot('console');
17         $this->uri = 'https://oapi.dingtalk.com/robot/send?access_token=xxx';
18     }
19 
20     public function setQrCodeObserver($qrCodeUrl)
21     {
22         $qrcode_url = str_replace('/l/', '/qrcode/', $qrCodeUrl);
23         $this->ding2Repository->robotQrSend($this->uri, $qrcode_url);
24     }
25 
26     public function setLoginSuccessObserver()
27     {
28         $this->ding2Repository->robotLoginSuccessSend($this->uri);
29 
30         $this->console->log('登录成功');
31     }
32 
33     public function setExitObserver()
34     {
35         $this->ding2Repository->robotExitSend($this->uri);
36 
37         $this->console->log('程序退出');
38     }
39 }
View Code

启动 Vbot 服务

1 public function handle()
2 {
3     ...
4     try {
5         $vbot->server->serve();
6     } catch (Exception $e) {
7         $this->error($e->getMessage());
8     }
9 }
View Code

编码完成之后就可以运行 PHP 命令来启动 Vbot 进程。

原文地址:https://www.cnblogs.com/imzhi/p/php-vbot-with-laravel.html