Class: Botsy::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/botsy.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subdomain, token, room_id, &block) ⇒ Bot

Returns a new instance of Bot.



14
15
16
17
18
19
20
21
22
23
# File 'lib/botsy.rb', line 14

def initialize(subdomain, token, room_id, &block)
  @subdomain = subdomain
  @room_id = room_id
  @token = token
  @regexes = {}
  load_info
  join_room
  ascertain_meaning(&block)
  start_listening_forever
end

Class Method Details

.fork(*args, &block) ⇒ Object



10
11
12
# File 'lib/botsy.rb', line 10

def self.fork(*args, &block)
  Kernel.fork { self.new(*args, &block) }
end

Instance Method Details

#hear(regex, &block) ⇒ Object

Register a responder via a regex that will be evaluated on the body



26
27
28
29
# File 'lib/botsy.rb', line 26

def hear(regex, &block)
  @regexes[regex] ||= []
  @regexes[regex] << block
end

#say(thing, type = 'TextMessage') ⇒ Object

Submit an HTTPS request with the JSON content to the speak path



32
33
34
35
36
37
38
39
# File 'lib/botsy.rb', line 32

def say(thing, type = 'TextMessage')
  request = Net::HTTP::Post.new("/room/#{@room_id}/speak.json", 'Content-Type' => 'application/json')
  request.body = { message: { type: type, body: thing } }.to_json
  request.basic_auth @token, 'x'
  http = Net::HTTP.new("#{@subdomain}.campfirenow.com", 443)
  http.use_ssl = true
  http.request(request)
end