generated from chillerlan/php-library-template-nodocs
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySkeetBot.php
108 lines (88 loc) · 2.72 KB
/
MySkeetBot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* Class MySkeetBot
*
* @todo: update/change docblock
*
* @created 04.11.2024
* @author smiley <[email protected]>
* @copyright 2024 smiley
* @license MIT
*/
declare(strict_types=1);
namespace PHPSkeetBot\MySkeetBot;
use chillerlan\HTTP\Utils\MessageUtil;
use chillerlan\Settings\SettingsContainerInterface;
use chillerlan\Utilities\Arr;
use PHPSkeetBot\PHPSkeetBot\SkeetBot;
use Psr\Http\Message\ResponseInterface;
use function date;
use function explode;
use function sprintf;
/**
* My first skeet bot
*/
class MySkeetBot extends SkeetBot{
/**
* MySkeetBot constructor
*/
public function __construct(SettingsContainerInterface|MySkeetBotOptions $options){ // phpcs:ignore
parent::__construct($options);
// do some constructor stuff...
}
/**
* this method is mandated by the interface and called in the runner
*
* @link https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/feed/post.json
*/
public function post():static{
// @todo: prepare your dataset
$text = 'Hello World!';
$images = [/* one or more image blobs returned by $this->uploadMedia($mediafile) */];
// prepare the skeet JSON body
$body = [
'collection' => 'app.bsky.feed.post',
'repo' => $this->bluesky->getAccountDID(),
'record' => [
// the text property *must* be present, even if empty
'text' => $text,
'langs' => ['en'],
'createdAt' => date('c'), // ISO 8601 date
'$type' => 'app.bsky.feed.post',
// you can omit the embed part if there's no media to post
'embed' => [
'$type' => 'app.bsky.embed.images',
'images' => $images,
],
],
];
$this->submitSkeet($body);
return $this;
}
/**
* a simple implementation of the success handler, which prints the skeet URL to the console and exits the script
*/
protected function submitSkeetSuccess(ResponseInterface $response):never{
$json = MessageUtil::decodeJSON($response);
if(!isset($json->validationStatus) || $json->validationStatus !== 'valid'){
$this->logger->error('invalid status');
exit(255);
}
// my god bsky just give me the full url to the post it's not hard
$url = sprintf('https://bsky.app/profile/%s/post/%s', $this->bluesky->getHandle(), Arr::last(explode('/', $json->uri)));
$this->logger->info(sprintf('posted: %s', $url));
exit(0);
}
/**
* the failure handler prints the error message (if any) to the console and exits with a non-success status
*/
protected function submitSkeetFailure(ResponseInterface|null $response):never{
if($response instanceof ResponseInterface){
$json = MessageUtil::decodeJSON($response);
if(isset($json->message)){
$this->logger->error($json->message);
}
}
exit(255);
}
}