PHP code example of mitoop / robot
1. Go to this page and download the library: Download mitoop/robot library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mitoop / robot example snippets
use Mitoop\Robot\Robot;
$config = [
// 默认发送的分组 支持多个
'default' => ['feishu.jishu'],
// bool值 是否显示服务器env 为 true 消息头部将会显示服务器env 未配置时缺省为 true
'show_env' => true,
// 发送消息的服务器env 如: production/development/local 等, 表示消息来自哪个环境
'env' => 'production',
// 发送通知的超时时间(秒)
'timeout' => 5,
// 机器人提供商 feishu : 飞书 / wecom : 企业微信 / dingding : 钉钉 / lark : Lark
'channels' => [
// 飞书
'feishu' => [
// demo 技术组
'jishu' => [
// 【必填】webhook 地址, 可以只填写最后的 uuid, 当然也支持全链接
'webhook' => '00000000-0000-0000-0000-000000000000',
// 【可选】分组默认 at 的成员 手机号或者/all
'at' => ['all'],
// 【可选】校验密钥
'secret' => '',
]
],
// 配置同 feishu 相同(v2.1 新增)
'lark' => [
// demo 技术组
'jishu' => [
// 【必填】webhook 地址, 可以只填写最后的 uuid, 当然也支持全链接
'webhook' => '00000000-0000-0000-0000-000000000000',
// 【可选】分组默认 at 的成员 手机号或者/all
'at' => ['all'],
// 【可选】校验密钥
'secret' => '',
]
],
// 企业微信 配置解释参考飞书(企业微信没有密钥校验策略)
'wecom' => [
'jishu' => [
// 【必填】webhook 地址, 可以只填写最后的 key, 当然也支持全链接
'webhook' => 'key',
'at' => ['all'],
],
],
// 钉钉 配置解释参考飞书
'dingding' => [
'jishu' => [
// 【必填】webhook 地址, 可以只填写最后的 access_token, 当然也支持全链接
'webhook' => 'access_token',
'at' => ['13888888888'],
'secret' => ''
],
],
],
];
$robot = new Robot($config);
$robot->sendMarkdownMsg('### Markdown');
$robot->sendTextMsg('debug回调信息', [
'method' => 'callback/get_item_detail',
'ip' => '127.0.0.1',
'ua' => 'okhttp/3.12.3',
'request_data' => [
'foo' => 'bar',
],
'time' => 1619754172,
]);
$markdownMessage = "### 五一值班安排 \n
五月一号:技术: 技术甲, 客服: 客服甲 \n
五月二号:技术: 技术乙, 客服: 客服乙 \n
五月三号:技术: 技术丙, 客服: 客服丙 \n
五月四号:技术: 技术丁, 客服: 客服丁";
$robot->sendMarkdownMsg($markdownMessage);
$dingdingMessage = "### 五一值班安排 \n";
$wecomMessage = "### <font color='info'>五一值班安排</font> ";
use Mitoop\Robot\Channels\Channel;
$robot->group(['dingding.jishu', 'wecom.jishu'])
->sendMarkdownMsg(function(Channel $channel)use($dingdingMessage, $wecomMessage){
if($channel->getName() == 'wecom') {
return $wecomMessage;
}
if($channel->getName() == 'dingding') {
return $dingdingMessage;
}
});
$data = []; // 各家原始的消息体
$robot->sendRawMsg($data);
$robot->group(['dingding.jishu', 'wecom.jishu'])->sendRawMsg($data);
$robot->group(['feishu.kefu', 'wecom.jishu'])
->sendTextMsg('...', [...]);
$at = 'all';
$at = ['all'];
$at = function(Channel $channel){
if($channel->getName() == 'wecome') {
return ['mitoop'];
}
if($channel->getName() == 'dingding') {
return ['13888888888'];
}
};
$robot->group(['feishu.kefu', 'wecom.jishu'])
->sendTextMsg('...', [...], $at); // 这里的 $at 将会覆盖 group 配置中的 `at`
[
'feishu.jishu' => [
'status' => 'success', // 成功
'result' => [...] // 平台返回值
],
'wecom.jishu' => [
'status' => 'failure', // 失败
'exception_msg' => '', // 异常信息
'exception_file' => '', // 异常文件以及行数
'response' => '', // 平台返回的信息
],
//...
]
$config = [
...
'channels' => [
...
'my-channel' => [
'jishu' => [
'webhook' => ''
],
]
]
];
$robot = new Robot($config);
// 注册
$robot->extend('my-channel', function($channelConfig){
// $channelConfig 来自配置文件里的 `channels.my-channel`
return new MyChannel($channelConfig);
});
// 调用
$robot->group('my-channel.jishu')
->sendTextMsg('自定义通道', [
'msg' => 'success'
]);