Skip to content
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 2 commits into from
Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/slack-random-response.json
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"
}
]
142 changes: 0 additions & 142 deletions scripts/dag_fact.coffee

This file was deleted.

95 changes: 95 additions & 0 deletions scripts/random-responses.js
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)
};
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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?

Copy link
Member Author

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.

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;
85 changes: 0 additions & 85 deletions test/scripts/dag_fact.js

This file was deleted.

Loading