-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
41 lines (30 loc) · 1.17 KB
/
index.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
<?php declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
function getRandomLine($texts)
{
$probabilityArray = [];
foreach ($texts as $key => $tweet) {
$lengthFilledProbability = array_fill(0, $tweet['probability'], $key);
$probabilityArray = array_merge($probabilityArray, $lengthFilledProbability);
}
$length = count($probabilityArray);
$randomKey = random_int(0, $length-1);
$tweetKey = $probabilityArray[$randomKey];
return $texts[$tweetKey]['text'];
}
$CONSUMER_KEY = getenv('CONSUMER_KEY');
$CONSUMER_SECRET = getenv('CONSUMER_SECRET');
$ACCESS_TOKEN = getenv('ACCESS_TOKEN');
$ACCESS_TOKEN_SECRET = getenv('ACCESS_TOKEN_SECRET');
$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $ACCESS_TOKEN, $ACCESS_TOKEN_SECRET);
$tweets = include_once __DIR__ . '/tweets.php';
return function ($event) use ($connection, $tweets)
{
$result = [];
$randomLine = getRandomLine($tweets);
$result[] = $randomLine;
$statues = $connection->post("statuses/update", ["status" => $randomLine]);
$result[] = $statues;
return json_encode($result, JSON_PRETTY_PRINT);
};