twitter-bot 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +75 -0
- data/Rakefile +1 -0
- data/lib/twitter/bot.rb +99 -0
- data/lib/twitter/bot/configuration.rb +54 -0
- data/lib/twitter/bot/version.rb +5 -0
- data/misc/sneak_peek.png +0 -0
- data/twitter-bot.gemspec +28 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 61933d001c493f9d222c14d77f7247891172d1d4
|
4
|
+
data.tar.gz: e65f894d371a3d2478019cd40266be41220a5c85
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a0fbe99a8e26c3bd9d1d68650d0869df5b43d26cc2adc363e40d52542c0845ed2dfcee991c9e7554bf161dc0fd7f49435ba7005080602da7f8db213b654ec1ad
|
7
|
+
data.tar.gz: e03e98c9e315045d61ea6fb23ace503007bf71986adde7a36491b725dcc057998c4dedf31650efa6070a72570298cfbb232caa1480a2a833d10ed6a32cf388af
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Heather Rivers
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# twitter-bot
|
2
|
+
|
3
|
+
Search Twitter for specific terms and automatically reply from your bot
|
4
|
+
account.
|
5
|
+
|
6
|
+
Please exercise reasonable benevolence with your new powers.
|
7
|
+
|
8
|
+

|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'twitter-bot'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install twitter-bot
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### 1. Register a Twitter client application
|
27
|
+
|
28
|
+
Using your Twitter bot account:
|
29
|
+
|
30
|
+
1. Go to https://apps.twitter.com/app/new and create an app
|
31
|
+
|
32
|
+
2. Go to the **Keys and Access Tokens** tab and click "Create my access token"
|
33
|
+
at the bottom.
|
34
|
+
|
35
|
+
You will need to save the 2 keypairs on this page (consumer key/secret & token
|
36
|
+
key/secret) to configure your bot.
|
37
|
+
|
38
|
+
### 2. Create a bot
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
bot = Twitter::Bot.new(
|
42
|
+
consumer_key: 'value',
|
43
|
+
consumer_secret: 'value',
|
44
|
+
access_token_key: 'value',
|
45
|
+
access_token_secret: 'value')
|
46
|
+
|
47
|
+
bot.search('sneak peak') do |tweet|
|
48
|
+
'I think you mean "sneak peek"'
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
#### Advanced configuration
|
53
|
+
|
54
|
+
* `interval`: seconds to wait between polling requests (default: 5)
|
55
|
+
* `user_agent`: custom user agent for Twitter API requests
|
56
|
+
|
57
|
+
#### More examples
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
bot.search('"how long is this tweet"') do |tweet|
|
61
|
+
"@#{tweet.user.screen_name} This is #{tweet.text.size} characters long"
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
bot.search('from:nihilist_arbys "horsey saurce"') { |tweet| 'Yum!' }
|
67
|
+
```
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
1. Fork it
|
72
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
73
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
74
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
75
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/twitter/bot.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'twitter/bot/configuration'
|
2
|
+
require 'twitter/bot/version'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module Twitter
|
6
|
+
class Bot
|
7
|
+
attr_writer :logger
|
8
|
+
attr_reader :options
|
9
|
+
|
10
|
+
# @param options [Hash] hash of configuration options
|
11
|
+
#
|
12
|
+
# @option options [String] :consumer_key
|
13
|
+
# @option options [String] :consumer_secret
|
14
|
+
# @option options [String] :access_token
|
15
|
+
# @option options [String] :access_token_secret
|
16
|
+
#
|
17
|
+
# @option options [String] :user_agent
|
18
|
+
# @option options [Integer] :interval polling interval
|
19
|
+
#
|
20
|
+
def initialize(options)
|
21
|
+
@options = options
|
22
|
+
end
|
23
|
+
|
24
|
+
def logger
|
25
|
+
@logger ||= Logger.new(STDOUT)
|
26
|
+
end
|
27
|
+
|
28
|
+
def search(query, &block)
|
29
|
+
query = query.to_s
|
30
|
+
|
31
|
+
logger.info "Polling for #{query.inspect} every #{config.interval}s..."
|
32
|
+
|
33
|
+
cursor = nil
|
34
|
+
|
35
|
+
loop do
|
36
|
+
perform_search(query, cursor) do |tweet|
|
37
|
+
reply(tweet, block)
|
38
|
+
|
39
|
+
cursor = tweet.id if cursor.nil? || tweet.id > cursor
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def perform_search(query, cursor)
|
47
|
+
options = { result_type: 'recent' }
|
48
|
+
|
49
|
+
options[:since_id] = cursor if cursor
|
50
|
+
|
51
|
+
logger.info "Searching for #{query}..."
|
52
|
+
|
53
|
+
begin
|
54
|
+
client.search(query, options).each { |tweet| yield tweet }
|
55
|
+
rescue Twitter::Error::TooManyRequests
|
56
|
+
logger.info 'Rate limit exceeded. Resuming in 30s...'
|
57
|
+
|
58
|
+
sleep 30
|
59
|
+
end
|
60
|
+
|
61
|
+
sleep config.interval
|
62
|
+
end
|
63
|
+
|
64
|
+
def reply(tweet, block)
|
65
|
+
*args = block.call(tweet)
|
66
|
+
|
67
|
+
text = args.shift
|
68
|
+
tweet_options = args.shift || {}
|
69
|
+
|
70
|
+
return unless text && text.size > 0
|
71
|
+
|
72
|
+
tweet_options[:in_reply_to_status] = tweet
|
73
|
+
|
74
|
+
logger.info "Tweet: #{tweet.full_text}"
|
75
|
+
logger.info "Autoreply: #{text}\n"
|
76
|
+
|
77
|
+
begin
|
78
|
+
client.update(text, tweet_options)
|
79
|
+
rescue Twitter::Error.errors.values => exception
|
80
|
+
logger.error "Autoreply failed: #{exception.class} #{exception.message}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def config
|
85
|
+
@config ||= Configuration.new(options)
|
86
|
+
end
|
87
|
+
|
88
|
+
def client
|
89
|
+
@client ||= Twitter::REST::Client.new do |client|
|
90
|
+
client.consumer_key = config.consumer_key
|
91
|
+
client.consumer_secret = config.consumer_secret
|
92
|
+
client.access_token = config.access_token
|
93
|
+
client.access_token_secret = config.access_token_secret
|
94
|
+
|
95
|
+
client.user_agent = config.user_agent
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Twitter
|
2
|
+
class Bot
|
3
|
+
class Configuration
|
4
|
+
DEFAULT_INTERVAL = 5
|
5
|
+
|
6
|
+
DEFAULT_USER_AGENT =
|
7
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 ' \
|
8
|
+
'(KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36'
|
9
|
+
|
10
|
+
CREDENTIALS = %w(
|
11
|
+
consumer_key
|
12
|
+
consumer_secret
|
13
|
+
access_token
|
14
|
+
access_token_secret
|
15
|
+
)
|
16
|
+
|
17
|
+
attr_reader :values
|
18
|
+
|
19
|
+
def initialize(values)
|
20
|
+
values.keys.each { |key| values[key.to_s] = values.delete(key) }
|
21
|
+
|
22
|
+
@values = values
|
23
|
+
|
24
|
+
validate!
|
25
|
+
end
|
26
|
+
|
27
|
+
CREDENTIALS.each do |name|
|
28
|
+
define_method(name) { values.fetch(name) }
|
29
|
+
end
|
30
|
+
|
31
|
+
def interval
|
32
|
+
values.fetch('interval', DEFAULT_INTERVAL)
|
33
|
+
end
|
34
|
+
|
35
|
+
def user_agent
|
36
|
+
values.fetch('user_agent', DEFAULT_USER_AGENT)
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def validate!
|
42
|
+
CREDENTIALS.each do |name|
|
43
|
+
unless values.key?(name)
|
44
|
+
fail InvalidValue, "Missing required configuration key `#{name}`"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
true
|
49
|
+
end
|
50
|
+
|
51
|
+
class InvalidValue < StandardError; end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/misc/sneak_peek.png
ADDED
Binary file
|
data/twitter-bot.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'twitter/bot/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'twitter-bot'
|
8
|
+
spec.version = Twitter::Bot::VERSION
|
9
|
+
spec.authors = ['Heather Rivers']
|
10
|
+
spec.email = ['git@heatherrivers.com']
|
11
|
+
spec.description = 'Twitter autoreply bot'
|
12
|
+
spec.summary = 'Search Twitter for specific terms and automatically ' \
|
13
|
+
'reply from your bot account.'
|
14
|
+
spec.homepage = 'https://github.com/rivers/twitter-bot'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.required_ruby_version = '~> 2.0'
|
23
|
+
|
24
|
+
spec.add_dependency 'twitter', '~> 5.14.0'
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
27
|
+
spec.add_development_dependency 'rake'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: twitter-bot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Heather Rivers
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: twitter
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.14.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.14.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Twitter autoreply bot
|
56
|
+
email:
|
57
|
+
- git@heatherrivers.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/twitter/bot.rb
|
68
|
+
- lib/twitter/bot/configuration.rb
|
69
|
+
- lib/twitter/bot/version.rb
|
70
|
+
- misc/sneak_peek.png
|
71
|
+
- twitter-bot.gemspec
|
72
|
+
homepage: https://github.com/rivers/twitter-bot
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ~>
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '2.0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.0.3
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Search Twitter for specific terms and automatically reply from your bot account.
|
96
|
+
test_files: []
|