danil005 / php-vk-bot

此包的最新版本(dev-master)没有可用的许可证信息。

dev-master 2019-02-08 15:02 UTC

This package is auto-updated.

Last update: 2024-09-09 04:17:16 UTC


README

Build Status

用于处理 VK 机器人的库。支持在对话中工作。

内容

  1. 待办事项
  2. 安装
  3. 处理命令
  4. 方法

待办事项

安装

composer require danil005/php-vk-bot:dev-master

3. 处理命令

3.1. 基本选项

3.1.1. 使用方法调用命令

为了处理命令,您需要进入 traits 文件夹并打开文件 CommantList.php。创建每个方法就是创建一个命令。同时,您只需创建仅使用小写字母的方法。

trait CommandList  
{  
	protected function hello()  
	{  
		$this->sendMessage('Hello!');  
	}
}

现在,如果在聊天中输入 "hello",机器人将回答:"Hello!"。

3.1.2. 使用方法忽略命令调用

如果您不希望在输入关键字时调用方法,请将下划线添加到方法名称的开头(function_hello

trait CommandList  
{  
   protected function _hello()  
   {  
        $this->sendMessage('Hello!');  
    
   }
}

那么在调用 _hello 或 hello 时,方法将不会执行。

3.1.3. 对建议或其他单词的反应

如果您想要对建议做出反应,请创建 CommandList 中的 cList() 方法。

protected function cList()  
{  
    return [  
        [ //Команда #1
            'text'=>'text message',  
            'method' => '_hello'  //Обязательно использовать _.
        ],
        [...], //Команда #2
        ...  
    ];
}

必须创建使用下划线的方法,否则可以调用此方法。

由一个包含嵌套数组的数组组成的数组创建了一个命令。

3.2. 扩展选项

3.2.1. 命令调用的灵活性

如果指定 text 键为数组,则机器人将对多个短语做出反应。

'text'=>['text message 1', 'text message 2']

3.2.2. 机器人对消息的反应方式

您可以通过不同的方式调用命令

  • 类似于
  • 以...开始
  • 以...结束
  • 包含
3.2.2.1. 类似于

要使用此选项,请将 | 添加到行的开头。

您可以在 [0-100] 范围内设置匹配概率。

此设置可以在 config.php 中通过 similar_percent 键进行设置。

默认值:75%。

'text'=>'|привет всем',
3.2.2.2. 以...开始

要使用此选项,请将 [| 添加到行的开头。

'text'=>'[|привет всем',
3.2.2.3. 以...结束

要使用此选项,请将 |] 添加到行的末尾。

'text'=>'привет всем|]',
3.2.2.4. 包含

要使用此选项,请用大括号 {短语} 将短语括起来。

'text'=>'{привет всем}',
3.2.2.5. 附加

此方法可以与多种调用方式一起使用。

'text'=>['[|привет', '{ку}', 'хай|]', '|здравствуйте']

3.2.3. 执行多个方法

如果指定 method 键为数组,则机器人将依次执行指定的方法。

'method'=>['_hello', '_goodbye']

4. 方法

4.1. attachments(array|string $attachments): array|string

为消息添加附件。在调用 sendMessage() 方法之前指定

示例

protected function _hello()  
{  
  $this->attachments('photo-100172_166443618');
  //$this->attachments(['photo-100172_166443618', 'photo-124172_166443618'])
  $this->sendMessage(['Hello!', 'HI!']);  
}

如果指定了两个或更多连续的 attachments 方法,则将使用最后一个。

媒体附件类型

更多信息请访问官方网站 VK.COM (messages.send)

4.2. sendMessage(array|string $message, int $peerId = null): void

向用户/群组发送消息。

示例

$this->sendMessage(['Hi', 'Hello!']);
//Result: Hi

4.3. sendPhoto(arrray|string $photos, int $peerId = null):void

发送照片,不使用附件方法。请使用 <owner_id>_<media_id>

照片类型默认为 photo。例如 photo-100172_166443618,只需插入选中的部分。

示例

$this->sendPhoto('175343153_456239018');

4.4. sendVideo(arrray|string $videos, int $peerId = null):void

发送视频,不使用附件方法。遵循与 sendPhoto 相同的规则。

示例

$this->sendVideo('175343153_456239018');

4.5. sendDoc(arrray|string $docs, int $peerId = null):void

发送文档,不使用附件方法。遵循与 sendPhoto 相同的规则。

示例

$this->sendDoc('175343153_456239018');

4.6. sendAudio(arrray|string $audio, int $peerId = null):void

发送音频,不使用附件方法。遵循与 sendPhoto 相同的规则。

示例

$this->sendAudio('175343153_456239018');

4.7. sendWall(arrray|string $walls, int $peerId = null):void

发送墙上的帖子,不使用附件方法。遵循与 sendPhoto 相同的规则。

示例

$this->sendWall('175343153_456239018');

4.8. sendPoll(arrray|string $polls, int $peerId = null):void

发送调查问卷,不使用附件方法。遵循与 sendPhoto 相同的规则。

示例

$this->sendPoll('175343153_456239018');

4.9. sendMarket(arrray|string $items, int $peerId = null):void

发送商品,不使用附件方法。遵循与 sendPhoto 相同的规则。

示例

$this->sendMarket('175343153_456239018');