Skip to content

Commit

Permalink
Merge pull request #23 from matallen/master
Browse files Browse the repository at this point in the history
Google Hangouts Chat notification support
  • Loading branch information
matallen authored Mar 14, 2019
2 parents 48ee85f + af7551e commit 64c262c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 6 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/redhat/sso/ninja/ChatNotification.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.redhat.sso.ninja;

import com.redhat.sso.ninja.utils.Http;
import com.redhat.sso.ninja.utils.Http.Response;
import com.redhat.sso.ninja.utils.MapBuilder;

public class ChatNotification{

public static void main(String[] asd){
Config c=Config.get();
System.out.println("googlehangoutschat.webhook.notifications.enabled="+c.getOptions().get("googlehangoutschat.webhook.notifications.enabled"));
System.out.println("googlehangoutschat.webhook.url="+c.getOptions().get("googlehangoutschat.webhook.url"));
System.out.println("googlehangoutschat.webhook.template="+c.getOptions().get("googlehangoutschat.webhook.template"));

new ChatNotification().send("test - please ignore");
}

public void send(String notificationText){
Config c=Config.get();
boolean enabled= "true".equalsIgnoreCase(c.getOptions().get("googlehangoutschat.webhook.notifications.enabled"));
if (enabled){
System.out.println("Sending notification...");
// https://developers.google.com/hangouts/chat/how-tos/webhooks
String url= c.getOptions().get("googlehangoutschat.webhook.url");
String template= c.getOptions().get("googlehangoutschat.webhook.template");
String googleHangoutsChatPayload=String.format(template, notificationText);

Response r=Http.post(url, googleHangoutsChatPayload, new MapBuilder<String, String>().put("Content-Type", "application/json; charset=UTF-8").build());
System.out.println("Response = "+r.getResponseCode());
}
}
}
1 change: 1 addition & 0 deletions src/main/java/com/redhat/sso/ninja/Database2.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public void addEvent(String type, String user, String text){
getEvents().remove(0);
}
}
// user is the target user: ie. fbloggs
public void addTask(String taskText, String user){
Map<String,String> task=new HashMap<String, String>();
task.put(TASK_FIELDS.TIMESTAMP.v, sdf2.format(new Date()));
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/redhat/sso/ninja/Heartbeat2.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.redhat.sso.ninja.Database2.TASK_FIELDS;
import com.redhat.sso.ninja.user.UserService;
import com.redhat.sso.ninja.user.UserService.User;
import com.redhat.sso.ninja.utils.DownloadFile;
Expand Down Expand Up @@ -206,6 +207,10 @@ public boolean addNewlyRegisteredUsers(Database2 db){

db.addEvent("New User Registered", userInfo.get("username"), "");

// Notify everyone on the Ninja chat group of a new registree
String displayName=userInfo.containsKey("displayName")?userInfo.get("displayName"):userInfo.get("username");
new ChatNotification().send("New User Registered: <https://mojo.redhat.com/people/"+userInfo.get("username")+"|"+displayName+">");

}else if (dbUsers.containsKey(userInfo.get("username"))){
log.debug("User already registered: "+userInfo.get("username"));
}
Expand Down Expand Up @@ -480,9 +485,13 @@ public void levelUpChecks(Database2 db){
userInfo.put("levelChanged", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));// nextLevel.getRight());

db.addEvent("User Promotion", userInfo.get("username"), "Promoted to "+nextLevel.getRight()+" level");
// db.getEvents().add("User Promotion: ["+userInfo.get("username") +"] was promoted to level ["+nextLevel.getRight()+"]");

db.addTask(userInfo.get("username")+" promoted to "+nextLevel.getRight()+" belt", userInfo.get("username"));
String displayName=userInfo.containsKey("displayName")?userInfo.get("displayName"):userInfo.get("username");
String title=displayName+" promoted to "+nextLevel.getRight()+" belt";
db.addTask(title, userInfo.get("username"));

// Notify everyone on the Ninja chat group of a new belt promotion
new ChatNotification().send(title);

count+=1;
}
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/redhat/sso/ninja/utils/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

Expand All @@ -30,20 +32,29 @@ public String getString(){
}

public static Response get(String url){
return http("GET", url, null);
return http("GET", url, null, null);
}

public static Response post(String url, String data){
return http("POST", url, data);
return http("POST", url, data, null);
}
public static Response post(String url, String data, Map<String,String> headers){
return http("POST", url, data, headers);
}

public static synchronized Response http(String method, String url, String data){
public static synchronized Response http(String method, String url, String data, Map<String,String> headers){
try {
// log.info("Http call '"+method+"' to '"+url+"'"+(null!=data?" (with data length of "+data.length()+" characters)":""));
URL obj=new URL(url);
HttpURLConnection cnn=(HttpURLConnection)obj.openConnection();
cnn.setRequestMethod(method.toUpperCase());

if (headers!=null){
for(Entry<String, String> e:headers.entrySet()){
cnn.setRequestProperty(e.getKey(), e.getValue());
}
}

if ("POST".equalsIgnoreCase(method) && null!=data){
cnn.setDoOutput(true);
OutputStream os = cnn.getOutputStream();
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
"ldap.enabled": "true",
"_users.ldap.provider": "ldap://server.example.com:123",
"users.ldap.baseDN": "ou=users,dc=redhat,dc=com",
"users.ldap.searchDN": "(&(objectclass=Person)(%s=%s))"
"users.ldap.searchDN": "(&(objectclass=Person)(%s=%s))",
"googlehangoutschat.webhook.notifications.enabled": "false",
"googlehangoutschat.webhook.template": "{\"text\":\"%s\"}",
"googlehangoutschat.webhook.url": ""
},
"values": {
"lastRun2": "2019-03-01T00:00:00"
Expand Down
File renamed without changes.

0 comments on commit 64c262c

Please sign in to comment.