我已经在NODE JS中编写了一个机器人,它将持续监控抽搐用户,当一些用户上线时,它会发送消息给" group me"用户在线的应用程序。 它工作正常。我的问题是,有时它会发送用户在线但实际上用户处于离线状态的消息。 关于如何解决这个问题的任何想法?
var users = ["Statelinejay", "Ad_914", "Houssam06" ];
var messages = ["notsent", "notsent", "notsent" ];
var Client = require('node-rest-client').Client;
var client = new Client();
var express = require('express');
var request = require('request');
var app = express();
app.set('port', (process.env.PORT || 5000));
function theCall(index) {
console.log('Processing ' + users[index]);
client.get('https://api.twitch.tv/kraken/streams/' + users[index] + '?client_id=xxxxxxxxxxxxxxxxxxxxxxx', function (data, response) {
if (data["stream"] === null) {
messages[index] = 'notsent';
console.log(users[index] + 'is offline');
} else
//// Start of sending message
var myJSONObject = {
"bot_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"text": " https://www.twitch.tv/" + users[index]
};
if (messages[index] === 'notsent') {
request.post({
url: " https://api.groupme.com/v3/bots/post",
method: "POST",
json: true, // <--Very important!!!
body: myJSONObject
},
function (error, response, body) {
console.log(error);
}
);
messages[index] = 'sent';
console.log('message sent');
}
//// End of sending message
console.log(users[index] + ' is online') ;
}
theCall((++index % users.length));
});
}
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
theCall(0);
});
&#13;
答案 0 :(得分:0)
当流媒体离线时,您确定数据[&#34; stream&#34;]始终为空吗?可以不定义吗?你能试试吗
if (!data["stream"])
而不是
if (data["stream"] === null)
如果这不起作用,你可以使用console.log记录虚假在线流媒体的数据和响应变量,并更新问题吗?