-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Announce.php
251 lines (206 loc) · 6.75 KB
/
Announce.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
declare(strict_types=1);
/**
* Announce
*
* Simple unified announce multi-client for site events.
* The point is to have, e.g., Announce::slack("foo") in one place.
* Better yet, Announce:all("foo") that hits IRC, RSS, Slack, and Twitter.
*/
class Announce
{
# IRC bot config options
private static $ircChannels = ["announce", "debug"];
private static $ircAddress = "10.0.0.6";
private static $ircPort = 51010;
# RSS bot config options
private static $foo = "foo";
# slack bot config options
private static $slackChannels = ["announce", "debug"];
# twitter bot config options
private static $bar = "bar";
/**
* all
*
* Blast all the channels with a message.
* On second thought, this needs to take an array:
*
* Announce::all([
* "irc" => "irc command",
* "twitter" => "tweet < 160 chars",
* # etc.
* ]);
*
* @param string $message Please no gay.pl stuff. :(
*/
public static function all(string $message)
{
# escape it (shouldn't be UGC anyway)
$message = \Gazelle\Text::esc($message);
/*
# send it places
self::irc($message);
self::rss($message);
self::slack($message);
self::twitter($message);
*/
}
/**
* irc
*
* Send a message to an IRC bot listening on $ENV->SOCKET_LISTEN_PORT.
*
* @param string $message An IRC protocol snippet to send.
* @param array $channels What channels you wanna blast.
*/
public static function irc(string $message, array $channels = [])
{
$app = \Gazelle\App::go();
# check if IRC is enabled
if (!$app->env->announceIrc) {
return false;
}
# set default channels
if (empty($channels)) {
$channels = self::$ircChannels;
}
# strip leading #channel hashes
$strippedHashes = [];
foreach ($channels as $channel) {
array_push($strippedHashes, preg_replace("/^#/", "", $channel));
}
# specific to AB's kana bot
# https://github.com/anniemaybytes/kana
$command = implode("-", $strippedHashes)
. "|%|"
. html_entity_decode($message, ENT_QUOTES);
# original input sanitization
$command = str_replace(["\n", "\r"], "", $command);
try {
# send the raw echo
$socket = fsockopen(self::$ircAddress, self::$ircPort);
fwrite($socket, $command);
fclose($socket);
} catch (Throwable $e) {
\Gazelle\Text::figlet("irc failure", "red");
!d($e->getMessage());
}
}
/**
* rss
*
* Make an RSS feed entry.
*/
public static function rss(string $message)
{
$app = \Gazelle\App::go();
# check if RSS is enabled
if (!$app->env->announceRss) {
return false;
}
try {
# todo
} catch (Throwable $e) {
\Gazelle\Text::figlet("rss failure", "red");
!d($e->getMessage());
}
}
/**
* slack
*
* @see https://github.com/jolicode/slack-php-api/blob/main/docs/examples/posting-message.php
*/
public static function slack(string $message, array $channels = [])
{
$app = \Gazelle\App::go();
# check if slack is enabled
if (!$app->env->announceSlack) {
return false;
}
# set default channels
if (empty($channels)) {
$channels = self::$slackChannels;
}
# webhooks must remain private
$webhooks = $app->env->private("slackWebhooks");
foreach ($channels as $channel) {
try {
# set up
$curl = curl_init($webhooks->$channel);
$data = json_encode(["text" => $message], JSON_UNESCAPED_SLASHES);
# options
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type: application/vnd.api+json"]);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
# do it
curl_exec($curl);
curl_close($curl);
} catch (TypeError $e) {
\Gazelle\Text::figlet("slack failure", "red");
!d($e->getMessage());
}
}
}
/**
* twitter
*
* @see https://twitteroauth.com
*/
public static function twitter(string $message)
{
$app = \Gazelle\App::go();
# check if twitter is enabled
if (!$app->env->enableTwitter) {
return false;
}
try {
$twitterCredentials = $app->env->private("twitterApi");
$connection = new Abraham\TwitterOAuth\TwitterOAuth(
$twitterCredentials->consumerKey,
$twitterCredentials->consumerSecret,
$twitterCredentials->accessToken,
$twitterCredentials->accessTokenSecret
);
# set api version
# $connection->setApiVersion("2");
$content = $connection->get("account/verify_credentials");
!d($content);
$statues = $connection->post("statuses/update", ["status" => "hello world"]);
!d($statues);
return;
# https://twitteroauth.com/redirect.php
define("OAUTH_CALLBACK", "fuck");
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
$url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
return $statues = $connection->post("statuses/update", ["status" => "hello world"]);
return $content = $connection->get("account/verify_credentials");
} catch (Throwable $e) {
\Gazelle\Text::figlet("twitter failure", "red");
!d($e->getMessage());
}
}
/**
* mastodon
*/
public static function mastodon(string $message)
{
$app = \Gazelle\App::go();
# check if mastodon is enabled
if (!$app->env->enableMastodon) {
return false;
}
try {
# mastodon api announce bot
$mastodon = new Mastodon(
"https://botsin.space",
"your_access_token"
);
$mastodon->postStatus($message);
} catch (Throwable $e) {
\Gazelle\Text::figlet("mastodon failure", "red");
!d($e->getMessage());
}
}
} # class