· last year · Oct 22, 2023, 02:10 PM
1<?php
2
3require 'vendor/autoload.php';
4
5use Abraham\TwitterOAuth\TwitterOAuth;
6
7// Function Get Random Line
8function randomTweet($tweets)
9{
10 $lines = file($tweets);
11 return $lines[array_rand($lines)];
12}
13
14// API KEY TWITTER
15$consumer_key = '';
16$consumer_secret = '';
17$access_token = '';
18$access_token_secret = '';
19
20// Config Auto Reply
21$total_tweet = 5; // Tweet per Action
22
23
24// Connect to Account
25$connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
26$connection->get('account/verify_credentials');
27
28if ($connection->getLastHttpCode() == 200) {
29 // Get Tweet Status
30 $get_status = $connection->get('statuses/home_timeline', ['count' => $total_tweet]);
31
32
33 foreach ($get_status as $status) {
34 $i = 0;
35// Config Counter
36$file = 'counter.txt';
37$counter = 1;
38 if (file_exists($file)) {
39 $counter += file_get_contents($file);
40 }
41
42 // // Get Reply from File
43 // $tweet = '@' . $status->user->screen_name . ' ' . randomTweet('tweet_reply.txt');
44
45 // // Reply to Tweet
46 // $connection->post('statuses/update', ['in_reply_to_status_id' => $status->id, 'status' => $tweet]);
47
48 // Reply with Media
49 $tweet = '@' . $status->user->screen_name . ' ' . randomTweet('tweet_reply.txt') . $counter;
50 $media = $connection->upload('media/upload', ['media' => 'asu.PNG']);
51 $data = [
52 'in_reply_to_status_id' => $status->id,
53 'status' => $tweet,
54 'media_ids' => $media->media_id_string
55 ];
56 //$connection->post('statuses/update', $data);
57 // // Reply to Tweet
58 $connection->post('statuses/update', ['in_reply_to_status_id' => $status->id, 'status' => $tweet]);
59
60 if ($connection->getLastHttpCode() == 200) {
61 echo 'Successfully replied to ' . $tweet . '</br>';
62 $i++;
63 } else {
64 echo 'Failed to reply to the tweet!';
65 break;
66 }
67
68 file_put_contents($file, $counter);
69 }
70
71 if ($i >= 1) {
72 echo '</br> Success ' . $i . ' replies';
73 }
74} else {
75 echo 'Invalid API key!';
76}
77