Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Introduce "Automations" #48

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ To add a bot you need to link your app with the bot twitch account.

1. Open `${APP_URL}/connection/bot/redirect` with your laravel-app.
2. Login into your Twitch-Bot account with your Twitch Application.
3. After redirect you need to manually connect your laravel-Account with a bot.
3. After redirect, you need to manually connect your laravel-Account with a bot.
4. Open Your Database table "bot_connections" and connect your bot with your user.
5. Restart the Bot Artisan Bot

Expand Down
14 changes: 12 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"homepage": "https://github.com/redbeed/openoverlay",
"keywords": ["Laravel", "OpenOverlay", "twitch", "Eventsub", "Bot", "IRC"],
"require": {
"php": "^8.0",
"illuminate/support": "~8|~9",
"guzzlehttp/guzzle": "^7.2",
"ext-json": "*",
Expand All @@ -23,9 +24,13 @@
"require-dev": {
"phpunit/phpunit": "~9.0",
"orchestra/testbench": "~5|~6",
"nunomaduro/phpinsights": "^1.14"
"nunomaduro/phpinsights": "^1.14",
"laravel/pint": "^0.2.3"
},
"autoload": {
"files": [
"src/Support/helpers.php"
],
"psr-4": {
"Redbeed\\OpenOverlay\\": "src/",
"Redbeed\\OpenOverlay\\Database\\": "database/"
Expand All @@ -47,5 +52,10 @@
}
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
24 changes: 6 additions & 18 deletions config/openoverlay.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
* You can use :username, :twitchUrl and :gameName for your message.
*/
\Redbeed\OpenOverlay\Listeners\AutoShoutOutRaid::class => [
'message' => 'Follow :username over at :twitchUrl. They were last playing :gameName'
'message' => 'Follow :username over at :twitchUrl. They were last playing :gameName',
],

\Redbeed\OpenOverlay\Support\ViewerInChat::class => [
'reset' => -1
]
'reset' => -1,
],
],

'webhook' => [
Expand All @@ -55,7 +55,7 @@

/**
* Your personal and unique secret is used to validate a twitch callback
* If you change your secret all previous configures webhook callbacks will be end as invalid
* If you change your secret all previous configures webhook callbacks will be ended as invalid
*/
'secret' => env('OVERLAY_SECRET'),

Expand All @@ -72,20 +72,8 @@
],

'bot' => [
'commands' => [

'simple' => [
'!hello' => 'Hello %username%! How are you doing?',
],

'advanced' => [
\Redbeed\OpenOverlay\ChatBot\Commands\HelloWorldBotCommand::class,
\Redbeed\OpenOverlay\ChatBot\Commands\ShoutOutBotCommand::class,
]
],

'schedules' => [
\Redbeed\OpenOverlay\Console\Scheduling\MadeWithChatBotScheduling::class,
]
]
],
],
];
3 changes: 1 addition & 2 deletions database/Factories/EventSubEventsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

class EventSubEventsFactory extends Factory
{

protected $model = EventSubEvents::class;

/**
* @inheritDoc
* {@inheritDoc}
*/
public function definition()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class CreateUserBotsEnabledTable extends Migration
public function up()
{
Schema::create('users_bots_enabled', function (Blueprint $table) {

$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('bot_id');
$table->timestamps();
Expand Down
6 changes: 0 additions & 6 deletions routes/openoverlay.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Illuminate\Support\Facades\Route;

use Redbeed\OpenOverlay\Http\Controllers\Api\Connection\WebhookController;
use Redbeed\OpenOverlay\Http\Controllers\Connection\AppTokenController;
use Redbeed\OpenOverlay\Http\Controllers\Connection\AuthController;
Expand All @@ -11,9 +10,7 @@

// prefix: /connection
Route::prefix('connection')->group(function () {

Route::middleware(['web', 'auth'])->group(function () {

Route::get('/redirect')->uses([AuthController::class, 'redirect'])
->name('connection.redirect');

Expand All @@ -27,7 +24,6 @@

Route::get('/callback')->uses([AppTokenController::class, 'handleProviderCallback'])
->name('connection.app-token.callback');

});

// prefix: /connection/bot
Expand All @@ -37,14 +33,12 @@

Route::get('/callback')->uses([BotAuthController::class, 'handleProviderCallback'])
->name('connection.bot.callback');

});
});

Route::middleware(['api'])->group(function () {
Route::any('/webhook')->uses([WebhookController::class, 'handleProviderCallback'])
->name('connection.webhook');
});

});
});
4 changes: 2 additions & 2 deletions src/Actions/RegisterUserTwitchWebhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function registerAll(Connection $connection, bool $clearBeforeRegi

public function clearBroadcasterSubscriptions()
{
$this->apiClient->deleteSubByBroadcasterId((string)$this->connection->service_user_id);
$this->apiClient->deleteSubByBroadcasterId((string) $this->connection->service_user_id);
}

public function register(string $type): bool
Expand All @@ -61,7 +61,7 @@ public function register(string $type): bool

private function registerCondition($type): array
{
$broadcasterId = (string)$this->connection->service_user_id;
$broadcasterId = (string) $this->connection->service_user_id;

if ($type === 'channel.raid') {
return ['to_broadcaster_user_id' => $broadcasterId];
Expand Down
35 changes: 35 additions & 0 deletions src/Automations/Actions/TwitchChatBotMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Redbeed\OpenOverlay\Automations\Actions;

use Illuminate\Support\Facades\Artisan;
use Redbeed\OpenOverlay\Console\Commands\ChatBot\SendMessageCommand;
use Redbeed\OpenOverlay\Models\User\Connection;

class TwitchChatBotMessage
{
use UseVariables;
use UseTwitchChatMessage;

public static string $name = 'Send chat message';

public static string $description = 'Send chat message via bot';

private Connection $connection;

private string $message;

public function __construct(string $message)
{
$this->message = $message;
}

public function handle()
{
Artisan::queue(SendMessageCommand::class, [
'userId' => $this->getUser()->id,
'--botId' => $this->getBot()->id,
'message' => $this->replaceInString($this->message),
]);
}
}
37 changes: 37 additions & 0 deletions src/Automations/Actions/TwitchRandomChatBotMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Redbeed\OpenOverlay\Automations\Actions;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;
use Redbeed\OpenOverlay\Console\Commands\ChatBot\SendMessageCommand;
use Redbeed\OpenOverlay\Models\User\Connection;

class TwitchRandomChatBotMessage
{
use UseVariables;
use UseTwitchChatMessage;

public static string $name = 'Send one of the random chat messages';

public static string $description = 'Send one of the random chat messages via bot';

private Connection $connection;

/** @var string[] */
private array $messages;

public function __construct(array $messages)
{
$this->messages = $messages;
}

public function handle()
{
Artisan::queue(SendMessageCommand::class, [
'userId' => $this->getUser()->id,
'--botId' => $this->getBot()->id,
'message' => $this->replaceInString(Arr::random($this->messages)),
]);
}
}
41 changes: 41 additions & 0 deletions src/Automations/Actions/UseTwitchChatMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Redbeed\OpenOverlay\Automations\Actions;

use Illuminate\Foundation\Auth\User;
use Redbeed\OpenOverlay\ChatBot\Twitch\ChatMessage;
use Redbeed\OpenOverlay\Models\BotConnection;

trait UseTwitchChatMessage
{
private ChatMessage $chatMessage;

private ?BotConnection $botConnection = null;

private ?User $user = null;

public function setChatMessage(ChatMessage $chatMessage): void
{
$this->chatMessage = $chatMessage;
}

public function setBotConnection(BotConnection $botConnection): void
{
$this->botConnection = $botConnection;
}

public function setUser(User $user): void
{
$this->user = $user;
}

protected function getBot(): ?BotConnection
{
return $this->botConnection ?: $this->chatMessage?->bot;
}

protected function getUser(): ?User
{
return $this->user ?: $this->chatMessage?->channelUser;
}
}
60 changes: 60 additions & 0 deletions src/Automations/Actions/UseVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Redbeed\OpenOverlay\Automations\Actions;

use Illuminate\Support\Str;

trait UseVariables
{
private array $variables = [];

public function addVariables(array $variables): void
{
$this->variables = array_merge_recursive($this->variables, $variables);
}

public function getVariables($filterName = ''): array
{
if ($filterName) {
return $this->filterVariables($filterName);
}

return $this->variables;
}

private function filterVariables(string $filterName): array
{
$filtered = [];
foreach ($this->variables as $name => $value) {
if (str_contains($name, $filterName)) {
$filtered[$name] = $value;
}
}

return $filtered;
}

protected function replaceInString(string $string): string
{
return strtr($string, $this->makeReplacements($string));
}

protected function makeReplacements(string $string): array
{
$replacements = [];
foreach ($this->variables as $key => $value) {
$keyPattern = ':'.$key;

if (Str::contains($string, $keyPattern)) {
if ($this->variables[$key] instanceof \Closure) {
// If the variable is a closure, we will execute it and replace the key with the result.
$this->variables[$key] = $this->variables[$key]();
}

$replacements[$keyPattern] = $this->variables[$key];
}
}

return $replacements;
}
}
Loading