Examples - shahmal1yev/blueskysdk GitHub Wiki

Create a Simple Post

Easily create and send a text-based post to BlueSky.

// Forge a new post with simple text
$post = bskyFacade($client)->post()->text("Hello, BlueSky!");

// Send the post to the server
$createdRecord = bskyFacade()->createRecord()
    ->record($post) // Include the forged post
    ->repo($client->authenticated()->did()) // Specify the authenticated user's DID
    ->collection($post->nsid()) // Use the appropriate collection namespace
    ->send();

// Output the URI of the created post
echo $createdRecord->uri();

Create a Post with an Image

Embed images into your posts for enhanced visual appeal.

use \Atproto\Lexicons\App\Bsky\Embed\Collections\ImageCollection;
use \Atproto\Lexicons\App\Bsky\Embed\Image;

// Upload an image to the server
$uploadedBlob = bskyFacade($client)->uploadBlob()
    ->blob('/path/to/image.jpg') // Specify the image path
    ->send()
    ->blob(); // Retrieve the uploaded blob metadata

// Forge a post embedding the uploaded image
$post = bskyFacade()->post()
    ->text("Hello, BlueSky with an image!")
    ->embed(bskyFacade()->imagesEmbed(
       bskyFacade()->image($uploadedBlob, 'Image description')
    ));

// Send the post and log the URI
$createdRecord = bskyFacade()->createRecord()
    ->record($post)
    ->repo($client->authenticated()->did())
    ->collection($post->nsid())
    ->send();

echo $createdRecord->uri();

Create a Post with External Links

Add external links with thumbnails for informative posts.

// Upload an image for the link preview
$uploadedBlob = bskyFacade($client)->uploadBlob()
    ->blob('/path/to/image.jpg')
    ->send()
    ->blob();

// Forge external link details
$external = bskyFacade()->externalEmbed(
    'https://example.com', // Link URL
    'Example Website',    // Link title
    'A description of the website.' // Link description
)->thumb($uploadedBlob); // Add the uploaded image as a thumbnail

// Forge a post including the external link
$post = bskyFacade()->post()
    ->text("Check out this website!")
    ->embed($external);

// Send the post and retrieve the URI
$createdRecord = bskyFacade()->createRecord()
    ->record($post)
    ->repo($client->authenticated()->did())
    ->collection($post->nsid())
    ->send();

echo $createdRecord->uri();

Search Posts

Search for posts on BlueSky using keywords or filters.

// Perform a keyword search on posts
$response = bskyFacade()->searchPosts('keyword')
    ->send();

// Loop through and display the search results
foreach ($response->posts() as $post) {
    echo $post->record()->text() . "\n";
}

GetTimeline

Get a view of the requesting account's home timeline.

$feed = bskyFacade($client)->getTimeline()
    ->limit(10)
    ->send()
    ->feed();

foreach($feed as $entry) {
    echo sprintf("Created by %s at %s" . PHP_EOL,
        $entry->post()->author()->handle(),
        $entry->post()->indexedAt()->format('d/m/Y H:i:s')
    );
}

Video Upload

Upload a video file to Bluesky and create a post with the embedded video.

use Atproto\Client;
use Atproto\DataModel\Blob\Blob;
use Atproto\Lexicons\App\Bsky\Embed\Video;
use Atproto\Responses\App\Bsky\Video\UploadVideoResponse;
use Atproto\Support\FileSupport;

// Authenticate the client with Bluesky credentials
$client = new Client();
$client->authenticate('your-handle.bsky.social', 'your-password');


// Retrieve the PDS server url from the authenticated session
$pdsUrl = null;
foreach ($client->authenticated()->didDoc()['service'] ?? [] as $service) {
    if (($service['id'] ?? null) === '#atproto_pds') {
        $pdsUrl = $service['serviceEndpoint'] ?? null;
        break;
    }
}

if (! $pdsUrl) {
    throw new LogicException("Could not resolve PDS endpoint from DID document."); // If this happens, please report it
}

// Extract the host part from the PDS endpoint
$pdsHost = str_replace(
    ["https://", "http://"],
    "did:web:",
    $pdsUrl
);

// Generate a temporary authentication token for uploading the video
$token = bskyFacade($client)->getServiceAuth()
    ->aud($pdsHost)
    ->lxm(bskyFacade()->uploadBlob()->nsid())
    ->exp(\Carbon\Carbon::now()->addMinutes(15)->timestamp)
    ->send()
    ->token();

// Define the video file path
$filePath = '/path/to/your/video.mp4';
$file = new FileSupport($filePath);

// Upload the video to Bluesky
$uploadedVideo = bskyFacade()->uploadVideo(basename($filePath), $file, $token)->send();

// Check if the upload response includes a video blob reference
$videoBlob = null;
if ($uploadedVideo->has('blob')) {
    $videoBlob = Blob::viaArray($uploadedVideo->blob());
}

// If the video blob is not immediately available, wait for job completion
while (! $videoBlob) {
    $jobStatusResponse = bskyFacade()->getJobStatus($uploadedVideo->jobId())->send();

    // If the upload job has completed, retrieve the video blob
    if ($jobStatusResponse->jobStatus()->state() === 'JOB_STATE_COMPLETED') {
        $videoBlob = Blob::viaArray($jobStatusResponse->jobStatus()->blob());
    }

    // Wait briefly before checking again
    sleep(0.5);
}

// Create a new post with the uploaded video
$post = bskyFacade()->post()
    ->text("Hello, BlueSky! This is a video.")
    ->embed(new Video($videoBlob));

// Publish the post and retrieve the created post's URI
$createdRecordRes = bskyFacade()->createRecord()
    ->repo($client->authenticated()->handle())
    ->collection($post->nsid())
    ->record($post)
    ->send();

// Construct the public URL of the posted video
$username = $client->authenticated()->handle();
$postNsid = $post->nsid();

$segments = explode("/", $createdRecordRes->uri());
$postId = end($segments);

$uri = "https://bsky.app/profile/$username/post/$postId";

// Output the post URL
echo $uri . PHP_EOL;