mirror of
https://github.com/Einundzwanzig-Podcast/einundzwanzig-portal.git
synced 2025-12-11 06:46:47 +00:00
twitter bot added
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Traits\TwitterTrait;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|||||||
83
app/Observers/CourseEventObserver.php
Normal file
83
app/Observers/CourseEventObserver.php
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Observers;
|
||||||
|
|
||||||
|
use App\Models\CourseEvent;
|
||||||
|
use App\Traits\TwitterTrait;
|
||||||
|
|
||||||
|
class CourseEventObserver
|
||||||
|
{
|
||||||
|
use TwitterTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the CourseEvent "created" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\CourseEvent $courseEvent
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function created(CourseEvent $courseEvent)
|
||||||
|
{
|
||||||
|
if (config('feeds.services.twitterAccountId')) {
|
||||||
|
$this->setNewAccessToken(1);
|
||||||
|
|
||||||
|
$text = sprintf("Unser Dozent %s hat einen neuen Kurs-Termin eingestellt:\n\n%s\n\n%s\n\n%s\n\n#Bitcoin #Kurs #Education #Einundzwanzig",
|
||||||
|
$courseEvent->course->lecturer->name,
|
||||||
|
$courseEvent->course->name,
|
||||||
|
str($courseEvent->course->description)->limit(100),
|
||||||
|
url()->route('school.landingPage.lecturer',
|
||||||
|
['country' => 'de', 'lecturer' => $courseEvent->course->lecturer]),
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->postTweet($text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the CourseEvent "updated" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\CourseEvent $courseEvent
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function updated(CourseEvent $courseEvent)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the CourseEvent "deleted" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\CourseEvent $courseEvent
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function deleted(CourseEvent $courseEvent)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the CourseEvent "restored" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\CourseEvent $courseEvent
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function restored(CourseEvent $courseEvent)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the CourseEvent "force deleted" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\CourseEvent $courseEvent
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function forceDeleted(CourseEvent $courseEvent)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
83
app/Observers/CourseObserver.php
Normal file
83
app/Observers/CourseObserver.php
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Observers;
|
||||||
|
|
||||||
|
use App\Models\Course;
|
||||||
|
use App\Traits\TwitterTrait;
|
||||||
|
|
||||||
|
class CourseObserver
|
||||||
|
{
|
||||||
|
use TwitterTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the Course "created" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Course $course
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function created(Course $course)
|
||||||
|
{
|
||||||
|
if (config('feeds.services.twitterAccountId')) {
|
||||||
|
$this->setNewAccessToken(1);
|
||||||
|
|
||||||
|
$text = sprintf("Unser Dozent %s hat einen neuen Kurs eingestellt:\n\n%s\n\n%s\n\n%s\n\n#Bitcoin #Kurs #Education #Einundzwanzig",
|
||||||
|
$course->lecturer->name,
|
||||||
|
$course->name,
|
||||||
|
str($course->description)->limit(100),
|
||||||
|
url()->route('school.landingPage.lecturer',
|
||||||
|
['country' => 'de', 'lecturer' => $course->lecturer]),
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->postTweet($text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the Course "updated" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Course $course
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function updated(Course $course)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the Course "deleted" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Course $course
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function deleted(Course $course)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the Course "restored" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Course $course
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function restored(Course $course)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the Course "force deleted" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Course $course
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function forceDeleted(Course $course)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
80
app/Observers/MeetupObserver.php
Normal file
80
app/Observers/MeetupObserver.php
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Observers;
|
||||||
|
|
||||||
|
use App\Models\Meetup;
|
||||||
|
use App\Traits\TwitterTrait;
|
||||||
|
|
||||||
|
class MeetupObserver
|
||||||
|
{
|
||||||
|
use TwitterTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the Meetup "created" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Meetup $meetup
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function created(Meetup $meetup)
|
||||||
|
{
|
||||||
|
if (config('feeds.services.twitterAccountId')) {
|
||||||
|
$this->setNewAccessToken(1);
|
||||||
|
|
||||||
|
$text = sprintf("Eine neue Meetup Gruppe wurde hinzugefügt:\n\n%s\n\n%s\n\n#Bitcoin #Meetup #Einundzwanzig",
|
||||||
|
$meetup->name,
|
||||||
|
url()->route('meetup.landing', ['country' => 'de', 'meetup' => $meetup])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->postTweet($text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the Meetup "updated" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Meetup $meetup
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function updated(Meetup $meetup)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the Meetup "deleted" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Meetup $meetup
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function deleted(Meetup $meetup)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the Meetup "restored" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Meetup $meetup
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function restored(Meetup $meetup)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the Meetup "force deleted" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Meetup $meetup
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function forceDeleted(Meetup $meetup)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
81
app/Observers/OrangePillObserver.php
Normal file
81
app/Observers/OrangePillObserver.php
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Observers;
|
||||||
|
|
||||||
|
use App\Models\OrangePill;
|
||||||
|
use App\Traits\TwitterTrait;
|
||||||
|
|
||||||
|
class OrangePillObserver
|
||||||
|
{
|
||||||
|
use TwitterTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the OrangePill "created" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\OrangePill $orangePill
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function created(OrangePill $orangePill)
|
||||||
|
{
|
||||||
|
if (config('feeds.services.twitterAccountId')) {
|
||||||
|
$this->setNewAccessToken(1);
|
||||||
|
|
||||||
|
$text = sprintf("In einem der weltweiten Bücherschränke wurde ein Bitcoin-Buch reingestellt:\n\n%s\n\n%s\n\n%s\n\n#Bitcoin #Education #Einundzwanzig",
|
||||||
|
$orangePill->bookCase->title,
|
||||||
|
$orangePill->bookCase->address,
|
||||||
|
url()->route('bookCases.comment.bookcase', ['country' => 'de', 'bookCase' => $orangePill->bookCase]),
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->postTweet($text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the OrangePill "updated" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\OrangePill $orangePill
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function updated(OrangePill $orangePill)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the OrangePill "deleted" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\OrangePill $orangePill
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function deleted(OrangePill $orangePill)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the OrangePill "restored" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\OrangePill $orangePill
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function restored(OrangePill $orangePill)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the OrangePill "force deleted" event.
|
||||||
|
*
|
||||||
|
* @param \App\Models\OrangePill $orangePill
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function forceDeleted(OrangePill $orangePill)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,14 @@
|
|||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
use App\Listeners\AddLoginReputation;
|
use App\Listeners\AddLoginReputation;
|
||||||
use App\Observers\EpisodeObserver;
|
use App\Models\Course;
|
||||||
|
use App\Models\CourseEvent;
|
||||||
|
use App\Models\Meetup;
|
||||||
|
use App\Models\OrangePill;
|
||||||
|
use App\Observers\CourseEventObserver;
|
||||||
|
use App\Observers\CourseObserver;
|
||||||
|
use App\Observers\MeetupObserver;
|
||||||
|
use App\Observers\OrangePillObserver;
|
||||||
use Illuminate\Auth\Events\Login;
|
use Illuminate\Auth\Events\Login;
|
||||||
use Illuminate\Auth\Events\Registered;
|
use Illuminate\Auth\Events\Registered;
|
||||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||||
@@ -30,7 +37,10 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
//
|
Meetup::observe(MeetupObserver::class);
|
||||||
|
OrangePill::observe(OrangePillObserver::class);
|
||||||
|
CourseEvent::observe(CourseEventObserver::class);
|
||||||
|
Course::observe(CourseObserver::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
41
app/Traits/TwitterTrait.php
Normal file
41
app/Traits/TwitterTrait.php
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Traits;
|
||||||
|
|
||||||
|
use App\Models\TwitterAccount;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
|
||||||
|
trait TwitterTrait
|
||||||
|
{
|
||||||
|
public function setNewAccessToken($accountId)
|
||||||
|
{
|
||||||
|
$twitterAccount = TwitterAccount::find($accountId);
|
||||||
|
|
||||||
|
$response = Http::acceptJson()
|
||||||
|
->post('https://api.twitter.com/2/oauth2/token', [
|
||||||
|
'grant_type' => 'refresh_token',
|
||||||
|
'refresh_token' => $twitterAccount->refresh_token,
|
||||||
|
'client_id' => 'a0I1Nnp4YmMzZzdtRzFod1ZWT2c6MTpjaQ',
|
||||||
|
]);
|
||||||
|
$json = $response->json();
|
||||||
|
\Log::info($json);
|
||||||
|
|
||||||
|
TwitterAccount::find($accountId)
|
||||||
|
->update([
|
||||||
|
'token' => $json['access_token'],
|
||||||
|
'refresh_token' => $json['refresh_token'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postTweet($text)
|
||||||
|
{
|
||||||
|
$twitterAccount = TwitterAccount::find(1);
|
||||||
|
$response = Http::acceptJson()
|
||||||
|
->withToken($twitterAccount->token)
|
||||||
|
->post('https://api.twitter.com/2/tweets', [
|
||||||
|
'text' => $text,
|
||||||
|
]);
|
||||||
|
|
||||||
|
\Log::info($response->json());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
'twitterAccountId' => env('TWITTER_ACCOUNT_ID', null),
|
||||||
|
|
||||||
'podcastindex-org' => [
|
'podcastindex-org' => [
|
||||||
'key' => env('PODCASTINDEX_ORG_KEY'),
|
'key' => env('PODCASTINDEX_ORG_KEY'),
|
||||||
'secret' => env('PODCASTINDEX_ORG_SECRET'),
|
'secret' => env('PODCASTINDEX_ORG_SECRET'),
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration {
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('twitter_accounts', function (Blueprint $table) {
|
||||||
|
$table->text('token')
|
||||||
|
->nullable()
|
||||||
|
->change();
|
||||||
|
$table->text('refresh_token')
|
||||||
|
->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('twitter_accounts', function (Blueprint $table) {
|
||||||
|
//
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -633,5 +633,6 @@
|
|||||||
"You get a point when you log in.": "",
|
"You get a point when you log in.": "",
|
||||||
"has": "",
|
"has": "",
|
||||||
"logins": "",
|
"logins": "",
|
||||||
"points": ""
|
"points": "",
|
||||||
|
"New meetup created: :title": "New meetup created: :title"
|
||||||
}
|
}
|
||||||
@@ -14,6 +14,7 @@ Route::get('/auth/twitter', function () {
|
|||||||
return Socialite::driver('twitter')
|
return Socialite::driver('twitter')
|
||||||
->scopes([
|
->scopes([
|
||||||
'tweet.write',
|
'tweet.write',
|
||||||
|
'offline.access',
|
||||||
])
|
])
|
||||||
->redirect();
|
->redirect();
|
||||||
})
|
})
|
||||||
@@ -26,6 +27,7 @@ Route::get('/auth/twitter/callback', function () {
|
|||||||
'twitter_id' => $twitterUser->id,
|
'twitter_id' => $twitterUser->id,
|
||||||
], [
|
], [
|
||||||
'twitter_id' => $twitterUser->id,
|
'twitter_id' => $twitterUser->id,
|
||||||
|
'refresh_token' => $twitterUser->refreshToken,
|
||||||
'nickname' => $twitterUser->nickname,
|
'nickname' => $twitterUser->nickname,
|
||||||
'token' => $twitterUser->token,
|
'token' => $twitterUser->token,
|
||||||
'expires_in' => $twitterUser->expiresIn,
|
'expires_in' => $twitterUser->expiresIn,
|
||||||
|
|||||||
Reference in New Issue
Block a user