Commit 9c20350c by HanSon Committed by GitHub

Merge pull request #32 from HanSon/dev

增加聊天操作、增加消息类型小程序
2 parents 16165e7e 2bd2ca6c
......@@ -39,6 +39,8 @@ PS:运行后二维码将保存于设置的缓存目录,命名为qr.png,控
[一键拜年](https://github.com/HanSon/vbot/blob/master/example/bainian.php)
[聊天操作](https://github.com/HanSon/vbot/blob/master/example/contact.php)
## 基本使用
......@@ -84,7 +86,7 @@ $robot->server->run();
# to do list
- [ ] 消息处理
- [x] 消息处理
- [x] 文字
- [x] 图片
- [x] 语音
......@@ -98,7 +100,7 @@ $robot->server->run();
- [x] 好友验证
- [x] 分享
- [x] 公众号推送
- [ ] 小程序
- [x] 小程序
- [x] 消息存储
- [x] 语音
......@@ -112,19 +114,19 @@ $robot->server->run();
- [x] 发送表情
- [x] 发送视频
- [ ] 群操作
- [ ] 创建群
- [ ] 把某人踢出群
- [ ] 邀请好友加入群
- [ ] 修改群名称
- [x] 群操作
- [x] 创建群
- [x] 把某人踢出群
- [x] 邀请好友加入群
- [x] 修改群名称
- [ ] 好友操作
- [ ] 给好友添加备注
- [x] 好友操作
- [x] 给好友添加备注
- [x] 通过好友验证
- [ ] 聊天窗口操作
- [ ] 置顶聊天会话
- [ ] 取消聊天会话指定
- [x] 聊天窗口操作
- [x] 置顶聊天会话
- [x] 取消聊天会话指定
- [ ] 命令行操作信息发送
......
<?php
/**
* Created by PhpStorm.
* User: HanSon
* Date: 2016/12/7
* Time: 16:33
*/
require_once __DIR__ . './../vendor/autoload.php';
use Hanson\Vbot\Foundation\Vbot;
use Hanson\Vbot\Message\Entity\Text;
use Hanson\Vbot\Support\Console;
$robot = new Vbot([
'tmp' => __DIR__ . '/./../tmp/',
'debug' => true
]);
$robot->server->setMessageHandler(function ($message) {
if(str_contains($message->content, '设置备注')){
$result = contact()->setRemarkName($message->from['UserName'], str_replace('设置备注', '', $message->content));
Console::log('设置备注:' . ($result ? '成功' : '失败'));
}
if(str_contains($message->content, '设置置顶')){
$result = contact()->setStick($message->from['UserName']);
Console::log('设置置顶:' . ($result ? '成功' : '失败'));
}
if(str_contains($message->content, '取消置顶')){
$result = contact()->setStick($message->from['UserName'], false);
Console::log('取消置顶:' . ($result ? '成功' : '失败'));
}
if(str_contains($message->content, '拉群测试')){
$username[] = contact()->getUsernameById('...');
$username[] = contact()->getUsernameById('...');
$group = group()->create($username);
Text::send($group['UserName'], '创建群聊天成功');
}
if(str_contains($message->content, '拉人')){
$nicknames = explode(',', str_replace('拉人', '', $message->content));
$members = [];
foreach ($nicknames as $nickname) {
$members[] = contact()->getUsernameByNickname($nickname);
}
$result = group()->addMember($message->from['UserName'], $members);
Console::log($result ? '拉人成功' : '拉人失败');
}
if(str_contains($message->content, '踢人')){
$nicknames = explode(',', str_replace('踢人', '', $message->content));
$members = [];
foreach ($nicknames as $nickname) {
$members[] = contact()->getUsernameByNickname($nickname);
}
$result = group()->deleteMember($message->from['UserName'], $members);
Console::log($result ? '踢人成功' : '踢人失败');
}
if(str_contains($message->content, '设置群名称')){
$result = group()->setGroupName($message->from['UserName'], str_replace('设置群名称', '', $message->content));
Console::log('设置群名称:' . ($result ? '成功' : '失败'));
}
});
$robot->server->run();
......@@ -23,6 +23,7 @@ use Hanson\Vbot\Message\Entity\Recommend;
use Hanson\Vbot\Message\Entity\Share;
use Hanson\Vbot\Message\Entity\Official;
use Hanson\Vbot\Message\Entity\Touch;
use Hanson\Vbot\Message\Entity\Mina;
use Hanson\Vbot\Message\Entity\RequestFriend;
$path = __DIR__ . '/./../tmp/';
......@@ -151,13 +152,17 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
return $reply;
}
// 分享小程序信息
if($message instanceof Mina){
/** @var $message Mina */
$reply = "收到小程序\n小程序名词:{$message->title}\n链接:{$message->url}";
return $reply;
}
// 公众号推送信息
if($message instanceof Official){
/** @var $message Official */
$reply = "收到公众号推送\n标题:{$message->title}\n描述:{$message->description}\n链接:{$message->url}";
if($message->app){
$reply .= "\n来源公众号名称:{$message->app}";
}
$reply = "收到公众号推送\n标题:{$message->title}\n描述:{$message->description}\n链接:{$message->url}\n来源公众号名称:{$message->app}";
return $reply;
}
......
......@@ -63,7 +63,7 @@ class Contact extends Collection
});
}
/**
* 根据通讯录中的名字获取通讯对象
* 根据通讯录中的备注获取通讯对象
*
* @param $id
* @return mixed
......@@ -77,5 +77,47 @@ class Contact extends Collection
});
}
/**
* 根据通讯录中的昵称获取通讯对象
*
* @param $nickname
* @return mixed
*/
public function getUsernameByNickname($nickname)
{
return $this->search(function($item, $key) use ($nickname){
if($item['NickName'] === $nickname){
return true;
}
});
}
public function setRemarkName($username, $remarkName)
{
$url = sprintf('%s/webwxoplog?lang=zh_CN&pass_ticket=%s', server()->baseUri, server()->passTicket);
$result = http()->post($url, json_encode([
'UserName' => $username,
'CmdId' => 2,
'RemarkName' => $remarkName,
'BaseRequest' => server()->baseRequest
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), true);
return $result['BaseResponse']['Ret'] == 0;
}
public function setStick($username, $isStick = true)
{
$url = sprintf('%s/webwxoplog?lang=zh_CN&pass_ticket=%s', server()->baseUri, server()->passTicket);
$result = http()->json($url, [
'UserName' => $username,
'CmdId' => 3,
'OP' => (int) $isStick,
'BaseRequest' => server()->baseRequest
], true);
return $result['BaseResponse']['Ret'] == 0;
}
}
\ No newline at end of file
......@@ -58,7 +58,7 @@ class ContactFactory
$this->getBatchGroupMembers();
myself()->alias = contact()->get(myself()->username)['Alias'] ? : myself()->username;
myself()->alias = contact()->get(myself()->username)['Alias'] ?? myself()->nickname ? : myself()->username;
if(server()->config['debug']){
FileManager::download('contact.json', json_encode(contact()->all()));
......
......@@ -61,4 +61,133 @@ class Group extends Collection
return $groups;
}
/**
* 创建群聊天
*
* @param array $contacts
* @return bool
*/
public function create(array $contacts)
{
$url = sprintf('%s/webwxcreatechatroom?lang=zh_CN&r=%s', server()->baseUri, time());
$result = http()->json($url, [
'MemberCount' => count($contacts),
'MemberList' => $this->makeMemberList($contacts),
'Topic' => '',
'BaseRequest' => server()->baseRequest
], true);
if($result['BaseResponse']['Ret'] != 0){
return false;
}
return $this->add($result['ChatRoomName']);
}
/**
* 删除群成员
*
* @param $group
* @param $members
* @return bool
*/
public function deleteMember($group, $members)
{
$members = is_string($members) ? [$members] : $members;
$result = http()->json(sprintf('%s/webwxupdatechatroom?fun=delmember&pass_ticket=%s', server()->baseUri, server()->passTicket), [
'BaseRequest' => server()->baseRequest,
'ChatRoomName' => $group,
'DelMemberList' => implode(',', $members)
], true);
return $result['BaseResponse']['Ret'] == 0;
}
/**
* 添加群成员
*
* @param $groupUsername
* @param $members
* @return bool
*/
public function addMember($groupUsername, $members)
{
$group = group()->get($groupUsername);
$groupCount = count($group['MemberList']);
list($fun, $key) = $groupCount > 40 ? ['invitemember', 'InviteMemberList'] : ['addmember', 'AddMemberList'];
$members = is_string($members) ? [$members] : $members;
$result = http()->json(sprintf('%s/webwxupdatechatroom?fun=%s&pass_ticket=%s', server()->baseUri, $fun, server()->passTicket), [
'BaseRequest' => server()->baseRequest,
'ChatRoomName' => $groupUsername,
$key => implode(',', $members)
], true);
return $result['BaseResponse']['Ret'] == 0;
}
/**
* 设置群名称
*
* @param $group
* @param $name
* @return bool
*/
public function setGroupName($group, $name)
{
$result = http()->post(sprintf('%s/webwxupdatechatroom?fun=modtopic&pass_ticket=%s', server()->baseUri, server()->passTicket),
json_encode([
'BaseRequest' => server()->baseRequest,
'ChatRoomName' => $group,
'NewTopic' => $name
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), true);
return $result['BaseResponse']['Ret'] == 0;
}
/**
* 增加群聊天到group
*
* @param $username
* @return bool
*/
private function add($username)
{
$result = http()->json(sprintf('%s/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s', server()->baseUri, time(), server()->passTicket), [
'Count' => 1,
'BaseRequest' => server()->baseRequest,
'List' => [
[
'ChatRoomId' => '',
'UserName' => $username
]
]
], true);
if($result['BaseResponse']['Ret'] != 0){
Console::log('增加聊天群组失败 '.$username, Console::WARNING);
return false;
}
group()->put($username, $result['ContactList'][0]);
return $result['ContactList'][0];
}
/**
* 生成member list 格式
*
* @param $contacts
* @return array
*/
private function makeMemberList($contacts)
{
$memberList = [];
foreach ($contacts as $contact) {
$memberList[] = ['UserName' => $contact];
}
return $memberList;
}
}
\ No newline at end of file
......@@ -17,12 +17,12 @@ use Hanson\Vbot\Message\Entity\Recall;
use Hanson\Vbot\Message\Entity\Recommend;
use Hanson\Vbot\Message\Entity\RedPacket;
use Hanson\Vbot\Message\Entity\RequestFriend;
use Hanson\Vbot\Message\Entity\ShareFactory;
use Hanson\Vbot\Message\Entity\Text;
use Hanson\Vbot\Message\Entity\Touch;
use Hanson\Vbot\Message\Entity\Transfer;
use Hanson\Vbot\Message\Entity\Video;
use Hanson\Vbot\Message\Entity\Voice;
use Hanson\Vbot\Message\ShareFactory;
class MessageFactory
{
......
......@@ -79,6 +79,7 @@ class Message
$this->fromType = 'System';
} elseif ($this->msg['FromUserName'] === myself()->username) {
$this->fromType = 'Self';
$this->from = account()->getAccount($this->msg['ToUserName']);
} elseif (substr($this->msg['FromUserName'], 0, 2) === '@@') { # group
$this->fromType = 'Group';
} elseif (contact()->get($this->msg['FromUserName'])) {
......
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2017/1/15
* Time: 12:29
*/
namespace Hanson\Vbot\Message\Entity;
use Hanson\Vbot\Message\MessageInterface;
class Mina extends Message implements MessageInterface
{
public $title;
public $url;
public function __construct($msg)
{
parent::__construct($msg);
$this->make();
}
public function make()
{
$array = (array)simplexml_load_string($this->msg['Content'], 'SimpleXMLElement', LIBXML_NOCDATA);
$info = (array)$array['appmsg'];
$this->title = $info['title'];
$this->url = $info['url'];
}
}
\ No newline at end of file
......@@ -6,8 +6,12 @@
* Time: 12:29
*/
namespace Hanson\Vbot\Message\Entity;
namespace Hanson\Vbot\Message;
use Hanson\Vbot\Message\Entity\File;
use Hanson\Vbot\Message\Entity\Mina;
use Hanson\Vbot\Message\Entity\Official;
use Hanson\Vbot\Message\Entity\Share;
use Hanson\Vbot\Support\Content;
class ShareFactory
......@@ -27,6 +31,8 @@ class ShareFactory
return new File($msg);
}else if(official()->get($msg['FromUserName'])){
return new Official($msg);
}else if($this->type == 33){
return new Mina($msg);
}else{
return new Share($msg);
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!