github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/examples/slackbot/guppy/func.rb (about)

     1  require_relative 'bundle/bundler/setup'
     2  require 'slack_webhooks'
     3  
     4  payload = STDIN.read
     5  #STDERR.puts "PAYLOAD: #{payload}"
     6  
     7  images = JSON.load(File.open('commands.json'))
     8  
     9  response = {}
    10  attachment = {
    11      "fallback" => "wat?!",
    12      "text" => "",
    13      "image_url" => "http://i.imgur.com/7kZ562z.jpg"
    14  }
    15  
    16  help = "Available options are:\n"
    17  images.each_key { |k| help << "* #{k}\n" }
    18  
    19  response = {}
    20  a = []
    21  response[:attachments] = a
    22  
    23  if payload.nil? || payload.strip == ""
    24    response[:text] = help
    25    a << attachment
    26    puts response.to_json
    27    exit
    28  end
    29  
    30  sh = SlackWebhooks::Hook.new('guppy', payload, "")
    31  r = images[sh.text]
    32  if r
    33    a << {image_url: r['image_url'], text: ""} 
    34    response[:response_type] = "in_channel"
    35    response[:text] = "guppy #{sh.text}" 
    36  else
    37    response[:text] = help
    38    a << attachment
    39  end
    40  
    41  puts response.to_json
    42  
    43  
    44