Receiving messages from matrix
This commit is contained in:
60
app/Events/Matrix/Base.php
Normal file
60
app/Events/Matrix/Base.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events\Matrix;
|
||||
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use App\Models\Echoarea;
|
||||
|
||||
abstract class Base
|
||||
{
|
||||
protected $_data = [];
|
||||
|
||||
public function __construct(array $request)
|
||||
{
|
||||
Log::info(sprintf('EMb:- Event Initialised [%s]',get_class($this)));
|
||||
|
||||
$this->_data = json_decode(json_encode($request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable getting values for keys in the response
|
||||
*
|
||||
* @note: This method is limited to certain values to ensure integrity reasons
|
||||
* @note: Classes should return:
|
||||
* + channel_id,
|
||||
* + team_id,
|
||||
* + ts,
|
||||
* + user_id
|
||||
* @param string $key
|
||||
* @return mixed|object
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
public function __get(string $key)
|
||||
{
|
||||
switch ($key) {
|
||||
case 'echoarea':
|
||||
$rooms = collect(config('matrix.rooms'));
|
||||
|
||||
return Echoarea::where('name',$rooms->get($this->room_id))->single();
|
||||
|
||||
case 'room':
|
||||
$room_alias = Http::withToken(config('matrix.as_token'))
|
||||
->get(sprintf('%s/_matrix/client/v3/rooms/%s/state/m.room.canonical_alias',config('matrix.server'),$this->room_id));
|
||||
|
||||
return $room_alias->json('alias',$this->room_id);
|
||||
|
||||
case 'topic':
|
||||
$subject = Http::withToken(config('matrix.as_token'))
|
||||
->get(sprintf('%s/_matrix/client/v3/rooms/%s/state/m.room.topic',config('matrix.server'),$this->room_id));
|
||||
|
||||
return $subject->json('topic','Message from Matrix');
|
||||
|
||||
case 'room_id':
|
||||
default:
|
||||
return object_get($this->_data,$key);
|
||||
}
|
||||
}
|
||||
}
|
50
app/Events/Matrix/Factory.php
Normal file
50
app/Events/Matrix/Factory.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events\Matrix;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Factory {
|
||||
private const LOGKEY = 'EMf';
|
||||
|
||||
/**
|
||||
* @var array event type to event class mapping
|
||||
*/
|
||||
public const map = [
|
||||
'm.room.message' => Message::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* Returns new event instance
|
||||
*
|
||||
* @param string $type
|
||||
* @param array $request
|
||||
* @return Base
|
||||
*/
|
||||
public static function create(string $type,array $request): Base
|
||||
{
|
||||
$class = Arr::get(self::map,$type,Unknown::class);
|
||||
Log::debug(sprintf('%s:- Working out Event Class for [%s] as [%s]',static::LOGKEY,$type,$class));
|
||||
|
||||
if (App::environment() == 'local')
|
||||
file_put_contents('/tmp/event.'.$type,print_r($request,TRUE));
|
||||
|
||||
return new $class($request);
|
||||
}
|
||||
|
||||
public static function make(array $request): Base
|
||||
{
|
||||
// During the life of the event, this method is called twice - once during Middleware processing, and finally by the Controller.
|
||||
static $o = NULL;
|
||||
static $or = NULL;
|
||||
|
||||
if (! $o OR ($or != $request)) {
|
||||
$or = $request;
|
||||
$o = self::create(Arr::get($request,'type','unknown'),$request);
|
||||
}
|
||||
|
||||
return $o;
|
||||
}
|
||||
}
|
52
app/Events/Matrix/Message.php
Normal file
52
app/Events/Matrix/Message.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events\Matrix;
|
||||
|
||||
/**
|
||||
* A matrix message event
|
||||
*
|
||||
* Array
|
||||
* (
|
||||
* [age] => 37
|
||||
* [content] => Array
|
||||
* (
|
||||
* [body] => This is my text
|
||||
* [m.mentions] => Array
|
||||
* (
|
||||
* )
|
||||
*
|
||||
* [msgtype] => m.text
|
||||
* )
|
||||
*
|
||||
* [event_id] => $fkpvy3qDkAGlB55nvqcH8mUfSxzELtaJ9TKJs6GP9us
|
||||
* [origin_server_ts] => 1717917709298
|
||||
* [room_id] => !bbXofZepRYOhKjihLH:matrix.dege.au
|
||||
* [sender] => @deon:matrix.dege.au
|
||||
* [type] => m.room.message
|
||||
* [unsigned] => Array
|
||||
* (
|
||||
* [age] => 37
|
||||
* )
|
||||
*
|
||||
* [user_id] => @deon:matrix.dege.au
|
||||
* )
|
||||
*/
|
||||
class Message extends Base
|
||||
{
|
||||
public function __get($key)
|
||||
{
|
||||
switch ($key) {
|
||||
case 'message':
|
||||
return object_get($this->_data,'content.body');
|
||||
|
||||
case 'sender':
|
||||
return object_get($this->_data,$key);
|
||||
|
||||
case 'ts':
|
||||
return object_get($this->_data,'origin_server_ts');
|
||||
|
||||
default:
|
||||
return parent::__get($key);
|
||||
}
|
||||
}
|
||||
}
|
20
app/Events/Matrix/Unknown.php
Normal file
20
app/Events/Matrix/Unknown.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events\Matrix;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Catch all unknown events that we havent specifically programmed for.
|
||||
*
|
||||
* @package Slack\Event
|
||||
*/
|
||||
class Unknown extends Base
|
||||
{
|
||||
public function __construct(array $request)
|
||||
{
|
||||
Log::notice(sprintf('EMU:? UNKNOWN Event received [%s]',get_class($this)));
|
||||
|
||||
parent::__construct($request);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user