Skip to content

Commit

Permalink
APP-3078 Update examples (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
thibauult authored Oct 6, 2020
1 parent 6559b5e commit 6e5d3b6
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 178 deletions.
33 changes: 19 additions & 14 deletions generators/bdk/java/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const axios = require('axios')
const BASE_JAVA = 'src/main/java';
const BASE_RESOURCES = 'src/main/resources';

const BDK_VERSION_DEFAULT = '1.2.1.BETA';
const BDK_VERSION_DEFAULT = '1.3.2.BETA';
const SPRING_VERSION_DEFAULT = '2.3.4.RELEASE'
const MAVEN_SEARCH_BASE = 'https://search.maven.org/solrsearch/select?q=g:com.symphony.platformsolutions+AND+a:';
const MAVEN_BDK_BOM_SEARCH_BASE = 'https://search.maven.org/solrsearch/select?q=g:com.symphony.platformsolutions+AND+a:';
const MAVEN_SPRING_BOOT_SEARCH_BASE = 'https://search.maven.org/solrsearch/select?q=g:org.springframework.boot';

module.exports = class extends Generator {
Expand Down Expand Up @@ -75,7 +75,7 @@ module.exports = class extends Generator {

// get latest BDK BOM version
try {
const mavenResponse = await axios.get(MAVEN_SEARCH_BASE + 'symphony-bdk-bom');
const mavenResponse = await axios.get(MAVEN_BDK_BOM_SEARCH_BASE + 'symphony-bdk-bom');
this.answers.bdkBomVersion = mavenResponse.data['response']['docs'][0]['latestVersion'];
this.log('Latest BDK version is '.green.bold + `${this.answers.bdkBomVersion}`.white.bold);
} catch (error) {
Expand Down Expand Up @@ -109,35 +109,40 @@ module.exports = class extends Generator {
this.answers.springBootVersion = mavenResponse.data['response']['docs'][0]['latestVersion'];
} catch (error) {
this.log(`\u26A0 Cannot retrieve latest Spring Boot Starter version from Maven Central. Default: ${SPRING_VERSION_DEFAULT}`.grey);
this.answers.bdkBomVersion = SPRING_VERSION_DEFAULT;
this.answers.springBootVersion = SPRING_VERSION_DEFAULT;
}

// process and copy application.yaml file
this.fs.copyTpl(
this.templatePath(path.join(this.answers.framework, 'application.yaml.ejs')),
this.destinationPath(path.join(BASE_RESOURCES, 'application.yaml')),
this.answers
)
);

break;
}

// process and copy template file
this.fs.copyTpl(
this.templatePath('gif.ftl'),
this.destinationPath(path.join(BASE_RESOURCES, "templates", "gif.ftl"))
)
this.destinationPath(path.join(BASE_RESOURCES, 'templates', 'gif.ftl'))
);
this.fs.copyTpl(
this.templatePath('welcome.ftl'),
this.destinationPath(path.join(BASE_RESOURCES, 'templates', 'welcome.ftl'))
);

// Process Java file
this._copyJavaTemplate(path.join(this.answers.framework, BASE_JAVA), basePackage);
// Process Framework specific files
this._copyJavaTemplate(this.answers.framework, basePackage);
// Process Common files
this._copyJavaTemplate('common', basePackage);

// Process build files
if (this.answers.build === 'Gradle') {
this._processGradleFiles()
this._processGradleFiles();
} else {
this._processMavenFiles()
this._processMavenFiles();
}

}

/**
Expand Down Expand Up @@ -167,13 +172,13 @@ module.exports = class extends Generator {

_copyJavaTemplate(dirPath, basePackage) {
let files = fs.readdirSync(path.join(__dirname, 'templates', dirPath))
files.forEach(file => {
files.filter(f => f.endsWith('java.ejs')).forEach(file => {
this.fs.copyTpl(
this.templatePath(path.join(dirPath, file)),
this.destinationPath(path.join(BASE_JAVA, basePackage, file.substr(0,file.length - 4))),
this.answers
);
})
});
}

_processGradleFiles() {
Expand Down
20 changes: 12 additions & 8 deletions generators/bdk/java/templates/build.gradle.ejs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
plugins {
id 'java-library'
<% if (framework == 'java') { %>
id 'application'
<% if (framework == 'spring') { %>id 'org.springframework.boot' version "<%= springBootVersion %>"<% } %>
<% } %>
<% if (framework == 'spring') { %>
id 'org.springframework.boot' version "<%= springBootVersion %>"
<% } %>
}

repositories {
Expand All @@ -12,23 +16,23 @@ dependencies {

implementation platform('com.symphony.platformsolutions:symphony-bdk-bom:<%= bdkBomVersion %>')

// define dependencies without versions
<% if (framework == 'java') { %>
implementation 'com.symphony.platformsolutions:symphony-bdk-core'
implementation 'com.symphony.platformsolutions:symphony-bdk-http-api'
implementation 'com.symphony.platformsolutions:symphony-bdk-template-api'
runtimeOnly 'com.symphony.platformsolutions:symphony-bdk-template-freemarker'
runtimeOnly 'com.symphony.platformsolutions:symphony-bdk-http-jersey2'
<% } else if (framework == 'spring') { %>
implementation 'com.symphony.platformsolutions:symphony-bdk-core-spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter'
<% } %>

implementation 'org.springframework.boot:spring-boot-starter'<% } %>
implementation 'ch.qos.logback:logback-classic'
implementation 'org.slf4j:slf4j-api'
runtimeOnly 'ch.qos.logback:logback-classic'
}

<% if (framework == 'java') { %>
application {
mainClass = '<%= basePackage %>.BotApplication'
}
<% } %>

version = '1.0.0-SNAPSHOT'
version = '0.0.1-SNAPSHOT'
40 changes: 40 additions & 0 deletions generators/bdk/java/templates/common/GifFormActivity.java.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package <%= basePackage %>;

import com.symphony.bdk.core.activity.ActivityMatcher;
import com.symphony.bdk.core.activity.form.FormReplyActivity;
import com.symphony.bdk.core.activity.form.FormReplyContext;
import com.symphony.bdk.core.activity.model.ActivityInfo;
import com.symphony.bdk.core.activity.model.ActivityType;
import com.symphony.bdk.core.service.MessageService;
<% if (framework == 'spring') { %>
import org.springframework.stereotype.Component;
@Component<% } %>
public class GifFormActivity extends FormReplyActivity<%-'<'%>FormReplyContext<%-'>'%> {

private final MessageService messageService;

public GifFormActivity(MessageService messageService) {
this.messageService = messageService;
}

@Override
public ActivityMatcher<%-'<'%>FormReplyContext<%-'>'%> matcher() {
return context -> "gif-category-form".equals(context.getFormId())
&& "submit".equals(context.getFormValue("action"));
}

@Override
public void onActivity(FormReplyContext context) {
final String category = context.getFormValue("category");
final String message = "<%-'<'%>messageML<%-'>'%>Received category '" + category + "'<%-'<'%>/messageML<%-'>'%>";
this.messageService.send(context.getSourceEvent().getStream(), message);
}

@Override
protected ActivityInfo info() {
return new ActivityInfo().type(ActivityType.FORM)
.name("Gif Display category form command")
.description("\"Form handler for the Gif Category form\"");
}
}
58 changes: 58 additions & 0 deletions generators/bdk/java/templates/java/BotApplication.java.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package <%= basePackage %>;

import com.symphony.bdk.core.SymphonyBdk;
import com.symphony.bdk.template.api.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.symphony.bdk.gen.api.model.V4UserJoinedRoom;
import com.symphony.bdk.gen.api.model.V4Initiator;
import com.symphony.bdk.core.service.datafeed.RealTimeEventListener;
import static com.symphony.bdk.core.config.BdkConfigLoader.loadFromClasspath;
import static com.symphony.bdk.core.activity.command.SlashCommand.slash;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;

/**
* Simple Bot Application.
*/
public class BotApplication {

/** The Logger */
private static final Logger log = LoggerFactory.getLogger(BotApplication.class);

public static void main(String[] args) throws Exception {

// Initialize BDK entry point
final SymphonyBdk bdk = new SymphonyBdk(loadFromClasspath("/config.yaml"));

// Register a "slash" activity
bdk.activities().register(slash("/gif", false, context -> {
try {
bdk.messages().send(context.getStreamId(), "/templates/gif.ftl", emptyMap());
} catch (TemplateException e) {
log.error("Unable to load template", e);
}
}));

// Register a "formReply" activity that handles the Gif category form submission
bdk.activities().register(new GifFormActivity(bdk.messages()));

// Subscribe to 'onUserJoinedRoom' Real Time Event
bdk.datafeed().subscribe(new RealTimeEventListener() {

@Override
public void onUserJoinedRoom(V4Initiator initiator, V4UserJoinedRoom event) {
final String userDisplayName = event.getAffectedUser().getDisplayName();
try {
bdk.messages().send(event.getStream(), "/templates/welcome.ftl", singletonMap("name", userDisplayName));
} catch (TemplateException e) {
log.error("Unable to load template", e);
}
}
});

// finally, start the datafeed read loop
bdk.datafeed().start();
}
}

This file was deleted.

23 changes: 9 additions & 14 deletions generators/bdk/java/templates/pom.xml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId><%= groupId %></groupId>
<artifactId><%= artifactId %></artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -32,23 +32,23 @@
</dependency>
<dependency>
<groupId>com.symphony.platformsolutions</groupId>
<artifactId>symphony-bdk-http-api</artifactId>
<artifactId>symphony-bdk-template-freemarker</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.symphony.platformsolutions</groupId>
<artifactId>symphony-bdk-template-api</artifactId>
<artifactId>symphony-bdk-http-jersey2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.symphony.platformsolutions</groupId>
<artifactId>symphony-bdk-template-freemarker</artifactId>
<scope>runtime</scope>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.symphony.platformsolutions</groupId>
<artifactId>symphony-bdk-http-jersey2</artifactId>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<% } else if (framework == 'spring') { %>
<dependency>
<groupId>com.symphony.platformsolutions</groupId>
Expand All @@ -59,11 +59,6 @@
<artifactId>spring-boot-starter</artifactId>
</dependency>
<% } %>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
25 changes: 25 additions & 0 deletions generators/bdk/java/templates/spring/GifSlashHandler.java.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package <%= basePackage %>;

import static java.util.Collections.emptyMap;

import com.symphony.bdk.core.activity.command.CommandContext;
import com.symphony.bdk.core.service.MessageService;
import com.symphony.bdk.spring.annotation.Slash;
import com.symphony.bdk.template.api.TemplateException;

import org.springframework.stereotype.Component;

@Component
public class GifSlashHandler {

private final MessageService messageService;

public GifSlashHandler(MessageService messageService) {
this.messageService = messageService;
}

@Slash(value = "/gif", mentionBot = false)
public void onSlashGif(CommandContext context) throws TemplateException {
this.messageService.send(context.getStreamId(), "/templates/gif.ftl", emptyMap());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package <%= basePackage %>;

import com.symphony.bdk.core.service.MessageService;
import com.symphony.bdk.core.service.datafeed.RealTimeEventListener;
import com.symphony.bdk.gen.api.model.V4Initiator;
import com.symphony.bdk.gen.api.model.V4UserJoinedRoom;
import com.symphony.bdk.template.api.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static java.util.Collections.singletonMap;

@Component
public class OnUserJoinedRoomListener implements RealTimeEventListener {

private static final Logger log = LoggerFactory.getLogger(OnUserJoinedRoomListener.class);

private final MessageService messageService;

@Autowired
public OnUserJoinedRoomListener(MessageService messageService) {
this.messageService = messageService;
}

@Override
public void onUserJoinedRoom(V4Initiator initiator, V4UserJoinedRoom event) {
final String userDisplayName = event.getAffectedUser().getDisplayName();
try {
this.messageService.send(event.getStream(), "/templates/welcome.ftl", singletonMap("name", userDisplayName));
} catch (TemplateException e) {
log.error("Unable to load template", e);
}
}
}
Loading

0 comments on commit 6e5d3b6

Please sign in to comment.