Skip to content

Instantly share code, notes, and snippets.

@leosilvadev
Last active September 20, 2017 13:39
Show Gist options
  • Save leosilvadev/b6e61fd6aadb5d3bc1e417802f3092fb to your computer and use it in GitHub Desktop.
Save leosilvadev/b6e61fd6aadb5d3bc1e417802f3092fb to your computer and use it in GitHub Desktop.
package com.finleap.bot.model;
import java.util.HashMap;
import java.util.Map;
import com.finleap.bot.service.BotResultPublisher;
import com.finleap.core.model.converstion.MessageType;
import com.finleap.resources.VerimiResource;
import com.finleap.service.StateUpdator;
public class VerimiBot extends Bot {
private static final String URL = "https://verimi.com/dipp/api/oauth/authorize/?response_type=code&client_id=DB&scope=login&redirect_uri=%s";
public VerimiBot(final String conversationId, final BotResultPublisher publisher,
final StateUpdator stateUpdator) {
super(conversationId, publisher, stateUpdator);
}
@Override
public void onAssign() {
final String redirectUrl = "http://4fdbac4f.eu.ngrok.io/app/api/v1/verimi";
final String url = String.format(URL, redirectUrl);
VerimiResource.latestConversationId = getConversationId();
publisher.publishReplyMessage(buildChannelMessage("Please click on the following link to verify yourself for the account opening process:", MessageType.BOT));
publisher.publishReplyMessage(buildChannelMessage(url, MessageType.BOT));
}
public static BotDescription getDescription() {
final Map<String, String> welcomeDescription = new HashMap<>();
welcomeDescription.put("en", "Verimi Bot.");
welcomeDescription.put("de", "Verimi Bot.");
return BotDescription.builder()
.withName(VerimiBot.class.getName())
.withDisplayName("Verimi Bot")
.withType(BotType.INTERNAL)
.withBehaviorType(BotBehaviorType.PASSIVE)
.withDescription(welcomeDescription)
.build();
}
@Override
public BotBehaviorType getBotBehaviorType() {
return getDescription().getBehaviorType();
}
}
package com.finleap.resources;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.auth0.jwt.JWT;
import com.finleap.bot.model.VerimiBot;
import com.finleap.bot.service.BotManager;
import com.finleap.bot.service.BotOrchestrator;
import com.finleap.bot.service.BotResultPublisher;
import com.finleap.core.model.converstion.ChannelMessage;
import com.finleap.core.model.converstion.MessageType;
import com.google.inject.Inject;
@Path("/api/v1/verimi")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_HTML)
public class VerimiResource {
private static final Logger LOGGER = LoggerFactory.getLogger(VerimiResource.class);
private static final String URL = "https://verimi.com/dipp/api/oauth/token?grant_type=authorization_code&code=%s&redirect_uri=%s";
private static final String URL_BASKET = "https://verimi.com/dipp/api/query/baskets";
private final BotResultPublisher publisher;
private final BotManager botManager;
private final Client client;
public static String latestConversationId = "";
private static Map<String, ?> lastBasket = null;
private final String HTML =
"<html>" +
"<body>" +
"<div><h1>Thank you for using Verimi</h1></div>" +
"<div><a href=\"fb-messages://user-thread/\"></a>" +
"</body>" +
"</html>";
private final String HTML_AGENT =
"<html>" +
"<body>" +
"<div><table>%s</table></div>" +
"</body>" +
"</html>";
@Inject
public VerimiResource(final BotResultPublisher publisher, final BotManager botManager) {
this.publisher = publisher;
this.botManager = botManager;
this.client = ClientBuilder.newClient();
}
@GET
@Path("/")
public Response get(@QueryParam("code") final String code) {
try {
String uri = String.format(URL, code, "http://4fdbac4f.eu.ngrok.io/app/api/v1/verimi");
String auth = "Basic " + Base64.encodeBase64String("DB:G|41|0an18ZIs_w".getBytes());
Map<String, ?> responseBody = client.target(uri).request().header("Authorization", auth).post(Entity.json(new HashMap<>()), Map.class);
Map<String, String> result = new HashMap<>();
result.put("scope", responseBody.get("scope").toString());
result.put("expiresIn", responseBody.get("expires_in").toString());
result.put("refreshToken", new String(Base64.decodeBase64(responseBody.get("refresh_token").toString())));
result.put("acessToken", JWT.decode(responseBody.get("access_token").toString()).getId());
lastBasket = client.target(URL_BASKET).request().get(Map.class);
List<Map<String, List<Map<String, String>>>> dataScopes = (List<Map<String, List<Map<String, String>>>>) lastBasket.get("dataScopes");
final ChannelMessage channelMessage = new ChannelMessage();
//FIXME(sergey & leonardogomesdasilve) this is a quick fix, for id problem.
final List<Map<String, String>> data = dataScopes.get(3).get("data");
channelMessage.setId(UUID.randomUUID().toString());
channelMessage.setContent("Hello " + data.get(0).get("value") + " " + data.get(1).get("value"));
channelMessage.setSender(VerimiBot.getDescription().getName());
channelMessage.setType(MessageType.BOT);
channelMessage.setConversationId(latestConversationId);
publisher.publishReplyMessage(channelMessage);
botManager.unassignAndEnqueueWhenHasNoActiveBots(latestConversationId, VerimiBot.getDescription().getName());
} catch(Exception ex) {
LOGGER.error(ex.getMessage(), ex);
}
return Response.ok(HTML).build();
}
@GET
@Path("/details")
public Response getDetails() {
if (null != lastBasket) {
StringBuilder tableBuilder = new StringBuilder();
List<Map<String, String>> finalData = new ArrayList<>();
List<Map<String, List<Map<String, String>>>> dataScopes = (List<Map<String, List<Map<String, String>>>>) lastBasket.get("dataScopes");
dataScopes.forEach(data -> {
finalData.addAll(data.get("data"));
});
finalData.forEach(data -> {
data.forEach((key, value) -> {
if ("name".equals(key)) {
tableBuilder.append("<tr>");
tableBuilder.append("<td>" + value + ": </td>");
} else {
tableBuilder.append("<td>" + value + "</td>");
tableBuilder.append("</tr>");
}
});
});
String html = String.format(HTML_AGENT, tableBuilder.toString());
return Response.ok(html).build();
}
String html = String.format(HTML_AGENT, "<h1>Customer is not authorized yet.</h1>");
return Response.ok(html).build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment