Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅允许在经过设定的时间后调用函数_Javascript_Node.js_Timer_Groupme - Fatal编程技术网

Javascript 仅允许在经过设定的时间后调用函数

Javascript 仅允许在经过设定的时间后调用函数,javascript,node.js,timer,groupme,Javascript,Node.js,Timer,Groupme,为了提供上下文,我试图解决以下问题: 我制作了一个giphy机器人,与我的朋友进行随意的群聊。通过在消息中键入/giphy[terms],它将自动发布[terms]的顶部结果。我的朋友们,作为一个任性的混蛋,很快就开始滥用它来向群聊发送垃圾邮件。我想做的是防止这种情况发生,只允许我的postMessage函数每分钟调用一次 我所尝试的: 使用setTimeout(),这并不完全符合我的要求,因为它只会在参数中指定的时间过后调用函数。据我所知,这将导致从调用bot时起消息延迟,但实际上不会阻止b

为了提供上下文,我试图解决以下问题:

我制作了一个giphy机器人,与我的朋友进行随意的群聊。通过在消息中键入
/giphy[terms]
,它将自动发布
[terms]
的顶部结果。我的朋友们,作为一个任性的混蛋,很快就开始滥用它来向群聊发送垃圾邮件。我想做的是防止这种情况发生,只允许我的
postMessage
函数每分钟调用一次

我所尝试的:

  • 使用
    setTimeout()
    ,这并不完全符合我的要求,因为它只会在参数中指定的时间过后调用函数。据我所知,这将导致从调用bot时起消息延迟,但实际上不会阻止bot在此期间接受新的
    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!");
}
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;
Bot.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!");
}
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]
,而不是等待一分钟后发布

我的问题:

  • 最好的方法是在
    response()
    函数上设置一个计时器,因为它实际上每分钟只解析一次传入的信息?有没有更优雅的地方放这个

  • 计时器应该如何在该功能上工作?我不认为我可以每分钟运行一次
    response()
    ,因为这似乎意味着它每分钟只解析一次来自GroupMeAPI的传入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();
}
var waitTime=10*1000;//10毫秒
var lastRequestTime=null;
函数respond(){
如果(上次请求时间){
var now=新日期();

如果(now.getTime()-lastRequestTime.getTime()),为什么不设置一个只允许bot每分钟发布一次的标志呢?例如,为最后一次发布的时间戳添加一个附加变量,如果当前时间大于一分钟,则调用
postMessage
函数