新来了项目,需要能监听指定微信群的消息并进行转发。基于 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:

 <?php

 $path = storage_path('wechat');
return [
'path' => $path,
/*
* swoole 配置项(执行主动发消息命令必须要开启,且必须安装 swoole 插件)
*/
'swoole' => [
'status' => false,
'ip' => '127.0.0.1',
'port' => '8866',
],
/*
* 下载配置项
*/
'download' => [
'image' => true,
'voice' => true,
'video' => true,
'emoticon' => true,
'file' => true,
'emoticon_path' => $path . '/emoticons', // 表情库路径(PS:表情库为过滤后不重复的表情文件夹)
],
/*
* 输出配置项
*/
'console' => [
'output' => true, // 是否输出
'message' => true, // 是否输出接收消息 (若上面为 false 此处无效)
],
/*
* 日志配置项
*/
'log' => [
'level' => 'debug',
'permission' => 0777,
'system' => $path . '/log', // 系统报错日志
'message' => $path . '/log', // 消息日志
],
/*
* 缓存配置项
*/
'cache' => [
'default' => 'file', // 缓存设置 (支持 redis 或 file)
'stores' => [
'file' => [
'driver' => 'file',
'path' => $path . '/cache',
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
],
],
/*
* 拓展配置
* ==============================
* 如果加载拓展则必须加载此配置项
*/
'extension' => [
// 管理员配置(必选),优先加载 remark(备注名)
'admin' => [
'remark' => '',
'nickname' => '',
],
// 'other extension' => [ ... ],
],
];

app/Console/Commands/SendVbot.php:

 public function handle()
{
$vbot = new Vbot(config('vbot_conf'));
}

设置消息处理器

app/Console/Commands/SendVbot.php:

 public function handle()
{
...
$myvbot = app(MyVbot::class); // 获取消息处理器实例
$messageHandler = $vbot->messageHandler; // 收到消息时触发
$messageHandler->setHandler([$myvbot, 'messageHandler']);
}

app/Handlers/MyVbot:

 <?php

 namespace App\Handlers;

 use Hanson\Vbot\Message\Text;
use Illuminate\Support\Collection; class MyVbot
{
public function messageHandler(Collection $message)
{
// 消息发送者类型
$fromType = $message['fromType'] ?? null;
// 消息类型
$type = $message['type'] ?? null;
// 经过处理显示在控制台的消息
$content = $message['content'] ?? null;
// 转格式后的消息
$message_in = $message['message'] ?? null;
// 发送者的 Username,当为群消息时此值为 sender 的 username
$username = $message['username'] ?? null; // 消息来源
$fromUserName = $message['from']['UserName'] ?? null;
$fromNickName = $message['from']['NickName'] ?? null; // 群消息发送者
$senderUserName = $message['sender']['UserName'] ?? null;
$senderNickName = $message['sender']['NickName'] ?? null; ... vbot('console')->log("【转发消息】:{$content}");
Text::send($group_username, $content); ...
}
}

设置监听器

app/Console/Commands/SendVbot.php:

 public function handle()
{
...
$myobserver = app(MyObserver::class); // 获取监听器实例
$observer = $vbot->observer; // 二维码监听器
$observer->setQrCodeObserver([$myobserver, 'setQrCodeObserver']); $observer->setLoginSuccessObserver([$myobserver, 'setLoginSuccessObserver']); $observer->setExitObserver([$myobserver, 'setExitObserver']);
}

app/Observers/MyObserver.php:

 <?php

 namespace App\Observers;

 use App\Repositories\Ding2Repository;

 class MyObserver
{
protected $ding2Repository;
protected $uri;
protected $console; public function __construct(Ding2Repository $ding2Repository)
{
$this->ding2Repository = $ding2Repository;
$this->console = vbot('console');
$this->uri = 'https://oapi.dingtalk.com/robot/send?access_token=xxx';
} public function setQrCodeObserver($qrCodeUrl)
{
$qrcode_url = str_replace('/l/', '/qrcode/', $qrCodeUrl);
$this->ding2Repository->robotQrSend($this->uri, $qrcode_url);
} public function setLoginSuccessObserver()
{
$this->ding2Repository->robotLoginSuccessSend($this->uri); $this->console->log('登录成功');
} public function setExitObserver()
{
$this->ding2Repository->robotExitSend($this->uri); $this->console->log('程序退出');
}
}

启动 Vbot 服务

 public function handle()
{
...
try {
$vbot->server->serve();
} catch (Exception $e) {
$this->error($e->getMessage());
}
}

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

最新文章

  1. Swift学习(一):自定义运算符 operator
  2. PHP curl 模拟POST 上传文件(含php 5.5后CURLFile)
  3. VS 扩展推荐
  4. List、ArrayList、Vector及map、HashTable、HashMap分别的区别
  5. C#_Jquery无刷新上传
  6. VC++的文件格式详解
  7. kernel_task占用大量CPU
  8. 关于android应用闪屏的几种情况
  9. Hdu5737-Differencia(有序表线段树)
  10. Visual Studio 2013 上使用 Github
  11. fp oo
  12. Liferay中利用URL传参数
  13. JS前端验证代码
  14. HashMap源码解析(JDK1.8)
  15. Spring mvc中junit测试遇到com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException错误怎么解决
  16. Ocelot + Consul + Registrator 基于Docker 实现服务发现、服务自动注册
  17. robocopy的用法,数据库局域网备份
  18. Python自动化开发 - Python操作Memcached、Redis、RabbitMQ
  19. Android下查看共享库依赖项
  20. 深入浅出!从语义角度分析隐藏在Unity协程背后的原理

热门文章

  1. 解决homebrew下install时出现的问题
  2. 【Offer】[16] 【数值的整数次方】
  3. Java中String为什么是不可变的
  4. NUMA导致的MySQL服务器SWAP问题分析
  5. JAVA测试(选择题)
  6. 判断是手机端还是PC短访问
  7. 《JavaScript设计模式与开发实践》读书笔记-基础知识
  8. oracle 常用脚本以及语句
  9. 20 (OC)* GCD、NSOperation、NSThread。多线程
  10. preg_relace_callback不起作用匿名函数不启作用替换字符串中的所有图片