Class: GroupMeBot::Bot

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

Constant Summary collapse

POST_URI =
"https://api.groupme.com/v3/bots/post"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot_id = nil, callback_port = 8080, post_uri = POST_URI) ⇒ Bot

Returns a new instance of Bot.



11
12
13
14
15
# File 'lib/group_me_bot/bot.rb', line 11

def initialize(bot_id = nil, callback_port = 8080, post_uri = POST_URI)
  @bot_id        = bot_id
  @callback_port = callback_port
  @post_uri      = URI.parse(post_uri)
end

Instance Attribute Details

#bot_idObject

Returns the value of attribute bot_id.



8
9
10
# File 'lib/group_me_bot/bot.rb', line 8

def bot_id
  @bot_id
end

#callback_portObject

Returns the value of attribute callback_port.



8
9
10
# File 'lib/group_me_bot/bot.rb', line 8

def callback_port
  @callback_port
end

#post_uriObject

Returns the value of attribute post_uri.



8
9
10
# File 'lib/group_me_bot/bot.rb', line 8

def post_uri
  @post_uri
end

Instance Method Details

#run_callback_server(&callback_block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/group_me_bot/bot.rb', line 64

def run_callback_server(&callback_block)
  Thread.start do
    server = TCPServer.new("0.0.0.0", self.callback_port)
    puts "Callback server running on port #{self.callback_port}"
    loop do
      Thread.start(server.accept) do |client|
        res = []

        while line = client.gets
          res.push(line.chomp)
        end

        client.close
        callback_body = JSON.parse(res.last)
        callback_block.call(self, callback_body)
        res.clear
      end
    end
  end
end

#send_image(url) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/group_me_bot/bot.rb', line 30

def send_image(url)
  req = Net::HTTP::Post.new(self.post_uri, initheader = { "Content-Type" => "application/json" })

  req.body = {
              "bot_id"      => self.bot_id,
              "attachments" => [{
                                 "type" => "image",
                                 "url"  => url
                               }]
             }.to_json

  res = Net::HTTP.start(self.post_uri.host, self.post_uri.port, :use_ssl => self.post_uri.scheme == "https") do |http|
    http.request(req)
  end
end

#send_location(long, lat, name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/group_me_bot/bot.rb', line 46

def send_location(long, lat, name)
  req = Net::HTTP::Post.new(self.post_uri, initheader = { "Content-Type" => "application/json" })

  req.body = {
              "bot_id"      => self.bot_id,
              "attachments" => [{
                                 "type" => "location",
                                 "lng"  => long.to_s,
                                 "lat"  => lat.to_s,
                                 "name" => name
                               }]
             }.to_json

  res = Net::HTTP.start(self.post_uri.host, self.post_uri.port, :use_ssl => self.post_uri.scheme == "https") do |http|
    http.request(req)
  end
end

#send_message(message) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/group_me_bot/bot.rb', line 17

def send_message(message)
  req = Net::HTTP::Post.new(self.post_uri, initheader = { "Content-Type" => "application/json" })

  req.body = {
              "bot_id" => self.bot_id,
              "text"   => message
             }.to_json

  res = Net::HTTP.start(self.post_uri.host, self.post_uri.port, :use_ssl => self.post_uri.scheme == "https") do |http|
    http.request(req)
  end
end