Added Twitter to PiGateway so it can send tweet events

Started by jorgmuskens, April 07, 2016, 02:02:28 PM

jorgmuskens

Hi,

I just wanted to let you know that I added a twitter functionality to the PiGateway version 8.
For those who want to be able to send tweets instead of email or sms.

First follow Felix tutorial to install the PiGateway to an Raspberry.

Step 1:
Login your raspberry via ssh and navigate to your gateway directory
pi@raspberrypi ~ $ cd gateway
pi@raspberrypi ~/gateway $


Step 2:
Install the twitter api for nodejs in de gateway directory by using the below code
npm install node-twitter-api


Step 3:
Edit your gateway.js file to add the twitter functionality
var twitterAPI = require('node-twitter-api');
var twitter = new twitterAPI({
    consumerKey: 'your consumer Key',
    consumerSecret: 'your consumer secret',
    callback: 'http://yoururl.tld/something'
});

global.sendTweet = function(BODY) {
  var tweet = {
      status: BODY
  };
  var accessToken = 'your access Token';
  var accessTokenSecret = 'your access Token Secret';

  twitter.statuses("update", tweet, accessToken, accessTokenSecret, function(error, data, response) {
    if(error) console.log('SENDTWEET ERROR: ' + error);
    else console.log('SENDETWEET SUCCESS: ' + tweet + " | " + info.response); 
  });
}


The consumer keys / token and secrets can be obtained via de twitter developer https://apps.twitter.com/
Follow the instructions on their site to obtain your keys or follow this guide http://www.makeuseof.com/tag/how-to-build-a-raspberry-pi-twitter-bot/
I used the section "Registering a Twitter app" to generate my keys.

Step 4:
Add an tweet event to metric.js for sending your tweet. You can change this in the way you want by following Felix's instructions on metric.js
I wanted to have my mote to send a tweet after 5 minutes of not reporting in.
Actually the way i programmed it now is that the Gateway is sending a tweet after every 30 seconds.
This piece of code is still a work in progress but you will have an idea to add twitter functionality to your PiGateway

exports.events = {
  TWEET : { label:'Tweet Error', icon:'alert', descr:'Send tweet when no update is received for more then 5 minutes',
	serverExecute:function(node) { 
		if (Date.now() - new Date(node.metrics['RED'].updated).getTime() < 30000)
			{ sendTweet('NO UPDATE RECEIVED FOR MORE THEN 5MINUTES ON NODE: [' + node._id + ':' + node.label + '] @ ' + (new Date().toLocaleTimeString() + (new Date().getHours() > 12 ? 'PM':'AM'))); 
		}; 
	} 
  }
};


Step 5:
The latest step is to add the event to your mote via the PiGateway webinterface.
If you want more info on this look for the youtube files about how the webinterface works on felix's site lowpowerlab.com

Greetz Jorg
The best way to get something done is to begin

jorgmuskens

If somebody has some ideas around my step 4 to check for mote that do not report in you can leave me a pm or reply on topic
Any help is appreciated!
The best way to get something done is to begin