/
script.php
30 lines (23 loc) · 874 Bytes
/
script.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
<?php
use Symfony\Component\DomCrawler\Crawler;
use GuzzleHttp\RequestOptions;
require __DIR__ . '/app/bootstrap.php';
// Choices one URL randomly
$url = $container['parameters']['urls'][array_rand($container['parameters']['urls'])];
// Calls the URL
$httpClient = $container['http.client'];
$response = $httpClient->request('GET', $url);
// Retrieves the image URL from META tag
$data = (new Crawler((string) $response->getBody()))
->filterXpath('//meta[@property="og:image"]')
->extract(['content']);
// Calls Slack API
if (!empty($data)) {
$httpClient->request('POST', 'https://slack.com/api/chat.postMessage', [
RequestOptions::FORM_PARAMS => [
'token' => $container['parameters']['slack']['bot']['token'],
'channel' => $container['parameters']['slack']['channel'],
'text' => $data[0],
],
]);
}