Skip to content

Commit

Permalink
twitter v2 api
Browse files Browse the repository at this point in the history
  • Loading branch information
tomahock committed Aug 22, 2023
1 parent abc4936 commit 9f287fe
Show file tree
Hide file tree
Showing 5 changed files with 3,203 additions and 2,132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use App\Models\Warning;
use App\Tools\BlueskyTool;
use App\Tools\FacebookTool;
use App\Tools\NotificationTool;
use App\Tools\TelegramTool;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function handle()
$text = "ALERTA: \r\n" . $status;
TwitterTool::tweet($text);
TelegramTool::publish($text);
BlueskyTool::publish($text);

$message = "ALERTA: %0A" . $status;
FacebookTool::publish($message);
Expand Down
55 changes: 22 additions & 33 deletions app/Tools/TwitterTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Tools;

use Illuminate\Support\Facades\Log;
use Noweh\TwitterApi\Client;

class TwitterTool
{
Expand All @@ -13,13 +14,15 @@ public static function getClient()
{
if (!self::$client) {
$settings = [
'oauth_access_token' => env('TWITTER_OAUTH_ACCESS_TOKEN'),
'oauth_access_token_secret' => env('TWITTER_OAUTH_ACCESS_TOKEN_SECRET'),
'access_token' => env('TWITTER_OAUTH_ACCESS_TOKEN'),
'access_token_secret' => env('TWITTER_OAUTH_ACCESS_TOKEN_SECRET'),
'consumer_key' => env('TWITTER_CONSUMER_KEY'),
'consumer_secret' => env('TWITTER_CONSUMER_SECRET'),
'bearer_token' => env('TWITTER_BEARER_TOKEN'),
'account_id' => 'fogospt',
];

self::$client = new \TwitterAPIExchange($settings);
self::$client = new Client($settings);
}

return self::$client;
Expand Down Expand Up @@ -118,53 +121,39 @@ public static function tweet($text, $lastId = false, $imagePath = false, $emerge
$fields = [];

if ($imagePath && file_exists($imagePath)) {
$file = file_get_contents($imagePath);
$data = base64_encode($file);

$url = 'https://upload.twitter.com/1.1/media/upload.json';
$method = 'POST';
$params = [
'media_data' => $data,
];

$imageResponse = $client
->setPostfields($params)
->buildOauth($url, $method)
->performRequest();

$imageResponse = json_decode($imageResponse);
$file_data = base64_encode(file_get_contents($imagePath));
$media_info = $client->uploadMedia()->upload($file_data);

// Extract media id
$id = $imageResponse->media_id_string;
$id = $media_info["media_id"];

$fields['media_ids'] = $id;
$fields['media']['media_ids'] = [(string)$id];
}

$url = 'https://api.twitter.com/1.1/statuses/update.json';

$tweets = self::splitTweets($text);

foreach ($tweets as $t) {
$fields['status'] = $t;
$fields['text'] = $t;

if ($lastId) {
$fields['in_reply_to_status_id'] = $lastId;
$fields['reply']['in_reply_to_tweet_id'] = $lastId;
}

$response = $client
->buildOauth($url, 'POST')
->setPostfields($fields)
->performRequest();

$r = json_decode($response);
$response = $client->tweet()->create()->performRequest(
$fields
);

if(isset($r->id)){
$lastId = $r->id;
if(isset($response->data->id)){
$lastId = $response->data->id;
} else {
$lastId = null;
}
}

if(isset($fields['media']['media_ids'])){
unset($fields['media']['media_ids']);
unset($fields['media']);
}
}
return $lastId;
}

Expand Down
Empty file added app/Tools/TwitterToolV2.php
Empty file.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"j7mbo/twitter-api-php": "^1.0",
"jenssegers/mongodb": "^3.8",
"laravel/lumen-framework": "^8.0",
"noweh/twitter-api-v2-php": "^3.4",
"php-imap/php-imap": "^5.0",
"predis/predis": "^1.1",
"sentry/sentry-laravel": "^2.7",
Expand Down
Loading

0 comments on commit 9f287fe

Please sign in to comment.