Last active
January 16, 2016 18:01
-
-
Save cdcarter/07b63874ae3270706bf9 to your computer and use it in GitHub Desktop.
NPSP-Slack-Bot
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
/** | |
* @author Christian Carter (@cdcarter) | |
* @date Jan 2016 | |
* | |
* | |
* @description A simple NPSP Slack slashcommand to show you the number of new donations today! | |
* This is a "my first slackbot" condition class, it doesn't check auth tokens, it doesn't check which | |
* command was called, but it'll get us started! | |
*/ | |
@restResource(urlMapping='/slackbot/*') | |
global class SlackBot { | |
// The big bad `doPost` is the processing method in our code. We wrap the RestContext in Slackbot | |
// specific types, do our logic (in this case, count the number of new Opportunities today), and then | |
// set response values. We rely on the Apex REST API to automatically serialize our SlackResponse | |
// into compliant JSON. | |
@httpPost | |
global static SlackResponse doPost() { | |
SlackRequest request = new SlackRequest(RestContext.request.params); | |
SlackResponse response = new SlackResponse(); | |
String count = String.valueOf(([SELECT Count(Id) FROM Opportunity WHERE CreatedDate = TODAY][0]).get('expr0')); | |
String base = '{0} new donations today!'; | |
response.text = String.format(base, new List<String> {count}); | |
response.response_type = 'ephemeral'; | |
return response; | |
} | |
public class SlackRequest { | |
public String token {get; private set;} | |
public String channel_name {get; private set;} | |
public String user_name {get; private set;} | |
public String command {get; private set;} | |
public String text {get; private set;} | |
// construct a SlackRequest from a RestRequest.params Map | |
public SlackRequest(Map<String,String> params) { | |
this.token = params.get('token'); | |
this.channel_name = params.get('channel_name'); | |
this.user_name = params.get('user_name'); | |
this.command = params.get('command'); | |
this.text = params.get('text'); | |
} | |
} | |
global class SlackResponse { | |
public String text {get {return SlackBot.escapeString(this.text);}set;} | |
public Boolean mrkdwn {get;set;} | |
public String response_type {get;set;} // valid values are ['epehemeral','in_channel'] | |
// when we want to support attachments and fields, we'll have to handle the | |
// JSON serialization manually with JSONGenerator. | |
} | |
// escape the string to Slack API standards. we expect to be in normal parsing mode. | |
public static String escapeString(String str) { | |
return str.replace('&','&').replace('<','<').replace('>','>'); | |
} | |
} | |
/* | |
Copyright (c) 2015 Christian Carter | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
* Neither the name of Salesforce.org nor the names of | |
its contributors may be used to endorse or promote products derived | |
from this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment