Skip to content

Instantly share code, notes, and snippets.

@jesusnoseq
Last active December 14, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesusnoseq/5076356 to your computer and use it in GitHub Desktop.
Save jesusnoseq/5076356 to your computer and use it in GitHub Desktop.
A little tool to read a direct messages from twitter and switch off the computer
import java.io.IOException;
import twitter4j.DirectMessage;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.UserStreamAdapter;
import twitter4j.conf.ConfigurationBuilder;
public class SwitchOffByTweet {
static final String OAuthConsumerKey="Your Consumer key";
static final String OAuthConsumerSecret="Your Consumer secret";
static final String OAuthAccessToken="Your Access token";
static final String OAuthAccessTokenSecret="Your Access token secret";
static final String USERNAME="Respetando mayusculas y minusculas"; //Ej: Jesusnoseq
// https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/stream/PrintUserStream.java
static UserStreamAdapter listener = new UserStreamAdapter() {
@Override
public void onDirectMessage(DirectMessage directMessage) {
String from=directMessage.getSender().getScreenName();
String text=directMessage.getText();
//System.out.println("onDirectMessage text:"+from+":"+text );
if(from.equals(USERNAME) && text.toLowerCase().startsWith("apagar") ){
System.out.println("Apagando...");
apagar();
}
}
};
public static void main(String[] args) throws TwitterException {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(OAuthConsumerKey)
.setOAuthConsumerSecret(OAuthConsumerSecret)
.setOAuthAccessToken(OAuthAccessToken)
.setOAuthAccessTokenSecret(OAuthAccessTokenSecret);
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
twitterStream.addListener(listener);
// user() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
twitterStream.user();
}
public static void apagar() {
// http://lineadecodigo.com/java/como-ejecutar-un-comando-del-sistema-desde-java/
// Para saber en SO se ejecuta el programa
//System.out.println(System.getProperty("os.name"));
// Comando de apagado en linux
//String cmd = "halt";
// Comando de apagado en windows
String[] cmd = { "shutdown", "-s", "-t", "10" };
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment