-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.php
More file actions
109 lines (83 loc) · 2.62 KB
/
bot.php
File metadata and controls
109 lines (83 loc) · 2.62 KB
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
#!/usr/bin/php
<?php
/**
* Twitter Auto Responder Bot
* Based on 'Twitter Autoresponder Bot' by Daniel15 (https://gist.github.com/820281)
*/
require '/oauth/twitteroauth.php';
require '/config.php';
class TwitterAutoResponder {
private $_replies = array();
private $_connection;
public function __construct() {
$this->_connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
}
public function run() {
echo '========= '.date('Y-m-d g:i:s A')." - Started =========\n";
// Get the last ID we replied to
$since_id = @file_get_contents('since_id');
if ($since_id == null) {
$since_id = 0;
}
// Store the ID of the last tweet
$max_id = $since_id;
// Verify the Twitter account exists
if (!$this->verify()) {
$this->_auth();
die();
}
// Loop through the replies
foreach ($this->_replies as $term => $reply) {
echo 'Performing search for '.$term.'... ';
$search = $this->search($term, $since_id);
echo 'Done, '.count($search->results).' results.';
// Store the max ID
if ($search->max_id_str > $max_id) {
$max_id = $search->max_id_str;
}
// Loop through the results
foreach ($search->results as $tweet) {
$this->reply($tweet, $reply);
}
}
file_put_contents('since_id', $max_id);
echo '========= '.date('Y-m-d g:i:s A')." - Finished =========\n";
}
private function reply($tweet, $reply) {
try {
echo '@'.$tweet->from_user.' said: '.$tweet->text."\n";
$this->_connection->post('status/update', array(
'status' => '@'.$tweet->from_user.' '.$reply,
'in_reply_to_status_id' => $tweet->id_str
);
}
catch (OAuthException $e) {
echo 'ERROR: '.$e->message;
die();
}
}
private function verify() {
try {
$this->_connection->get('account/verify_credentials');
return true;
} catch (OAuthException $e) {
return false;
}
}
private function _auth() {
// First get a request token, then prompt them to go to the URL
echo 'OAuth Verification Needed. Retrieving request token...';
$request_token = $this->_connection->getRequestToken();
$redirect_url = $this->_connection->getAuthorizeUrl($request_token);
echo 'Please navigate to this URL for authentication: '.$redirect_url;
echo 'Once done, and you have a PIN Number, press ENTER.';
fread(STDIN, 10);
echo 'PIN Number: ';
$pin = trim(fread(STDIN, 10));
// Swap the PIN for an access token
//!!!
}
public function test() {
$this->_connection->post('statuses/update', array('status' => 'Test tweet from TwitterBot'));
}
}