2016-10-19 202 views
0

我已經在NODE JS中編寫了一個bot,它將持續監控抽搐用戶,當某些用戶上線時,它會發送消息給用戶在線的「group me」應用。 它工作正常。我的問題是,有時它發送消息,該用戶在線但實際上用戶處於脫機狀態。 關於如何解決這個問題的任何想法?Twitch流狀態顯示在線。當用戶離線

See screenshot

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)); 
 
    }); 
 
\t 
 
\t 
 
\t 
 
\t 
 
} 
 

 
app.listen(app.get('port'), function() { 
 
    console.log('Node app is running on port', app.get('port')); 
 
    theCall(0); 
 
}); 
 

回答

0

你確定數據[ 「流」]總是空每當流光離線?它可以不確定嗎?可否請您嘗試

if (!data["stream"]) 

代替

if (data["stream"] === null) 

如果這不起作用,你可以console.log你的數據和響應變量的虛假在線流,並更新問題?

+0

是的,當用戶離線時它將爲空。數據變量不同 –

相關問題