-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generalized random responder (replacing dag bot) #155
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[ | ||
{ | ||
"botName": "Dag Bot", | ||
"defaultEmoji": ":dog:", | ||
"trigger": "d(o|a)g facts?", | ||
"responseUrl": "https://raw.githubusercontent.com/18F/18f-bot-facts/master/dags.json" | ||
} | ||
] |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const fs = require('fs'); | ||
const configs = JSON.parse( | ||
fs.readFileSync('config/slack-random-response.json') | ||
); | ||
|
||
const cachedRequests = {}; | ||
|
||
const getResponses = async (robot, config) => { | ||
if (config.responseList) { | ||
return config.responseList; | ||
} | ||
|
||
if (config.responseUrl) { | ||
if (cachedRequests[config.responseUrl]) { | ||
const cached = cachedRequests[config.responseUrl]; | ||
if (Date.now() < cached.expiry) { | ||
return cached.value; | ||
} | ||
} | ||
|
||
return new Promise(resolve => { | ||
robot | ||
.http(config.responseUrl) | ||
.header('User-Agent', '18F-bot') | ||
.get()((err, res, body) => { | ||
cachedRequests[config.responseUrl] = { | ||
expiry: Date.now() + 60000, // five minutes | ||
value: JSON.parse(body) | ||
}; | ||
resolve(JSON.parse(body)); | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
const responseFrom = ( | ||
robot, | ||
{ botName = null, defaultEmoji = null, ...config } = {} | ||
) => async res => { | ||
const message = {}; | ||
if (defaultEmoji) { | ||
message.icon_emoji = defaultEmoji; | ||
} | ||
if (botName) { | ||
message.username = botName; | ||
} | ||
|
||
const responses = await getResponses(robot, config); | ||
const response = res.random(responses); | ||
|
||
if (typeof response === 'object') { | ||
message.text = response.text; | ||
if (response.name) { | ||
message.username = response.name + (botName ? ` (${botName})` : ''); | ||
} | ||
if (response.emoji) { | ||
message.icon_emoji = response.emoji; | ||
} | ||
} else { | ||
message.text = response; | ||
const match = response.match(/^(:[^:]+:)(.*)$/); | ||
if (match) { | ||
message.icon_emoji = match[1]; | ||
message.text = match[2].trim(); | ||
} | ||
} | ||
|
||
if (message.icon_emoji || message.username) { | ||
message.as_user = false; | ||
} | ||
|
||
res.send(message); | ||
}; | ||
|
||
const attachTrigger = (robot, trigger, config) => { | ||
if (Array.isArray(trigger)) { | ||
trigger.forEach(t => | ||
robot.hear(new RegExp(t, 'i'), responseFrom(robot, config)) | ||
); | ||
} else { | ||
robot.hear(new RegExp(trigger, 'i'), responseFrom(robot, config)); | ||
} | ||
}; | ||
|
||
module.exports = robot => { | ||
if (Array.isArray(configs)) { | ||
configs.forEach(async config => { | ||
attachTrigger(robot, config.trigger, config); | ||
}); | ||
} | ||
}; | ||
|
||
module.exports.attachTrigger = attachTrigger; | ||
module.exports.getResponses = getResponses; | ||
module.exports.responseFrom = responseFrom; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think I'm clear on the 5 minutes caching here. What are we doing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments incoming, but the idea is not to make the network hit on every single response. Instead, it caches off the network result for five minutes before re-refetching it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So does that mean if call dag fact twice in a five minute window it'll give the same response, or that it only fetches the json every 5 max?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latter. You'll still get a random response, but it'll only fetch the JSON every five minutes.