class GroupMeBot::Bot

Constants

POST_URI

Attributes

bot_id[RW]
callback_port[RW]
post_uri[RW]

Public Class Methods

new(bot_id = nil, callback_port = 8080, post_uri = POST_URI) click to toggle source
# 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

Public Instance Methods

run_callback_server(&callback_block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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