socialMediaJava/ 0040750 0000000 0000000 00000000000 14034111546 014426 5 ustar 00root root 0000000 0000000 socialMediaJava/SocialMedia/ 0040750 0000000 0000000 00000000000 14034111546 016600 5 ustar 00root root 0000000 0000000 socialMediaJava/SocialMedia/src/ 0040750 0000000 0000000 00000000000 14034111546 017367 5 ustar 00root root 0000000 0000000 socialMediaJava/SocialMedia/src/main/ 0040750 0000000 0000000 00000000000 14034111546 020313 5 ustar 00root root 0000000 0000000 socialMediaJava/SocialMedia/src/main/java/ 0040750 0000000 0000000 00000000000 14034111546 021234 5 ustar 00root root 0000000 0000000 socialMediaJava/SocialMedia/src/main/java/org/ 0040750 0000000 0000000 00000000000 14034111546 022023 5 ustar 00root root 0000000 0000000 socialMediaJava/SocialMedia/src/main/java/org/sap/ 0040750 0000000 0000000 00000000000 14034111546 022606 5 ustar 00root root 0000000 0000000 socialMediaJava/SocialMedia/src/main/resources/ 0040750 0000000 0000000 00000000000 14034111546 022325 5 ustar 00root root 0000000 0000000 socialMediaJava/SocialMedia/src/main/resources/config/ 0040750 0000000 0000000 00000000000 14034111546 023572 5 ustar 00root root 0000000 0000000 socialMediaJava/SocialMedia/src/test/ 0040750 0000000 0000000 00000000000 14034111546 020346 5 ustar 00root root 0000000 0000000 socialMediaJava/SocialMedia/src/test/java/ 0040750 0000000 0000000 00000000000 14034111546 021267 5 ustar 00root root 0000000 0000000 socialMediaJava/SocialMedia/src/test/java/org/ 0040750 0000000 0000000 00000000000 14034111546 022056 5 ustar 00root root 0000000 0000000 socialMediaJava/SocialMedia/src/test/java/org/sap/ 0040750 0000000 0000000 00000000000 14034111546 022641 5 ustar 00root root 0000000 0000000 socialMediaJava/README.md 0100640 0000000 0000000 00000000761 14034111546 015707 0 ustar 00root root 0000000 0000000 # Social Media
This is a simple application that listens to tweets from a twitter account and routes tweets to a phone number using SAP Live Link 365 api.
## Setup
Have jdk and maven installed on your local.
### Build
To build, run with Maven using the command: `mvn package`.
### Execute
To execute, run as a plain jar:
`java -jar ./target/SocialMedia-jar-with-dependencies.jar`
All the other parameters that are in the config.properties can be replaced with an external -D configuration.
socialMediaJava/SocialMedia/pom.xml 0100640 0000000 0000000 00000004754 14034111546 020125 0 ustar 00root root 0000000 0000000
4.0.0
org.sap
SocialMedia
jar
1.0-SNAPSHOT
SocialMedia
http://maven.apache.org
junit
junit
3.8.1
test
org.twitter4j
twitter4j-core
4.0.4
org.twitter4j
twitter4j-stream
4.0.4
org.json
json
20160810
SocialMedia
org.apache.maven.plugins
maven-eclipse-plugin
2.9
true
false
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
${jdk.version}
org.apache.maven.plugins
maven-assembly-plugin
2.4.1
jar-with-dependencies
org.sap.App
make-assembly
package
single
socialMediaJava/SocialMedia/src/main/java/org/sap/App.java 0100640 0000000 0000000 00000012302 14034111546 024165 0 ustar 00root root 0000000 0000000 package org.sap;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import twitter4j.FilterQuery;
import twitter4j.StallWarning;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.TwitterException;
import org.sap.MessageHandler;
public class App {
private static String CONSUMER_KEY = "";
private static String CONSUMER_SECRET = "";
private static String TOKEN = "";
private static String TOKEN_SECRET = "";
private static long[] follower_ids = {};
private static String LIVELINK_APP_KEY = "";
private static String LIVELINK_APP_SECRET = "";
private static String BASE_URL = "";
static Properties config;
static MessageHandler handler;
public static void main(String[] args) throws TwitterException, IOException {
StatusListener listener = new StatusListener(){
public void onStatus(Status status) {
String message = status.getText();
ArrayList numbers = new ArrayList();
Enumeration enums = (Enumeration) config.propertyNames();
while (enums.hasMoreElements()) {
String key = enums.nextElement();
String value = config.getProperty(key);
if(message.contains(key)){
numbers.add(config.getProperty(key));
}
if(numbers.size() > 0){
for (int i = 0; i < numbers.size(); i++) {
try {
handler.sendSMS(message, numbers.get(i), handler.getAccessToken(LIVELINK_APP_KEY, LIVELINK_APP_SECRET));
} catch (IOException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
System.out.println(status.getUser().getName() + " : " + status.getText()+ " Tweeted AT: " + status.getCreatedAt());
}
@Override
public void onDeletionNotice(StatusDeletionNotice sdn) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onTrackLimitationNotice(int i) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onScrubGeo(long l, long l1) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onException(Exception excptn) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onStallWarning(StallWarning sw) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
};
loadProperties();
CONSUMER_KEY = config.getProperty("consumer_key");
CONSUMER_SECRET = config.getProperty("consumer_secret");
TOKEN = config.getProperty("access_token");
TOKEN_SECRET = config.getProperty("access_token_secret");
BASE_URL = config.getProperty("base_url");
LIVELINK_APP_KEY = config.getProperty("app_key");
LIVELINK_APP_SECRET = config.getProperty("app_secret");
handler = new MessageHandler(BASE_URL);
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey(CONSUMER_KEY);
cb.setOAuthConsumerSecret(CONSUMER_SECRET);
cb.setOAuthAccessToken(TOKEN);
cb.setOAuthAccessTokenSecret(TOKEN_SECRET);
TwitterStreamFactory tf = new TwitterStreamFactory(cb.build());
TwitterStream twitterStream = tf.getInstance();
twitterStream.addListener(listener);
FilterQuery query = new FilterQuery();
query.follow(follower_ids);
twitterStream.filter(query);
}
public static void loadProperties(){
config = new Properties();
InputStream input = null;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
input = loader.getResourceAsStream("config/config.properties");
// load a properties file
config.load(input);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
} socialMediaJava/SocialMedia/src/main/java/org/sap/MessageHandler.java 0100640 0000000 0000000 00000011372 14034111546 026335 0 ustar 00root root 0000000 0000000 package org.sap;
import org.json.*;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;
public class MessageHandler
{
private String baseURL;
public MessageHandler(String baseUrl) {
this.baseURL = baseUrl;
}
public String getAccessToken(String appKey, String appSecret) throws MalformedURLException, IOException
{
String token_url = this.baseURL + "/oauth/token";
//Encode your App Key and App Secret in Base64
String credentials = appKey + ":" + appSecret;
String credentialsEncoded = Base64.getEncoder().encodeToString(credentials.getBytes());
// System.out.println(credentialsEncoded);
String param = "grant_type=client_credentials";
String authTokenJsonString = this.postForAccessToken(token_url, param, credentialsEncoded);
JSONObject object = new JSONObject(authTokenJsonString);
//At this point, you will have retrieved your json string, which should be parsed.
//There are many third parties available for this.
//From the json string, extract the "access_token" attribute and return it in this function.
//return "access_token"; //Return the "access_token" field from the json string.
return object.getString("access_token");
}
public String postForAccessToken(String path, String data, String authenticationToken) throws MalformedURLException, IOException
{
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization", "Basic " + authenticationToken);
connection.setDoOutput(true);
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.writeBytes(data);
writer.flush();
writer.close();
InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = reader.readLine()) != null) response.append(inputLine);
reader.close();
return response.toString();
}
public void sendSMS(String message, String destination, String authenticationToken)
{
//Make sure to escape all double quotes inside the json, only the outer quotes for each string should be left unescaped.
// String postBody = "{" + " \"body\": \"" + message + "\"," //This is the string that will be shown to the user.
// + " \"destination\": \"" + destination + "\"," //Place here the destination number(s)
// + "}";
JSONObject postBody = new JSONObject()
.put("message", message)
.put("destination", destination);
//
String url = this.baseURL + "/v2/sms";
try
{
this.post(url, postBody.toString(), authenticationToken);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//This is a method that does an http post, given the path/url, data (a json string).
public String post(String path, String data, String authenticationToken) throws MalformedURLException, IOException
{
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
if (authenticationToken != null && !authenticationToken.isEmpty())
{
connection.setRequestProperty("Authorization", "Bearer " + authenticationToken);
}
System.out.println(authenticationToken);
connection.setDoOutput(true);
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.writeBytes(data);
writer.flush();
writer.close();
InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = reader.readLine()) != null) response.append(inputLine);
reader.close();
return response.toString();
}
} socialMediaJava/SocialMedia/src/main/resources/config/config.properties 0100640 0000000 0000000 00000000452 14034111546 027154 0 ustar 00root root 0000000 0000000 @john_doe= [2343]
#Insert your api url here
base_url=https://livelink.sapmobileservices.com/api
#Insert your generated app key here
app_key=appKey
#Insert your generated app secret here
app_secret=appSecret
consumer_key=ckey
consumer_secret=csecret
access_token=atoken
access_token_secret=asecret socialMediaJava/SocialMedia/src/test/java/org/sap/AppTest.java 0100640 0000000 0000000 00000001173 14034111546 025064 0 ustar 00root root 0000000 0000000 package org.sap;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}