mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Add slackbot guppy example (#342)
* Add slackbot guppy example * Add json dependence to slackbot Gemfile
This commit is contained in:
committed by
Pedro Nasser
parent
867eb4b176
commit
179c659cc8
5
examples/slackbot/guppy/Gemfile
Normal file
5
examples/slackbot/guppy/Gemfile
Normal file
@@ -0,0 +1,5 @@
|
||||
source 'https://rubygems.org'
|
||||
|
||||
gem 'httparty', '~> 0.14.0'
|
||||
gem 'slack_webhooks'
|
||||
gem 'json', '> 1.8.2'
|
||||
32
examples/slackbot/guppy/README.md
Normal file
32
examples/slackbot/guppy/README.md
Normal file
@@ -0,0 +1,32 @@
|
||||
## Quick Example for a SlackBot command in Ruby
|
||||
|
||||
This example will show you how to test and deploy a SlackBot command to IronFunctions.
|
||||
|
||||
```sh
|
||||
# create your func.yaml file
|
||||
fn init <YOUR_DOCKERHUB_USERNAME>/guppy
|
||||
# install dependencies, we need the json gem to run this
|
||||
docker run --rm -v "$PWD":/worker -w /worker iron/ruby:dev bundle install --standalone --clean
|
||||
# build the function
|
||||
fn build
|
||||
# test it
|
||||
cat slack.payload | fn run
|
||||
# push it to Docker Hub for use with IronFunctions
|
||||
fn push
|
||||
# Create a route to this function on IronFunctions
|
||||
fn routes create slackbot /guppy
|
||||
# Change the route response header content-type to application/json
|
||||
curl -X PUT http://127.0.0.1:8080/v1/apps/slackbot/routes/guppy -d '{ "route": { "headers": { "Content-type": ["application/json"] } } }'
|
||||
```
|
||||
|
||||
## Create a Slash Command integration in Slack
|
||||
|
||||
In Slack, go to Integrations, find Slash Commands, click Add, type in / as the command then click Add again. On the next page, take the IronFunctions route URL and paste it into the URL field then click Save Integration.
|
||||
|
||||
If running in localhost, use [ngrok](https://github.com/inconshreveable/ngrok).
|
||||
|
||||
# Try it out!
|
||||
|
||||
In slack, type /<COMMAND> [options] and you'll see the magic!
|
||||
|
||||
|
||||
37
examples/slackbot/guppy/commands.json
Normal file
37
examples/slackbot/guppy/commands.json
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"tableflip": {"image_url": "http://i.imgur.com/oGwPRux.png"},
|
||||
"shrug": {"image_url": "http://www.theblaze.com/wp-content/uploads/2015/03/Google-comment-gif.gif"},
|
||||
"forlorn": {"image_url": "https://lh4.googleusercontent.com/-Jp85Ek_5jJc/UdEYqhHzuLI/AAAAAAACF3s/U708yywLDCQ/w700-h412-no/forlorn%2Bcat.gif"},
|
||||
"beardscratch": {"image_url": "https://lh5.googleusercontent.com/-gfUwQSvd1bs/UjjHeU-b6HI/AAAAAAAC81o/xtMpBn1F87E/w958-h539-no/beardman.gif"},
|
||||
"atlast": {"image_url": "https://lh4.googleusercontent.com/-jzywmNWCPps/UdEYqnq2ECI/AAAAAAACF3w/eNnS_KXHzN0/w480-h226-no/morpheus-at-last.gif"},
|
||||
"ohyeah": {"image_url": "https://lh4.googleusercontent.com/-IqiWUKEzd_U/UnmdxBZp-bI/AAAAAAADFf4/l7dyXzMTdDs/w245-h285-no/code-05.gif"},
|
||||
"facepalm": {"image_url": "https://lh4.googleusercontent.com/-kbD1OZrG7kI/UnmdupWzP1I/AAAAAAADFfg/g3SXEo1JXIs/s300-no/cQEBX.gif"},
|
||||
"peak": {"image_url": "https://lh4.googleusercontent.com/-V9zKyTXHrD4/Unmdzpnb1UI/AAAAAAADFgc/-61luVxML_M/w500-h320-no/efuITnY.gif"},
|
||||
"right": {"image_url": "https://lh6.googleusercontent.com/-vPkV8r6Mhy0/UnmdtC490bI/AAAAAAADFfM/Hil5QyuTX78/w500-h221-no/HuVXfMb.gif"},
|
||||
"sup": {"image_url": "https://lh3.googleusercontent.com/-iqoKtA72rIo/Unmd7o17ZUI/AAAAAAADFhM/7-MLxTAXU7Q/w470-h248-no/yC5BsCS.gif"},
|
||||
"nod": {"image_url": "https://lh4.googleusercontent.com/-byviKRrwH5c/Unny4gWkMqI/AAAAAAADFkc/OckIZqXDCY4/w500-h225-no/iPOS6.gif"},
|
||||
"thumbsup": {"image_url": "https://lh4.googleusercontent.com/-1l8KOia5vis/Unny3Jw851I/AAAAAAADFkY/bRLzh5VyPbc/w312-h176-no/ZCGrZ.gif"},
|
||||
"dance1": {"image_url": "https://lh5.googleusercontent.com/-5sN8WVo6Myc/Unny6ZsWZGI/AAAAAAADFk8/JssUbaDRG_M/w250-h303-no/mbjD3.gif"},
|
||||
"dance2": {"image_url": "https://lh5.googleusercontent.com/-2aPDr_v7Jgc/Unny3pXSfvI/AAAAAAADFkI/KyloHLe5DaM/w400-h234-no/gary.gif"},
|
||||
"dance3": {"image_url": "https://lh5.googleusercontent.com/-rqM2JLfd2xg/Unny2Pvpy1I/AAAAAAADFjY/Zx6D93XEws4/w480-h340-no/1tumblr_lgp6q5NhE21qcjtu8o1_500.gif"},
|
||||
"sidepunch": {"image_url": "https://lh6.googleusercontent.com/-HZYoVvXWc_s/UnmfpjqTbiI/AAAAAAADFic/q3dGlwfxV2w/w500-h247-no/code-27.gif"},
|
||||
"tyrionbird": {"image_url": "https://lh5.googleusercontent.com/-6frbpEP6M3g/Unmfl9i3fcI/AAAAAAADFh8/SOpKxwyOk3k/w300-h149-no/code-19.gif"},
|
||||
"obiwan": {"image_url": "http://i.giphy.com/2vERRTu559XQk.gif"},
|
||||
"wat": {"image_url": "http://i.giphy.com/qxtxlL4sFFle.gif"},
|
||||
"ivan": {"image_url": "http://i.giphy.com/2OMHmoFMiJjfq.gif"},
|
||||
"freakout": {"image_url": "http://i.giphy.com/FwpecpDvcu7vO.gif"},
|
||||
"boom-chuck": {"image_url": "http://i.giphy.com/10PkQyXWagNrqg.gif"},
|
||||
"boom-drake": {"image_url": "http://i.giphy.com/8vW915bv0DqXC.gif"},
|
||||
"boom-dany": {"image_url": "http://i.giphy.com/LrN9NbJNp9SWQ.gif"},
|
||||
"boom-moon": {"image_url": "http://i.giphy.com/ydMNTWYVjSEFi.gif"},
|
||||
"boom-franco": {"image_url": "http://i.giphy.com/b3u8anVaWFQ9G.gif"},
|
||||
"boom-starcraft": {"image_url": "http://i.giphy.com/5xtDarxQ05ee2ktRA5i.gif"},
|
||||
"boom-anime": {"image_url": "http://i.giphy.com/fMNNDKtOVbxSM.gif"},
|
||||
"boom-denzel":{"image_url":"http://i.imgur.com/1LXMDns.gif"},
|
||||
"boom-ump":{"image_url":"http://cdn0.sbnation.com/imported_assets/1160455/umpboom.gif"},
|
||||
"fun": {"image_url": "http://s-ak.buzzfed.com/static/imagebuzz/web04/2011/3/14/10/anigif_fun-fun-fun-29834-1300112565-20.gif"},
|
||||
"fthis": {"image_url": "http://i.giphy.com/iICCUuGJOhOnK.gif"},
|
||||
"disappear": {"image_url": "http://i.giphy.com/4pMX5rJ4PYAEM.gif"},
|
||||
"cry": {"image_url": "http://i.giphy.com/10tIjpzIu8fe0.gif"},
|
||||
"micdrop":{"image_url":"http://i.imgur.com/9XHl18c.gif"}
|
||||
}
|
||||
44
examples/slackbot/guppy/func.rb
Normal file
44
examples/slackbot/guppy/func.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
require_relative 'bundle/bundler/setup'
|
||||
require 'slack_webhooks'
|
||||
|
||||
payload = STDIN.read
|
||||
#STDERR.puts "PAYLOAD: #{payload}"
|
||||
|
||||
images = JSON.load(File.open('commands.json'))
|
||||
|
||||
response = {}
|
||||
attachment = {
|
||||
"fallback" => "wat?!",
|
||||
"text" => "",
|
||||
"image_url" => "http://i.imgur.com/7kZ562z.jpg"
|
||||
}
|
||||
|
||||
help = "Available options are:\n"
|
||||
images.each_key { |k| help << "* #{k}\n" }
|
||||
|
||||
response = {}
|
||||
a = []
|
||||
response[:attachments] = a
|
||||
|
||||
if payload.nil? || payload.strip == ""
|
||||
response[:text] = help
|
||||
a << attachment
|
||||
puts response.to_json
|
||||
exit
|
||||
end
|
||||
|
||||
sh = SlackWebhooks::Hook.new('guppy', payload, "")
|
||||
r = images[sh.text]
|
||||
if r
|
||||
a << {image_url: r['image_url'], text: ""}
|
||||
response[:response_type] = "in_channel"
|
||||
response[:text] = "guppy #{sh.text}"
|
||||
else
|
||||
response[:text] = help
|
||||
a << attachment
|
||||
end
|
||||
|
||||
puts response.to_json
|
||||
|
||||
|
||||
|
||||
1
examples/slackbot/guppy/slack.payload
Normal file
1
examples/slackbot/guppy/slack.payload
Normal file
@@ -0,0 +1 @@
|
||||
token=somerandomtoken&team_id=someteamid&team_domain=somename&channel_id=somechannelid&channel_name=directmessage&user_id=someid&user_name=someuser&command=%2Fguppy&text=freakout
|
||||
Reference in New Issue
Block a user