Created
December 30, 2022 21:18
-
-
Save stympy/c5923633ec2a4949c4781c42c2e210ef to your computer and use it in GitHub Desktop.
Lambda function for slack chat -> Printfection API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
require "rest-client" | |
require "honeybadger" | |
def command_parser(message) | |
case message | |
when /shirt/i | |
response = RestClient.post("https://#{ENV["PRINTFECTION_TOKEN"]}:@api.printfection.com/v2/orders", | |
JSON.dump({campaign_id: ENV["PRINTFECTION_SHIRT_CAMPAIGN"]}), | |
{content_type: :json, accept: :json}) | |
"Go here to get your shirt: #{JSON.parse(response.body)["url"]}" | |
else | |
"Sorry, I dont know how to respond to that" | |
end | |
end | |
def tell_slack(channel, message) | |
RestClient.post("https://slack.com/api/chat.postMessage", {token: ENV["OAUTH_TOKEN"], channel: channel, text: message}) | |
end | |
def handler(event:, context:) | |
data = JSON.parse(event["body"]) | |
# This is sent when slack is verifying the bot URL during setup | |
return {statusCode: 200, body: data["challenge"]} if data["challenge"] | |
return {statusCode: 403, body: "Invalid token"} if data["token"].nil? || data["token"] != ENV["SLACK_TOKEN"] | |
slack_event = data["event"] | |
# Ignore events generated by the bot or non-text events | |
if slack_event["bot_id"].nil? && slack_event["text"] | |
reply = command_parser(slack_event["text"].sub(%r{(<@\w+> )?}, "")) | |
tell_slack(slack_event["channel"], reply) | |
end | |
{statusCode: 200, body: "OK"} | |
rescue => e | |
tell_slack(slack_event["channel"], "Got error: #{e}") | |
Honeybadger.notify(e) | |
{statusCode: 500, body: e.message} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment