PHP

php 슬랙 slack 메세지 연동

은둔한량 2020. 4. 8. 16:33
반응형

php 슬랙 slack 메세지 연동

	public function _slackPostMessage( $strChannel='#channel', $strMessageText='message', $strAttachments='', $strUserName='Bot' ) {

		/*
        $attachments = '[{
			"title": "테스트 내용 -----------------------------------------------------------------",
			"fields": [
				{"value": "테스트1","short": true},{"value": "test1","short": true},
				{"value": "테스트2","short": true},{"value": "test2","short": true},
				{"value": "테스트3","short": true},{"value": "test3","short": true},
				{"value": "테스트4","short": true},{"value": "test4","short": true}												
			],
			"color": "#ff1200" // line color
		}]';

		$this->_slackPostMessage( '#channel', 'Slak Send Test 내용 추가 ----------------', $attachments);
		*/

		$strSlackHost = 'https://slack.com/api/chat.postMessage';
		$strSlackToken = '슬랙에서 받은 토큰';
		$params = array('token' => $strSlackToken, 
                        'text' => $strMessageText, 
                        'channel' => $strChannel, 
                        'username' => $strUserName );
                        
		if($strAttachments) $params['attachments'] = $strAttachments;
		
		//$this->load->library('curl'); // CI용
		//$this->curl->simple_post($strSlackHost, $params);
		
		$ch = curl_init();
		curl_setopt( $ch, CURLOPT_URL, $strSlackHost );
		curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt( $ch, CURLOPT_SSLVERSION, 1 );
		curl_setopt( $ch, CURLOPT_POST, 1 );
		curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
		curl_setopt( $ch, CURLOPT_TIMEOUT, 300 );
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
		$result[0] = curl_exec( $ch );
		$result[1] = curl_errno( $ch );
		$result[2] = curl_error( $ch );
		$result[3] = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
		curl_close( $ch );

		return true;

	}
반응형