仅在经过设定的时间后才允许调用函数
Allow a function to be called only after a set amount of time has passed
为了提供上下文,这是我试图解决的问题:
我制作了一个 giphy 机器人,用于与我的朋友进行休闲群聊。通过在消息中键入 /giphy [terms]
,它将自动 post [terms]
的最高结果。我的朋友们,他们都是粗鲁的混蛋,很快就开始滥用它来向群聊发送垃圾邮件。我想做的是只允许我的 postMessage
函数每分钟调用一次。
我试过的:
- 使用
setTimeout()
,这并不完全符合我的要求,因为它只会在参数中指定的时间过去后调用该函数。据我所知,这将导致从调用机器人时起消息延迟,但它实际上不会阻止机器人在那段时间内接受新的 postMessage()
呼叫。
- 使用
setInterval()
,这只会导致函数在一定的时间间隔内永远被调用。
我认为可行的方法:
现在,我正在处理两个 .js 文件。
Index.js
var http, director, cool, bot, router, server, port;
http = require('http');
director = require('director');
bot = require('./bot.js');
router = new director.http.Router({
'/' : {
post: bot.respond,
get: ping
}
});
server = http.createServer(function (req, res) {
req.chunks = [];
req.on('data', function (chunk) {
req.chunks.push(chunk.toString());
});
router.dispatch(req, res, function(err) {
res.writeHead(err.status, {"Content-Type": "text/plain"});
res.end(err.message);
});
});
port = Number(process.env.PORT || 5000);
server.listen(port);
function ping() {
this.res.writeHead(200);
this.res.end("This is my giphy side project!");
}
Bot.js
var HTTPS = require('https');
var botID = process.env.BOT_ID;
var giphy = require('giphy-api')();
function respond() {
var request = JSON.parse(this.req.chunks[0]);
var giphyRegex = /^\/giphy (.*)$/;
var botMessage = giphyRegex.exec(request.text);
var offset = Math.floor(Math.random() * 10);
if(request.text && giphyRegex.test(request.text) && botMessage != null) {
this.res.writeHead(200);
giphy.search({
q: botMessage[1],
rating: 'pg-13'
}, function (err, res) {
try {
postMessage(res.data[offset].images.downsized.url);
} catch (err) {
postMessage("There is no gif of that.");
}
});
this.res.end();
} else {
this.res.writeHead(200);
this.res.end();
}
function postMessage(phrase) {
var botResponse, options, body, botReq;
botResponse = phrase;
options = {
hostname: 'api.groupme.com',
path: '/v3/bots/post',
method: 'POST'
};
body = {
"bot_id" : botID,
"text" : botResponse
};
botReq = HTTPS.request(options, function(res) {
if(res.statusCode == 202) {
} else {
console.log('Rejecting bad status code: ' + res.statusCode);
}
});
botReq.on('error', function(err) {
console.log('Error posting message: ' + JSON.stringify(err));
});
botReq.on('timeout', function(err) {
console.log('Timeout posting message: ' + JSON.stringify(err));
});
botReq.end(JSON.stringify(body));
}
exports.respond = respond;
基本上,我想知道哪里是实现我设想的计时器的理想场所。似乎我想让它只在一分钟后收听 /giphy [terms]
,而不是等待一分钟 post.
我的问题:
解决此问题的最佳方法是在 response()
函数上设置一个计时器,这样它实际上每分钟只会解析一次传入的信息吗?有没有更优雅的地方放这个?
计时器应该如何处理该功能?我认为我不能每分钟 运行 response()
一次,因为这似乎意味着它只会每分钟解析一次来自 GroupMe API 的传入 json,所以它可能会错过我希望它捕获的传入消息。
存储发出请求的时间,然后使用它来查看是否应忽略后续请求(如果执行速度过快)。
var waitTime = 10*1000; // 10 s in millis
var lastRequestTime = null;
function respond() {
if(lastRequestTime){
var now = new Date();
if(now.getTime() - lastRequestTime.getTime() <= waitTime){
this.res.writeHead(200);
this.res.end("You have to wait "+waitTime/1000+" seconds.");
return;
}
}
lastRequestTime = new Date();
postMessage();
}
为了提供上下文,这是我试图解决的问题:
我制作了一个 giphy 机器人,用于与我的朋友进行休闲群聊。通过在消息中键入 /giphy [terms]
,它将自动 post [terms]
的最高结果。我的朋友们,他们都是粗鲁的混蛋,很快就开始滥用它来向群聊发送垃圾邮件。我想做的是只允许我的 postMessage
函数每分钟调用一次。
我试过的:
- 使用
setTimeout()
,这并不完全符合我的要求,因为它只会在参数中指定的时间过去后调用该函数。据我所知,这将导致从调用机器人时起消息延迟,但它实际上不会阻止机器人在那段时间内接受新的postMessage()
呼叫。 - 使用
setInterval()
,这只会导致函数在一定的时间间隔内永远被调用。
我认为可行的方法:
现在,我正在处理两个 .js 文件。
Index.js
var http, director, cool, bot, router, server, port;
http = require('http');
director = require('director');
bot = require('./bot.js');
router = new director.http.Router({
'/' : {
post: bot.respond,
get: ping
}
});
server = http.createServer(function (req, res) {
req.chunks = [];
req.on('data', function (chunk) {
req.chunks.push(chunk.toString());
});
router.dispatch(req, res, function(err) {
res.writeHead(err.status, {"Content-Type": "text/plain"});
res.end(err.message);
});
});
port = Number(process.env.PORT || 5000);
server.listen(port);
function ping() {
this.res.writeHead(200);
this.res.end("This is my giphy side project!");
}
Bot.js
var HTTPS = require('https');
var botID = process.env.BOT_ID;
var giphy = require('giphy-api')();
function respond() {
var request = JSON.parse(this.req.chunks[0]);
var giphyRegex = /^\/giphy (.*)$/;
var botMessage = giphyRegex.exec(request.text);
var offset = Math.floor(Math.random() * 10);
if(request.text && giphyRegex.test(request.text) && botMessage != null) {
this.res.writeHead(200);
giphy.search({
q: botMessage[1],
rating: 'pg-13'
}, function (err, res) {
try {
postMessage(res.data[offset].images.downsized.url);
} catch (err) {
postMessage("There is no gif of that.");
}
});
this.res.end();
} else {
this.res.writeHead(200);
this.res.end();
}
function postMessage(phrase) {
var botResponse, options, body, botReq;
botResponse = phrase;
options = {
hostname: 'api.groupme.com',
path: '/v3/bots/post',
method: 'POST'
};
body = {
"bot_id" : botID,
"text" : botResponse
};
botReq = HTTPS.request(options, function(res) {
if(res.statusCode == 202) {
} else {
console.log('Rejecting bad status code: ' + res.statusCode);
}
});
botReq.on('error', function(err) {
console.log('Error posting message: ' + JSON.stringify(err));
});
botReq.on('timeout', function(err) {
console.log('Timeout posting message: ' + JSON.stringify(err));
});
botReq.end(JSON.stringify(body));
}
exports.respond = respond;
基本上,我想知道哪里是实现我设想的计时器的理想场所。似乎我想让它只在一分钟后收听 /giphy [terms]
,而不是等待一分钟 post.
我的问题:
解决此问题的最佳方法是在
response()
函数上设置一个计时器,这样它实际上每分钟只会解析一次传入的信息吗?有没有更优雅的地方放这个?计时器应该如何处理该功能?我认为我不能每分钟 运行
response()
一次,因为这似乎意味着它只会每分钟解析一次来自 GroupMe API 的传入 json,所以它可能会错过我希望它捕获的传入消息。
存储发出请求的时间,然后使用它来查看是否应忽略后续请求(如果执行速度过快)。
var waitTime = 10*1000; // 10 s in millis
var lastRequestTime = null;
function respond() {
if(lastRequestTime){
var now = new Date();
if(now.getTime() - lastRequestTime.getTime() <= waitTime){
this.res.writeHead(200);
this.res.end("You have to wait "+waitTime/1000+" seconds.");
return;
}
}
lastRequestTime = new Date();
postMessage();
}