Skip to content

Commit

Permalink
#45 is now done. --surprise switch returns a random phrase
Browse files Browse the repository at this point in the history
  • Loading branch information
matijaabicic committed Jan 18, 2017
1 parent 7d4d1f0 commit bd2ed96
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 5 deletions.
15 changes: 15 additions & 0 deletions lib/helpPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ module.exports.help = function (responseType, req_command){
}
]
},
{
"pretext" : "Feeling lucky? Why not --surprise yourself and yoru channel. --surprise gives you a completely random phrase.",
"fields" : [
{
"title" : "Surprise",
"value" : "--surprise",
"short" : true
}
]
},
{
"pretext" : "Don't botheer retyping that long phrase just to make make it --public. Just use --last",
"fields" : [
Expand Down Expand Up @@ -110,6 +120,11 @@ module.exports.help = function (responseType, req_command){
"title":"Example use 8:",
"value":req_command + ' --set --public --norandom --sfw',
"short":true
},
{
"title":"Example use 9:",
"value":req_command + ' --surprise --public --sfw',
"short":true
}
]
}
Expand Down
7 changes: 6 additions & 1 deletion lib/slack-command-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ module.exports.parse = function (preParsedCommandText, userDefaultSettings){
result.feedback = true;
commandText = commandText.replace(/—feedback/g, '').replace(/--feedback/g,'');
}


// #45 - ability to produce a random (surprise) definition
if (commandText.match(/--surprise/g) || commandText.match(/—surprise/g)){
result.surprise = true;
}

//look for "sfw" switch. will be used to censor the output.
if (commandText.match(/--sfw/g) || commandText.match(/—sfw/g)){
result.rating = 'sfw';
Expand Down
6 changes: 5 additions & 1 deletion lib/urban-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ exports.parse = function (bodyString, parsedCommand){

switch(data.result_type)
{
// #45 - random lookup don't come with result type element.
// added in v1.2.0 as a special case
case (undefined && parsedCommand.surprise):
case "exact":
{
var numberOfDefinitions = data.list.length;
Expand Down Expand Up @@ -53,7 +56,8 @@ exports.parse = function (bodyString, parsedCommand){
}];

//add a sound if it's contained in the response
if (data.sounds.length > 0){
// #45 - random (surprise) results don't return the sounds node. safe check.
if (!parsedCommand.surprise && data.sounds.length > 0){

//#33 randomize the sounds as well in v0.4.2
if(parsedCommand.random)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "urban-slack",
"version": "1.1.0",
"version": "1.2.0",
"description": "Urban Dictionary Slack bot",
"main": "server.js",
"directories": {
Expand Down
11 changes: 10 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,15 @@ app.post('/api', function(req, res){
//if requesting last, we need to fetch it from the DB first
//this can be moved to in-memory later
//get ready to issue request to Urban API
var urban_request = settings.urbanAPI + mlabOptions.documents.queryText; // (parsedCommand.last ? mLabHelper.GetLastQueryForUser(req_team_id, req_user_id) : parsedCommand.Command);

var urban_request = "";
// #45 - Random (surprise) phrase. Added in v1.2.0
if (parsedCommand.surprise){
urban_request = settings.randomUrbanAPI;
}
else {
urban_request = settings.urbanAPI + mlabOptions.documents.queryText; // (parsedCommand.last ? mLabHelper.GetLastQueryForUser(req_team_id, req_user_id) : parsedCommand.Command);
}
//set urban api url;
var options = {
url: urban_request
Expand All @@ -288,6 +295,8 @@ app.post('/api', function(req, res){

//save the type of response from UD API
var result_type = JSON.parse(body).result_type;
// added in v1.2.0 - #45 - treat random surprise queries separately
if (parsedCommand.surprise) result_type = 'surprise';

//update lastPhrase if we've got an exact response
if(result_type=="exact"){
Expand Down
1 change: 1 addition & 0 deletions settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
serverPort : 5000,
GA : 'UA-74299720-1', // Google analytics tracking ID
urbanAPI : 'http://api.urbandictionary.com/v0/define?term=',
randomUrbanAPI : 'http://api.urbandictionary.com/v0/random',
gaIgnoreHosts : ['localhost:8080', 'localhost:5000', 'urban-slack.com'], //list of hosts to be ignored on API paths

//slack response modes:
Expand Down
4 changes: 3 additions & 1 deletion test/sampledata.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
"defaultsChecker" : "/urban --mysettings",
"defaultsChecker_result" : {"responseType":"ephemeral","rating":"nsfw","random":false,"mysettings":true,"Command":"\"/urban \""},
"userFeedback" : "/urban --feedback Here's some feedback!!",
"userFeedback_result" : {"responseType":"ephemeral","rating":"nsfw","random":false,"feedback":true,"Command":"\"/urban Here's some feedback!!\""}
"userFeedback_result" : {"responseType":"ephemeral","feedback":true,"rating":"nsfw","random":false,"Command":"\"/urban Here's some feedback!!\""},
"surprise" : "/urban --surprise",
"surprise_result" : {"responseType":"ephemeral","rating":"nsfw","random":false,"Command":"\"/urban --surprise\""}
}
}
4 changes: 4 additions & 0 deletions web/views/howto.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
<dd><code>/urban --mysettings</code> </dd>
<dd>It might come in handy checking your settings before that --NSFW definition --randomly goes into a --public channel.</dd>
<br/>
<dt>Surprise !!</dt>
<dd><code>/urban --surprise</code> </dd>
<dd>Picks a completely random phrase. For those rare moments when you're bored and uninspired, just go <code>/urban --public --surprise</code> and hope for the best.</dd>
<br/>
<dt>Say it like it is</dt>
<dd><code>/urban --feedback This is Awesome</code></dd>
<dd>Tell us what you think at any time with <code>--feedback</code> switch without leaving slack</dd>
Expand Down

0 comments on commit bd2ed96

Please sign in to comment.