-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#3403] Rasa Connector #3611
Merged
Merged
[#3403] Rasa Connector #3611
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
51e2482
Initialized connector structure and skeleton
armanjindal c41b920
Reading from kafka stream and logging
armanjindal 3d1506d
OpenFeign dependency imported
armanjindal bc29581
Called Rasa REST API succesfully
armanjindal 808c879
Decode Rasa REST API succesfully
armanjindal 7cbeb10
Import async library and used in connector
armanjindal 65fd579
Basic connector working
armanjindal 21b3a84
Added health check and removed api auth token code
armanjindal ffc5115
rebase and update application.properties working
armanjindal 54ba6ed
Handle multiple responses from Rasa
armanjindal 3fa86db
Message Handler test written
armanjindal 60b5545
Rasa Connector Service Test completed
armanjindal 20e3538
Lint fix
armanjindal 46183c9
Test clean up and code improvements
armanjindal 9571a04
Test fixes
armanjindal 1db04f1
Draft Helm files
armanjindal 2f398d0
Test Helm pushed to test repository
armanjindal 2c2972a
removed un-used dependencies
armanjindal 41a7ea8
Improve Helm packaging and fix app deps
armanjindal 9c1f3e7
Dependency fix
armanjindal e0e3cbf
Image rendering in Airy Chat plugin
armanjindal 59e1070
Delete unused test
armanjindal b684ccd
incorporate feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
load("//tools/build:springboot.bzl", "springboot") | ||
load("//tools/build:junit5.bzl", "junit5") | ||
load("//tools/build:container_release.bzl", "container_release") | ||
load("@com_github_airyhq_bazel_tools//lint:buildifier.bzl", "check_pkg") | ||
|
||
check_pkg(name = "buildifier") | ||
|
||
app_deps = [ | ||
"//:spring", | ||
"//:springboot", | ||
"//:springboot_actuator", | ||
"//:jackson", | ||
"//:lombok", | ||
"//backend/model/message", | ||
"//backend/model/metadata", | ||
"//:feign", | ||
"//lib/java/log", | ||
"//lib/java/spring/kafka/core:spring-kafka-core", | ||
"//lib/java/spring/core:spring-core", | ||
"//lib/java/spring/kafka/streams:spring-kafka-streams", | ||
"//lib/java/spring/async:spring-async", | ||
] | ||
|
||
springboot( | ||
name = "rasa-connector", | ||
srcs = glob(["src/main/java/**/*.java"]), | ||
main_class = "co.airy.spring.core.AirySpringBootApplication", | ||
deps = app_deps, | ||
) | ||
|
||
[ | ||
junit5( | ||
size = "medium", | ||
file = file, | ||
resources = glob(["src/test/resources/**/*"]), | ||
deps = [ | ||
":app", | ||
"//backend:base_test", | ||
"//lib/java/test", | ||
"//lib/java/kafka/test:kafka-test", | ||
"//lib/java/spring/test:spring-test", | ||
] + app_deps, | ||
) | ||
for file in glob(["src/test/java/**/*Test.java"]) | ||
] | ||
|
||
container_release( | ||
registry = "ghcr.io/airyhq/connectors", | ||
repository = "rasa-connector", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
load("@rules_pkg//:pkg.bzl", "pkg_tar") | ||
load("@com_github_airyhq_bazel_tools//helm:helm.bzl", "helm_template_test") | ||
load("//tools/build:helm.bzl", "helm_push") | ||
|
||
filegroup( | ||
name = "files", | ||
srcs = glob( | ||
["**/*"], | ||
exclude = ["BUILD"], | ||
), | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
pkg_tar( | ||
name = "package", | ||
srcs = [":files"], | ||
extension = "tgz", | ||
strip_prefix = "./", | ||
) | ||
|
||
helm_template_test( | ||
name = "template", | ||
chart = ":package", | ||
) | ||
|
||
helm_push( | ||
chart = ":package", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
apiVersion: v2 | ||
appVersion: "1.0" | ||
description: A Helm chart for the Rasa connector | ||
name: rasa-connector | ||
version: 1.0 |
11 changes: 11 additions & 0 deletions
11
backend/connectors/rasa-connector/helm/templates/configmap.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: "{{ .Values.component }}" | ||
labels: | ||
core.airy.co/managed: "true" | ||
core.airy.co/mandatory: "{{ .Values.mandatory }}" | ||
core.airy.co/component: "{{ .Values.component }}" | ||
core.airy.co/enterprise: "false" | ||
annotations: | ||
core.airy.co/enabled: "{{ .Values.enabled }}" |
75 changes: 75 additions & 0 deletions
75
backend/connectors/rasa-connector/helm/templates/deployment.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: {{ .Values.component }} | ||
labels: | ||
app: {{ .Values.component }} | ||
core.airy.co/managed: "true" | ||
core.airy.co/mandatory: "{{ .Values.mandatory }}" | ||
core.airy.co/component: {{ .Values.component }} | ||
spec: | ||
replicas: {{ if .Values.enabled }} 1 {{ else }} 0 {{ end }} | ||
selector: | ||
matchLabels: | ||
app: {{ .Values.component }} | ||
strategy: | ||
rollingUpdate: | ||
maxSurge: 1 | ||
maxUnavailable: 1 | ||
type: RollingUpdate | ||
template: | ||
metadata: | ||
labels: | ||
app: {{ .Values.component }} | ||
spec: | ||
containers: | ||
- name: app | ||
image: "{{ .Values.global.containerRegistry}}/{{ .Values.image }}:{{ default .Chart.Version }}" | ||
imagePullPolicy: Always | ||
envFrom: | ||
- configMapRef: | ||
name: security | ||
- configMapRef: | ||
name: kafka-config | ||
env: | ||
- name: RASA_WEBHOOK_URL | ||
valueFrom: | ||
configMapKeyRef: | ||
key: rasaWebhookUrl | ||
name: {{ .Values.component }} | ||
- name: SERVICE_NAME | ||
value: {{ .Values.component }} | ||
- name: POD_NAMESPACE | ||
valueFrom: | ||
fieldRef: | ||
apiVersion: v1 | ||
fieldPath: metadata.namespace | ||
- name: REQUESTED_CPU | ||
valueFrom: | ||
resourceFieldRef: | ||
containerName: app | ||
resource: requests.cpu | ||
- name: LIMIT_CPU | ||
valueFrom: | ||
resourceFieldRef: | ||
containerName: app | ||
resource: limits.cpu | ||
- name: LIMIT_MEMORY | ||
valueFrom: | ||
resourceFieldRef: | ||
containerName: app | ||
resource: limits.memory | ||
livenessProbe: | ||
httpGet: | ||
path: /actuator/health | ||
port: 8080 | ||
httpHeaders: | ||
- name: Health-Check | ||
value: health-check | ||
initialDelaySeconds: 60 | ||
periodSeconds: 10 | ||
failureThreshold: 3 | ||
volumes: | ||
- name: {{ .Values.component }} | ||
configMap: | ||
name: {{ .Values.component }} |
14 changes: 14 additions & 0 deletions
14
backend/connectors/rasa-connector/helm/templates/service.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
labels: | ||
app: {{ .Values.component }} | ||
name: {{ .Values.component }} | ||
spec: | ||
ports: | ||
- port: 80 | ||
protocol: TCP | ||
targetPort: 8080 | ||
selector: | ||
app: {{ .Values.component }} | ||
type: ClusterIP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
component: rasa-connector | ||
mandatory: false | ||
enabled: false | ||
image: connectors/rasa-connector | ||
global: | ||
containerRegistry: ghcr.io/airyhq | ||
resources: {} |
89 changes: 89 additions & 0 deletions
89
...d/connectors/rasa-connector/src/main/java/co/airy/core/rasa_connector/MessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package co.airy.core.rasa_connector; | ||
|
||
import co.airy.avro.communication.DeliveryState; | ||
import co.airy.avro.communication.Message; | ||
import co.airy.core.rasa_connector.models.MessageSendResponse; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.Instant; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
@Service | ||
public class MessageHandler { | ||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
MessageHandler() { | ||
} | ||
|
||
public Message getMessage(Message contactMessage, MessageSendResponse response) throws Exception { | ||
String content = getContent(contactMessage.getSource(), response); | ||
if (content == null) { | ||
throw new Exception("Unable to map rasa reply to source response."); | ||
} | ||
|
||
return Message.newBuilder() | ||
.setId(UUID.randomUUID().toString()) | ||
.setChannelId(contactMessage.getChannelId()) | ||
.setContent(content) | ||
.setConversationId(contactMessage.getConversationId()) | ||
.setHeaders(Map.of()) | ||
.setDeliveryState(DeliveryState.PENDING) | ||
.setSource(contactMessage.getSource()) | ||
.setSenderId("rasa-bot") | ||
.setSentAt(Instant.now().toEpochMilli()) | ||
.setIsFromContact(false) | ||
.build(); | ||
} | ||
|
||
public String getContent(String source, MessageSendResponse response) throws JsonProcessingException { | ||
final String text = response.getText(); | ||
if (text == null) { | ||
return null; | ||
} | ||
|
||
final ObjectNode node = getNode(); | ||
switch (source) { | ||
case "google": { | ||
final ObjectNode representative = getNode(); | ||
representative.put("representativeType", "BOT"); | ||
node.set("representative", representative); | ||
node.put("text", text); | ||
return mapper.writeValueAsString(node); | ||
} | ||
case "viber": { | ||
node.put("text", text); | ||
node.put("type", text); | ||
return mapper.writeValueAsString(node); | ||
} | ||
case "chatplugin": | ||
case "instagram": | ||
case "facebook": { | ||
node.put("text", text); | ||
return mapper.writeValueAsString(node); | ||
} | ||
case "twilio.sms": | ||
case "twilio.whatsapp": { | ||
node.put("Body", text); | ||
return mapper.writeValueAsString(node); | ||
} | ||
case "whatsapp": { | ||
node.put("Body", text); | ||
return mapper.writeValueAsString(node); | ||
} | ||
|
||
default: { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
private ObjectNode getNode() { | ||
final JsonNodeFactory jsonNodeFactory = JsonNodeFactory.instance; | ||
return jsonNodeFactory.objectNode(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/connectors/rasa-connector/src/main/java/co/airy/core/rasa_connector/RasaClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package co.airy.core.rasa_connector; | ||
|
||
import co.airy.core.rasa_connector.models.MessageSend; | ||
import co.airy.core.rasa_connector.models.MessageSendResponse; | ||
import feign.Headers; | ||
import feign.RequestLine; | ||
|
||
import java.util.List; | ||
|
||
public interface RasaClient { | ||
@RequestLine("POST /webhooks/rest/webhook") | ||
@Headers("Content-Type: application/json") | ||
List<MessageSendResponse> sendMessage(MessageSend content); | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...connectors/rasa-connector/src/main/java/co/airy/core/rasa_connector/RasaClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package co.airy.core.rasa_connector; | ||
|
||
|
||
import feign.Feign; | ||
import feign.jackson.JacksonDecoder; | ||
import feign.jackson.JacksonEncoder; | ||
import feign.okhttp.OkHttpClient; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class RasaClientConfig { | ||
@Bean | ||
public RasaClient rasaClient(@Value("${rasa.rest-webhook-url}") String rasaRestUrl) { | ||
return Feign.builder() | ||
.client(new OkHttpClient()) | ||
.encoder(new JacksonEncoder()) | ||
.decoder(new JacksonDecoder()) | ||
.logger(new feign.Logger.ErrorLogger()) | ||
.logLevel(feign.Logger.Level.FULL) | ||
.target(RasaClient.class, rasaRestUrl); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no authentication token? How do you authenticate witht he rasa api?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So to just call the REST API endpoint, no authentication is needed. You need authentication when calling other endpoints like /models.parse etc. (i.e for a human-handoff like feature) but not for getting plain responses (without any other data).