slack/src/Interactive/InteractiveMessage.php

106 lines
2.6 KiB
PHP

<?php
namespace Slack\Interactive;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Slack\Message;
/**
* Class InteractiveMessage
*
* @package Slack\Interactive
*
* [type] => interactive_message
* [actions] => Array
* (
* [0] => stdClass Object
* (
* [name] => type
* [type] => select
* [selected_options] => Array
* (
* [0] => stdClass Object
* (
* [value] => ID|1
* )
* )
* )
* )
* [callback_id] => classify|438
* [team] => stdClass Object
* (
* [id] => {SLACKTEAM}
* [domain] => {SLACKDOMAIN}
* )
* [channel] => stdClass Object
* (
* [id] => {SLACKCHANNEL}
* [name] => directmessage
* )
* [user] => stdClass Object
* (
* [id] => {SLACKUSER}
* [name] => {SLACKUSER}
* )
* [action_ts] => 1603777165.467584
* [message_ts] => 1603768794.012800
* [attachment_id] => 3
* [token] => Oow8S2EFvrZoS9z8N4nwf9Jo
* [is_app_unfurl] =>
* [original_message] => stdClass Object
* (
* ...
* )
* [response_url] => {SLACKRESPONSEURL}
* [trigger_id] => 1452241456197.39333245939.7f8618e13013ae0a0ae7d86be2258021
*/
class InteractiveMessage extends Base
{
protected const LOGKEY = 'IIM';
// Does the event respond with a reply to the HTTP request, or via a post with a trigger
public $respondNow = TRUE;
public function __get($key)
{
switch ($key) {
// An event can have more than 1 action, each action can have 1 value.
case 'action_id':
case 'name':
case 'type':
return object_get(Arr::get(object_get($this->_data,'actions'),$this->index),$key);
case 'value':
switch ($this->type) {
case 'button':
return Arr::get(object_get($this->_data,'actions'),$this->index)->value;
case 'select':
return Arr::get(object_get(Arr::get(object_get($this->_data,'actions'),$this->index),'selected_options'),0)->value;
}
break;
case 'channel_id':
return object_get($this->_data,'channel.id');
case 'message_ts':
return object_get($this->_data,$key);
default:
return parent::__get($key);
}
}
/**
* This method should be overridden by a local implementation
*
* @return Message
*/
public function respond(): Message
{
Log::info(sprintf('%s:Interactive Message - Callback [%s] Name [%s] Type [%s]',static::LOGKEY,$this->callback_id,$this->name,$this->type),['m'=>__METHOD__]);
Log::notice(sprintf('%s:Unhandled action [%s]',static::LOGKEY,$this->callback_id),['m'=>__METHOD__]);
return (new Message)->text('That didnt work, I didnt know what to do with your button - you might like to tell '.$this->team()->owner->slack_user);
}
}