-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.php
81 lines (67 loc) · 2.65 KB
/
twitter.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
<?php
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$CONSUMER_KEY = getenv('CONSUMER_KEY');
$CONSUMER_SECRET = getenv('CONSUMER_SECRET');
$ACCESS_TOKEN = getenv('ACCESS_TOKEN');
$ACCESS_TOKEN_SECRET = getenv('ACCESS_TOKEN_SECRET');
$aqi = getAQI();
sendNewStatusFor($aqi);
setNewDisplayNameFor($aqi);
setNewAvatarFor($aqi);
function getAQI(){
// Read the JSON file contents into a string
$json_string = file_get_contents('outputs/aqi-outputs.json');
// Parse the JSON string into a PHP object
$data = json_decode($json_string);
return $data;
}
function sendNewStatusFor($aqi) {
global $CONSUMER_KEY , $CONSUMER_SECRET , $ACCESS_TOKEN, $ACCESS_TOKEN_SECRET;
$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $ACCESS_TOKEN, $ACCESS_TOKEN_SECRET);
$connection->setTimeouts(10, 60);
try {
$media = $connection->upload('media/upload', ['media' => 'outputs/map.png']);
echo " ** media: ", $media->media_id_string;
$parameters = ['lat' => 13.03886045, 'long' => 101.69978836, 'place_id' => "49c909a0270e8699", 'display_coordinates' => true, 'status' => $aqi->th_en_status, 'media_ids' => $media->media_id_string];
$result = $connection->post('statuses/update', $parameters);
} catch (Exception $e) {
$to = '[email protected]';
$subject = '@BangkokAQI Tweet Issue';
$message = $e->getMessage();
mail($to, $subject, $message);
}
}
function setNewDisplayNameFor($aqi) {
global $CONSUMER_KEY , $CONSUMER_SECRET , $ACCESS_TOKEN, $ACCESS_TOKEN_SECRET;
$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $ACCESS_TOKEN, $ACCESS_TOKEN_SECRET);
$connection->setTimeouts(10, 60);
try {
$parameters = ['name' => $aqi->name];
$result = $connection->post('account/update_profile', $parameters);
} catch (Exception $e) {
$to = '[email protected]';
$subject = '@BangkokAQI Name Issue';
$message = $e->getMessage();
mail($to, $subject, $message);
}
}
function setNewAvatarFor($aqi) {
global $CONSUMER_KEY , $CONSUMER_SECRET , $ACCESS_TOKEN, $ACCESS_TOKEN_SECRET;
$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $ACCESS_TOKEN, $ACCESS_TOKEN_SECRET);
$connection->setTimeouts(10, 60);
try {
$filename = $aqi->avatar;
$parameters = [
'image' => base64_encode(file_get_contents($filename))
];
echo " ** filename: ", $filename;
$result = $connection->post('account/update_profile_image', $parameters);
} catch (Exception $e) {
$to = '[email protected]';
$subject = '@BangkokAQI Name Issue';
$message = $e->getMessage();
mail($to, $subject, $message);
}
}
?>