chatroid 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +41 -33
- data/examples/hip_chat.rb +13 -0
- data/examples/irc.rb +13 -0
- data/examples/twitter_public_stream.rb +18 -0
- data/examples/twitter_timer.rb +21 -0
- data/examples/twitter_user_stream.rb +20 -0
- data/lib/chatroid/adapter/twitter.rb +1 -1
- data/lib/chatroid/timer/event.rb +54 -0
- data/lib/chatroid/timer.rb +24 -0
- data/lib/chatroid/version.rb +1 -1
- data/lib/chatroid.rb +4 -0
- metadata +21 -14
data/README.md
CHANGED
@@ -7,8 +7,13 @@ Chatroid is a gem for quickly creating chatterbot in Ruby.
|
|
7
7
|
$ gem "chatroid"
|
8
8
|
```
|
9
9
|
|
10
|
+
## Supported
|
11
|
+
* IRC
|
12
|
+
* Twitter
|
13
|
+
* HipChat(xmpp)
|
14
|
+
|
10
15
|
## Example
|
11
|
-
|
16
|
+
For more examples, please see [examples](https://github.com/r7kamura/chatroid/tree/master/examples).
|
12
17
|
|
13
18
|
```ruby
|
14
19
|
require "chatroid"
|
@@ -21,53 +26,56 @@ Chatroid.new do
|
|
21
26
|
set :access_secret, "..."
|
22
27
|
|
23
28
|
on_tweet do |event|
|
24
|
-
if event["text"] =~ /
|
29
|
+
if event["text"] =~ /chatroid/
|
25
30
|
favorite event
|
26
|
-
follow event
|
27
31
|
end
|
28
32
|
end
|
29
33
|
|
30
34
|
on_reply do |event|
|
31
|
-
reply "
|
35
|
+
reply "Hi, i am a chatroid", event
|
36
|
+
end
|
37
|
+
|
38
|
+
on_time :hour => 12, :min => 0 do
|
39
|
+
tweet "Hello, world!"
|
32
40
|
end
|
33
41
|
end.run!
|
34
42
|
```
|
35
43
|
|
36
|
-
|
37
|
-
require "chatroid"
|
44
|
+
## Deploy to Heroku
|
38
45
|
|
39
|
-
Chatroid.new do
|
40
|
-
set :service, "HipChat"
|
41
|
-
set :room, "12345_example@conf.hipchat.com"
|
42
|
-
set :jid, "12345_67890@chat.hipchat.com"
|
43
|
-
set :nick, "example"
|
44
|
-
set :password, "..."
|
45
|
-
|
46
|
-
on_message do |time, nick, text|
|
47
|
-
if nick != config[:nick]
|
48
|
-
say "Hi, #{nick}"
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end.run!
|
52
46
|
```
|
47
|
+
$ gem install heroku
|
48
|
+
$ heroku login
|
49
|
+
|
50
|
+
$ mkdir bot
|
51
|
+
$ cd !$
|
52
|
+
$ echo "bot: bundle exec ruby bot.rb" >> Procfile
|
53
|
+
$ echo "source :rubygems" >> Gemfile
|
54
|
+
$ echo "gem 'chatroid'" >> Gemfile
|
55
|
+
$ bundle install
|
56
|
+
$ vim bot.rb
|
53
57
|
|
54
|
-
```ruby
|
55
58
|
require "chatroid"
|
56
59
|
|
57
60
|
Chatroid.new do
|
58
|
-
set :service,
|
59
|
-
set :
|
60
|
-
set :
|
61
|
-
set :
|
62
|
-
set :
|
63
|
-
|
64
|
-
|
65
|
-
|
61
|
+
set :service, "Twitter"
|
62
|
+
set :filter, "chatroid,Chatroid"
|
63
|
+
set :consumer_key, ENV["CONSUMER_KEY"]
|
64
|
+
set :consumer_secret, ENV["CONSUMER_SECRET"]
|
65
|
+
set :access_key, ENV["ACCESS_KEY"]
|
66
|
+
set :access_secret, ENV["ACCESS_SECRET"]
|
67
|
+
|
68
|
+
on_tweet do |event|
|
69
|
+
favorite event
|
70
|
+
end
|
71
|
+
|
72
|
+
on_reply do |event|
|
73
|
+
favorite event
|
66
74
|
end
|
67
75
|
end.run!
|
68
|
-
```
|
69
76
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
77
|
+
$ heroku create your_favorite_bot_name --stack cedar
|
78
|
+
$ heroku config:add CONSUMER_KEY=... CONSUMER_SECRET=... ACCESS_KEY=... ACCESS_SECRET=...
|
79
|
+
$ git push heroku master
|
80
|
+
$ heroku ps:scale bot=1
|
81
|
+
```
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "chatroid"
|
2
|
+
|
3
|
+
Chatroid.new do
|
4
|
+
set :service, "HipChat"
|
5
|
+
set :room, "12345_example@conf.hipchat.com"
|
6
|
+
set :jid, "12345_67890@chat.hipchat.com"
|
7
|
+
set :nick, "example"
|
8
|
+
set :password, "..."
|
9
|
+
|
10
|
+
on_message do |time, nick, text|
|
11
|
+
say "Hi, #{nick}" if nick != config[:nick]
|
12
|
+
end
|
13
|
+
end.run!
|
data/examples/irc.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "chatroid"
|
2
|
+
|
3
|
+
Chatroid.new do
|
4
|
+
set :service, "Irc"
|
5
|
+
set :server, "example.com"
|
6
|
+
set :port, "6667"
|
7
|
+
set :channel, "#chatroid"
|
8
|
+
set :username, "chatroid"
|
9
|
+
|
10
|
+
on_privmsg do |message|
|
11
|
+
privmsg "#chatroid", ":" + "Hi, i am a chatroid"
|
12
|
+
end
|
13
|
+
end.run!
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "chatroid"
|
2
|
+
|
3
|
+
Chatroid.new do
|
4
|
+
set :service, "Twitter"
|
5
|
+
set :filter, "chatroid,Chatroid"
|
6
|
+
set :consumer_key, "..."
|
7
|
+
set :consumer_secret, "..."
|
8
|
+
set :access_key, "..."
|
9
|
+
set :access_secret, "..."
|
10
|
+
|
11
|
+
on_tweet do |event|
|
12
|
+
favorite event
|
13
|
+
end
|
14
|
+
|
15
|
+
on_reply do |event|
|
16
|
+
favorite event
|
17
|
+
end
|
18
|
+
end.run!
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "chatroid"
|
2
|
+
|
3
|
+
Chatroid.new do
|
4
|
+
set :service, "Twitter"
|
5
|
+
set :consumer_key, "..."
|
6
|
+
set :consumer_secret, "..."
|
7
|
+
set :access_key, "..."
|
8
|
+
set :access_secret, "..."
|
9
|
+
|
10
|
+
on_timer :hour => 6 do
|
11
|
+
tweet "Good morning!"
|
12
|
+
end
|
13
|
+
|
14
|
+
on_timer :hour => 0 do
|
15
|
+
tweet "Good night!"
|
16
|
+
end
|
17
|
+
|
18
|
+
on_timer :day => [10, 20, 30] do
|
19
|
+
tweet "@chatroid How are you?"
|
20
|
+
end
|
21
|
+
end.run!
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "chatroid"
|
2
|
+
|
3
|
+
Chatroid.new do
|
4
|
+
set :service, "Twitter"
|
5
|
+
set :consumer_key, "..."
|
6
|
+
set :consumer_secret, "..."
|
7
|
+
set :access_key, "..."
|
8
|
+
set :access_secret, "..."
|
9
|
+
|
10
|
+
on_tweet do |event|
|
11
|
+
if event["text"] =~ /chatroid/
|
12
|
+
favorite event
|
13
|
+
follow event
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
on_reply do |event|
|
18
|
+
reply "Hi, i am a chatroid", event
|
19
|
+
end
|
20
|
+
end.run!
|
@@ -0,0 +1,54 @@
|
|
1
|
+
class Chatroid
|
2
|
+
module Timer
|
3
|
+
class Event
|
4
|
+
# `every` is a Hash object which may have following keys:
|
5
|
+
# :sec
|
6
|
+
# :min
|
7
|
+
# :hour
|
8
|
+
#
|
9
|
+
# `block` will be executed at times specified by `every`
|
10
|
+
#
|
11
|
+
# Example: "Hello, world!" will be printed at 14:00 and 14:30 every day
|
12
|
+
# event = Event.new(:hour => 14, :min => [0, 30], :sec => 0) do
|
13
|
+
# foo
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# loop do
|
17
|
+
# event.call
|
18
|
+
# sleep 1
|
19
|
+
# end
|
20
|
+
|
21
|
+
UNITS = [
|
22
|
+
:sec,
|
23
|
+
:min,
|
24
|
+
:hour,
|
25
|
+
:day,
|
26
|
+
:wday,
|
27
|
+
:month,
|
28
|
+
].freeze
|
29
|
+
|
30
|
+
def initialize(every, &block)
|
31
|
+
@every = every
|
32
|
+
@block = block
|
33
|
+
end
|
34
|
+
|
35
|
+
def call
|
36
|
+
@block.call if available?
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def available?
|
42
|
+
now = Time.now
|
43
|
+
UNITS.all? do |unit|
|
44
|
+
if @every[unit]
|
45
|
+
conditions = Array(@every[unit])
|
46
|
+
conditions.any? { |condition| condition == now.send(unit) }
|
47
|
+
else
|
48
|
+
true
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "chatroid/timer/event"
|
2
|
+
|
3
|
+
class Chatroid
|
4
|
+
module Timer
|
5
|
+
def on_time(every, &block)
|
6
|
+
events << Event.new(every, &block)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def start_timer
|
12
|
+
Thread.new do
|
13
|
+
loop do
|
14
|
+
events.each(&:call)
|
15
|
+
sleep 1
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def events
|
21
|
+
@events ||= []
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/chatroid/version.rb
CHANGED
data/lib/chatroid.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require "chatroid/version"
|
2
2
|
require "chatroid/callback"
|
3
|
+
require "chatroid/timer"
|
3
4
|
require "chatroid/adapter"
|
4
5
|
|
5
6
|
class Chatroid
|
6
7
|
include Callback
|
8
|
+
include Timer
|
7
9
|
|
8
10
|
def initialize(&block)
|
9
11
|
instance_eval(&block) if block_given?
|
@@ -14,8 +16,10 @@ class Chatroid
|
|
14
16
|
end
|
15
17
|
|
16
18
|
def run!
|
19
|
+
Thread.abort_on_exception = true
|
17
20
|
validate_connection
|
18
21
|
extend(adapter)
|
22
|
+
start_timer unless events.empty?
|
19
23
|
connect
|
20
24
|
end
|
21
25
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chatroid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: twitter-stream
|
16
|
-
requirement: &
|
16
|
+
requirement: &70332858617320 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70332858617320
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: twitter_oauth
|
27
|
-
requirement: &
|
27
|
+
requirement: &70332858613600 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70332858613600
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: xmpp4r
|
38
|
-
requirement: &
|
38
|
+
requirement: &70332858611960 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70332858611960
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: avalon
|
49
|
-
requirement: &
|
49
|
+
requirement: &70332858625340 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70332858625340
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: zircon
|
60
|
-
requirement: &
|
60
|
+
requirement: &70332858622360 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70332858622360
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
|
-
requirement: &
|
71
|
+
requirement: &70332858635180 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,7 +76,7 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70332858635180
|
80
80
|
description: Chatroid is a gem for quickly creating chatterbot in Ruby
|
81
81
|
email:
|
82
82
|
- r7kamura@gmail.com
|
@@ -90,6 +90,11 @@ files:
|
|
90
90
|
- README.md
|
91
91
|
- Rakefile
|
92
92
|
- chatroid.gemspec
|
93
|
+
- examples/hip_chat.rb
|
94
|
+
- examples/irc.rb
|
95
|
+
- examples/twitter_public_stream.rb
|
96
|
+
- examples/twitter_timer.rb
|
97
|
+
- examples/twitter_user_stream.rb
|
93
98
|
- lib/chatroid.rb
|
94
99
|
- lib/chatroid/adapter.rb
|
95
100
|
- lib/chatroid/adapter/hip_chat.rb
|
@@ -97,6 +102,8 @@ files:
|
|
97
102
|
- lib/chatroid/adapter/twitter.rb
|
98
103
|
- lib/chatroid/adapter/twitter/event.rb
|
99
104
|
- lib/chatroid/callback.rb
|
105
|
+
- lib/chatroid/timer.rb
|
106
|
+
- lib/chatroid/timer/event.rb
|
100
107
|
- lib/chatroid/version.rb
|
101
108
|
- spec/chatroid/adapter/hip_chat_spec.rb
|
102
109
|
- spec/chatroid/adapter/irc_spec.rb
|