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 進程。
總結
以上是生活随笔為你收集整理的PHP 微信机器人 Vbot 结合 Laravel 基础入门及实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 24 Swap Nod
- 下一篇: tensorflow的一些函数