phalapi / ding-com-bot
PhalApi 2.x 扩展类库:钉钉企业机器人
dev-master
2020-04-03 13:26 UTC
Requires
- php: >=7.0
- phalapi/dingbot: dev-master
This package is not auto-updated.
Last update: 2021-11-13 21:13:30 UTC
README
安装
需要使用curl
在composer.json里面添加
"require": {
"phalapi/ding-com-bot":"dev-master",
},
控制台执行 composer update
配置
//./path/to/app.php
return array(
// 钉钉企业内部webhook机器人
// @link https://ding-doc.dingtalk.com/doc#/serverapi2/elzz1p
'DingBot' => array(
'AppSecret'=>'<your AppSecret>',
'webHook'=>'<your dingbot webHook>'
),
);
注册
//./path/to/di.php
//钉钉机器人
$di->ddBot = new PhalApi\DingBot\Lite($di->config->get('app.DingBot'));
接收消息
测试
<?php
//path/to/public
require_once dirname(__FILE__) . '/init.php';
$server = new PhalApi\DingComBot\Lite($di->config->get('app.DingBot'));
//默认使用内置的消息处理 \PhalApi\DingComBot\Server
$server->Server();
上述操作完成后,正常状态下@机器人发消息 将会受到回复。
实现自身业务
添加自定义类
<?php
/**
* Created by PhpStorm.
* User: Chen Kaihang <cmxds@hotmail.com>
* Date: 2020/4/3
* Time: 19:48
*/
namespace App\DingBot;
use PhalApi\DingComBot\Server;
/**
* Class DingBotServer
* @package App\DingBot
*继承PhalApi\DingComBot\Server 重写其中的方法即可
*/
class DingBotServer extends Server
{
public function text($msg)
{
// do something
}
}
注册自身业务
<?php
//path/to/public
require_once dirname(__FILE__) . '/init.php';
$server = new PhalApi\DingComBot\Lite($di->config->get('app.DingBot'));
//默认使用内置的消息处理 \PhalApi\DingComBot\Server
$server->Server('\\App\\DingBot\\DingBotServer');
发送消息
Text类型消息
\PhalApi\DI()->ddBot->Text('测试');
Link类型消息
\PhalApi\DI()->ddBot->Link('链接','标题','https://www.dingtalk.com');
Markdown类型消息
\PhalApi\DI()->ddBot->Markdown('天气',"#### 杭州天气 @156xxxx8827\n");
ActionCard类型消息,整体跳转ActionCard
\PhalApi\DI()->ddBot
->ActionCard('标题','内容','点击标题','https://www.dingtalk.com');
ActionDialog类型消息,独立跳转ActionCard
\PhalApi\DI()->ddBot->ActionDialog('标题','内容',array(
array('title'=>'标题1','actionURL'=>'https://www.dingtalk.com/'),
array('title'=>'标题2','actionURL'=>'https://www.dingtalk.com/')
)));
FeedCard类型消息
\PhalApi\DI()->ddBot->FeedCard(array(
array('title'=>'标题1',
'messageURL'=>'https://www.dingtalk.com/',
'picURL'=>'https://www.dingtalk.com/'),
array('title'=>'标题2',
'messageURL'=>'https://www.dingtalk.com/',
'picURL'=>'https://www.dingtalk.com/')
)));
更多详细内容参考钉钉官方文档:https://ding-doc.dingtalk.com/doc#/serverapi2/krgddi