Receiving messages from matrix
This commit is contained in:
parent
61cfb773e2
commit
86995b03a8
@ -57,3 +57,7 @@ AWS_BUCKET=
|
||||
AWS_ENDPOINT=
|
||||
AWS_DEFAULT_REGION=home
|
||||
AWS_USE_PATH_STYLE_ENDPOINT=true
|
||||
|
||||
MATRIX_SERVER=
|
||||
MATRIX_AS_TOKEN=
|
||||
MATRIX_HS_TOKEN=
|
||||
|
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);
|
||||
}
|
||||
}
|
23
app/Http/Controllers/MatrixController.php
Normal file
23
app/Http/Controllers/MatrixController.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use App\Events\Matrix\Factory as MatrixEventFactory;
|
||||
|
||||
final class MatrixController extends Controller
|
||||
{
|
||||
private const LOGKEY = 'CMC';
|
||||
|
||||
public function webhook(Request $request)
|
||||
{
|
||||
$event = MatrixEventFactory::make(Arr::get($request->events,0,[]));
|
||||
Log::info(sprintf('%s:- Dispatching Matrix Event [%s]',static::LOGKEY,get_class($event)));
|
||||
event($event);
|
||||
|
||||
return response(['result'=>'OK']);
|
||||
}
|
||||
}
|
31
app/Listeners/Matrix/MessageListener.php
Normal file
31
app/Listeners/Matrix/MessageListener.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners\Matrix;
|
||||
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
|
||||
use App\Events\Matrix\Message;
|
||||
use App\Notifications\Echomails\MatrixMessage;
|
||||
|
||||
class MessageListener implements ShouldQueue
|
||||
{
|
||||
protected const LOGKEY = 'LMM';
|
||||
|
||||
public string $queue = 'matrix';
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param Message $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Message $event): void
|
||||
{
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:- Message Event in [%s] from [%s]',self::LOGKEY,$event->room_id,$event->sender));
|
||||
|
||||
Notification::route('echomail',$event->echoarea)->notify(new MatrixMessage($event));
|
||||
}
|
||||
}
|
59
app/Notifications/Echomails/MatrixMessage.php
Normal file
59
app/Notifications/Echomails/MatrixMessage.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Echomails;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use App\Events\Matrix\Message;
|
||||
use App\Models\Echomail;
|
||||
use App\Notifications\Echomails;
|
||||
use App\Traits\MessagePath;
|
||||
|
||||
class MatrixMessage extends Echomails
|
||||
{
|
||||
use MessagePath;
|
||||
|
||||
private const LOGKEY = 'NMM';
|
||||
|
||||
/**
|
||||
* Post a message from Matrix.
|
||||
*
|
||||
* @param Message $mo
|
||||
*/
|
||||
public function __construct(private Message $mo)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mail representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
* @return Echomail
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function toEchomail(object $notifiable): Echomail
|
||||
{
|
||||
$echoarea = $notifiable->routeNotificationFor(static::via);
|
||||
$o = $this->setupEchomail($echoarea);
|
||||
|
||||
Log::info(sprintf('%s:+ Sending Matrix Message to [%s]',self::LOGKEY,$echoarea->name));
|
||||
|
||||
$our = our_address($echoarea->domain)->last();
|
||||
$o->to = 'All';
|
||||
$o->from = $this->mo->sender;
|
||||
$o->datetime = Carbon::createFromTimestampMs($this->mo->ts);
|
||||
$o->subject = $this->mo->topic;
|
||||
$o->fftn_id = $our->id;
|
||||
$o->kludges->put('CHRS:','UTF8 2');
|
||||
|
||||
// Message
|
||||
$o->msg = $this->mo->message;
|
||||
$o->set_origin = sprintf('Matrix %s (%s)',$this->mo->room,$our->ftn4d);
|
||||
|
||||
$o->save();
|
||||
|
||||
return $o;
|
||||
}
|
||||
}
|
@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Notifications\ChannelManager;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
@ -40,5 +42,9 @@ class AppServiceProvider extends ServiceProvider
|
||||
public function boot()
|
||||
{
|
||||
static::bootSingleOrFail();
|
||||
|
||||
Auth::viaRequest('matrix-token',function (Request $request) {
|
||||
return (config('matrix.hs_token') && ($request->bearerToken() === config('matrix.hs_token'))) ? TRUE : NULL;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,13 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
|
||||
use App\Events\Matrix\Message;
|
||||
use App\Listeners\Matrix\MessageListener;
|
||||
|
||||
class EventServiceProvider extends ServiceProvider
|
||||
{
|
||||
@ -29,6 +32,9 @@ class EventServiceProvider extends ServiceProvider
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
//
|
||||
Event::listen(
|
||||
Message::class,
|
||||
MessageListener::class,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -52,6 +52,10 @@ return [
|
||||
'provider' => 'users',
|
||||
'hash' => false,
|
||||
],
|
||||
|
||||
'matrix' => [
|
||||
'driver' => 'matrix-token',
|
||||
]
|
||||
],
|
||||
|
||||
/*
|
||||
|
13
config/matrix.php
Normal file
13
config/matrix.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Matrix integration configuration
|
||||
*/
|
||||
return [
|
||||
'server' => ENV('MATRIX_SERVER'),
|
||||
'as_token' => ENV('MATRIX_AS_TOKEN'),
|
||||
'hs_token' => ENV('MATRIX_HS_TOKEN'),
|
||||
'rooms' => [
|
||||
// '!bbXofZepRYOhKjihLH:matrix.dege.au' => 'PVT_TEST',
|
||||
]
|
||||
];
|
@ -2,7 +2,7 @@
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
use App\Http\Controllers\{DomainController,SystemController,ZoneController};
|
||||
use App\Http\Controllers\{DomainController,MatrixController};
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
@ -16,3 +16,7 @@ use App\Http\Controllers\{DomainController,SystemController,ZoneController};
|
||||
*/
|
||||
|
||||
Route::get('/domain/daily',[DomainController::class,'api_daily_stats']);
|
||||
|
||||
Route::any('matrix/{item}',[MatrixController::class,'webhook'])
|
||||
->where('item', '.*')
|
||||
->middleware('auth:matrix');
|
Loading…
Reference in New Issue
Block a user