tatarko / yii-slack
使用 Guzzle 接口的 Yii 扩展,用于访问 Slack API
v0.4.1
2015-04-28 14:29 UTC
Requires
- guzzlehttp/guzzle: ~5.0
- yiisoft/yii: ~1.1
This package is not auto-updated.
Last update: 2024-09-28 16:57:05 UTC
README
Yii 扩展,通过 Guzzle 在 Yii 框架中访问 Slack API。
安装
Yii Slack 是 composer 库,您可以使用以下命令安装最新版本:
php composer.phar require tatarko/yii-slack
配置
将以下内容添加到应用程序的配置文件中:
'components' => array( 'slack' => array( 'class' => 'Tatarko\\YiiSlack\\ApplicationComponent', 'appId' => '', // Your's application ID 'appSecret' => '', // Your's application secret code 'tokenStateName' => 'slack.access.token'; // optional - change name of the user's state variable to store access token in 'companyToken' => '', // optional - set global access token of your company's account to use slack component without user authentication ), )
对于 OAuth 认证,将以下方法添加到控制器中:
class SiteController extends Controller { public function actions() { return array( 'slack' => array( 'class' => 'Tatarko\\YiiSlack\\AuthenticationAction', 'onAuthSuccess' => function(CEvent $event) { // you can get $event->params->access_token and store it in some persistant database instead of user's states (that is basically sessions variable) $this->redirect('welcome'); }, 'onAuthError' => function(CEvent $event) { // $event->params is instance of Exception (CException or GuzzleHttp\Exception\TransferException) $this->redirect('login'); }, ), ); } }
用法
对于简单的 OAuth,只需在任意视图文件中创建链接:
<a href="<?= $this->createUrl('site/slack') ?>">Login with Slack</a>
之后,您可以通过调用以下代码来检查当前网络用户是否已通过 Slack 登录:
var_dump(Yii::app()->slack->isAuthenticated); // boolean
如果用户确实已通过 Slack 认证,您可以进行如下 API 调用:
var_dump(Yii::app()->slack->get('auth.test'));
这将打印类似以下内容:
array(6) { 'ok' => bool(true) 'url' => string(25) "https://myteam.slack.com/" 'team' => string(7) "My Team" 'user' => string(3) "cal" 'team_id' => string(6) "T12345" 'user_id' => string(6) "U12345" }
对于额外的参数,请使用
Yii::app()->slack->post('channels.create', array('name' => 'mychannel'));
有关所有可用方法和其参数的完整列表,请访问官方 Slack 文档。