Skip to content
This repository has been archived by the owner on May 23, 2018. It is now read-only.

Find People #2

Merged
merged 17 commits into from
Nov 2, 2017
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ dependencies {
compile('com.linecorp.bot:line-bot-model:1.10.0')
compile('com.linecorp.bot:line-bot-api-client:1.10.0')
compile('com.linecorp.bot:line-bot-spring-boot:1.10.0')
compile('org.jsoup:jsoup:1.10.3')
compile('me.ghui:Fruit:1.0.4')
runtime('org.springframework.boot:spring-boot-devtools')
// testCompile('org.springframework.boot:spring-boot-starter-test')

Expand Down
149 changes: 107 additions & 42 deletions src/main/java/com/comp3111/chatbot/CallbackController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -68,6 +70,9 @@
public class CallbackController {
@Autowired
private LineMessagingClient lineMessagingClient;
// For People
private static int number = 0;
// For OpeningHours
private static int tag = 0;

@EventMapping
Expand Down Expand Up @@ -221,9 +226,58 @@ private void handleTextContent(String replyToken, Event event, TextMessageConten
throws Exception {

String text = content.getText();

text=text.toLowerCase();

log.info("Got text message from {}: {}", replyToken, text);

if (number==1) { // for finding people
String replyPeople;

URLConnectionReader search = new URLConnectionReader();
PeopleList result=search.SearchPeople(text);
ArrayList<people> resultList = result.getList();

StringBuilder results = new StringBuilder();
results.append("Search Result(s):");

if (resultList ==null) {
results.append("\nNot found.");
this.replyText(replyToken, results.toString());
return;
}
else{
for (people p : resultList){
results.append("\n");
results.append("Title: ");
results.append(p.getTitle());
results.append("\n");
results.append("Name: ");
results.append(p.getName());
results.append("\n");
results.append("Email: ");
results.append(p.getEmail());
results.append("\n");
results.append("Phone: ");
results.append(p.getPhone());
results.append("\n");
results.append("Department: ");
results.append(p.getDepartment());
results.append("\n");
results.append("Room: ");
results.append(p.getRoom());
results.append("\n");
}
}

if (PeopleList.too_many==true) {
results.append("Too many results...");
}
replyPeople = results.toString();
this.replyText(replyToken, replyPeople);
number=0;
return;
}

if(tag == 'b')
{
String reply = null;
Expand All @@ -238,6 +292,8 @@ private void handleTextContent(String replyToken, Event event, TextMessageConten
reply
);
}

String reply = "";
switch (text) {
case "profile": {
String userId = event.getSource().getUserId();
Expand Down Expand Up @@ -410,54 +466,63 @@ private void handleTextContent(String replyToken, Event event, TextMessageConten
)
));
break;

case "b": //provide facilities time
String reply = "Please enter the number in front of the facilities to query the opening hour:\n";
try {
reply += database.showFacilitiesChoices();
} catch (Exception e) {
reply = "Exception occur";
}
reply = "Please enter the number in front of the facilities to query the opening hour:\n";
try {
reply += database.showFacilitiesChoices();
} catch (Exception e) {
reply = "Exception occur";
}
log.info("Returns echo message {}: {}", replyToken, reply);
this.replyText(
replyToken,
reply
replyToken,
reply
);
tag = 'b';
break;

tag = 'b';
break;
case "c": // suggestedLinks
String reply ="Which link do you want to find?\n"
+"1) Register for a locker\n"
+"2) Register for courses\n"
+"3) Check grades\n"
+"4) Find school calendar\n"
+"5) Book library rooms\n"
+"6) Find lecture materials\n";

this.replyText(
replyToken,
reply
);
// get input and search for links
break;

reply ="Which link do you want to find?\n"
+"1) Register for a locker\n"
+"2) Register for courses\n"
+"3) Check grades\n"
+"4) Find school calendar\n"
+"5) Book library rooms\n"
+"6) Find lecture materials\n";

this.replyText(
replyToken,
reply
);
// get input and search for links
break;

case"d": //find people
reply ="Who do you want to find? Please enter his/her full name or ITSC.";
this.replyText(
replyToken,
reply
);
number=1; // get input and search for name
break;

default:
String default_reply ="Which information do you want to know?\n"
+"a) Course information\n"
+"b) Restaurant/Facilities opening hours\n"
+"c) Links suggestions\n"
+"d) Find people\n"
+"e) Lift advisor\n"
+"f) Bus arrival/Departure time\n"
+"g) Deadline list\n"
+"h) Set notifications\n";
log.info("Returns message {}: {}", replyToken, default_reply);
this.replyText(
replyToken,
default_reply
);
break;
+"a) Course information\n"
+"b) Restaurant/Facilities opening hours\n"
+"c) Links suggestions\n"
+"d) Find people\n"
+"e) Lift advisor\n"
+"f) Bus arrival/Departure time\n"
+"g) Deadline list\n"
+"h) Set notifications\n";
log.info("Returns message {}: {}", replyToken, default_reply);
this.replyText(
replyToken,
default_reply
);
break;
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/comp3111/chatbot/PeopleList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.comp3111.chatbot;

import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;


@Slf4j
public class PeopleList {
private ArrayList <people> people;
public static boolean too_many=false;

public PeopleList () {
this.people=null;
}

public void addPeople (String p0, String p1, String p2, String p3, String p4, String p5) {
people p = new people(p0, p1, p2, p3, p4, p5);
if (this.people==null) {
this.people=new ArrayList<people>();
}
this.people.add(p);
}

public ArrayList<people> getList(){
return this.people;
}

}
64 changes: 64 additions & 0 deletions src/main/java/com/comp3111/chatbot/URLConnectionReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.comp3111.chatbot;

import java.net.*;
import java.io.*;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import lombok.extern.slf4j.Slf4j;


@Slf4j
public class URLConnectionReader {
public PeopleList SearchPeople(String args) throws Exception {
String s1=args.replace(" ", "%20");
Document doc = Jsoup.connect("http://www.ust.hk/search/"+s1+"/?sopt=people").get();
//Document doc = Jsoup.connect("http://www.ust.hk/search/a%20b%20c/?sopt=people").get();
Element content = doc.getElementById("p-s-table");
Elements inputElements = content.getElementsByTag("td");

String [] values = new String [42];
PeopleList l = new PeopleList();

int i=0;

for(Element td : inputElements) {
if (i<42) {
values[i] = td.text();
if (values[i].equals("No result found matching your search criteria."))
return l;
//log.info("values {} = {}", i, values[i]);
i++;
}
else
{
PeopleList.too_many=true;
log.info("\nToo many results");
break;
}
}

int j =0;
while(j<i) {
log.info("Values {} = {}",j, values[j]);
log.info("Values {} = {}",j+1, values[j+1]);
log.info("Values {} = {}",j+2, values[j+2]);
log.info("Values {} = {}",j+3, values[j+3]);
log.info("Values {} = {}",j+4, values[j+4]);
log.info("Values {} = {}",j+5, values[j+5]);

l.addPeople(values[j], values[j+1], values[j+2], values[+3],
values[j+4], values[j+5]);

j=j+6;
}

return l;

}

}
57 changes: 57 additions & 0 deletions src/main/java/com/comp3111/chatbot/people.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.comp3111.chatbot;

import lombok.extern.slf4j.Slf4j;


@Slf4j
public class people{
private String title;
private String name;
private String email;
private String phone;
private String department;
private String room;

public people( String title, String name, String email, String phone,
String department, String room){
this.title=title;
this.name=name;
this.email=email;
this.phone=phone;
this.department=department;
this.room=room;

}

public String getTitle() {
if (this.title.equals(""))
return "No info";
return this.title;
}

public String getName() {
if (this.name.equals(""))
return "No info";
return this.name;
}
public String getEmail() {
if (this.email.equals(""))
return "No info";
return this.email;
}
public String getPhone() {
if (this.phone.equals(""))
return "No info";
return this.phone;
}
public String getDepartment() {
if (this.department.equals(""))
return "No info";
return this.department;
}
public String getRoom() {
if (this.room.equals(""))
return "No info";
return this.room;
}
}