Skip to content

Commit

Permalink
Merge branch 'master' of gitlab.com:pencillabs/ej/ej-bot into main
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiola-m committed Feb 19, 2021
2 parents 80b7ff1 + 3c8a42e commit 70a94c8
Show file tree
Hide file tree
Showing 18 changed files with 427 additions and 99 deletions.
13 changes: 6 additions & 7 deletions .env
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
RASA_X_VERSION=<rasa_x_version>
RASA_VERSION=<rasa_version>
RASA_TOKEN=c4TdROQ4sVTc8JUlFLQFvQ
RASA_X_TOKEN=1PJkHOcdUCd21Tnw7T1oTg
PASSWORD_SALT=Mt6gU4mudBeT8B9tgJVfeg
JWT_SECRET=p5J/3qFcHEDl9LC66mQapQ
RABBITMQ_PASSWORD=LzkOXZbnGesQKKHFnh7wmg
DB_PASSWORD=PZQwYSewjII2/zG2+Kmn3g
REDIS_PASSWORD=D3gO8RnXPbnyZ2UyYaHNhw
RASA_TOKEN=
RASA_X_TOKEN=
PASSWORD_SALT=
RABBITMQ_PASSWORD=
DB_PASSWORD=
REDIS_PASSWORD=
RASA_TELEMETRY_ENABLED=false
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ build-coach:

run-shell: ## Run bot in shell, sucessful when shows "Bot loaded. Type a message and press enter (use '/stop' to exit): "
docker-compose run -d actions make actions
docker-compose up -d duckling
docker-compose run bot make shell

run-api:
Expand All @@ -54,10 +55,11 @@ run-x:
docker-compose run --rm --service-ports x make x
############################## TESTS ##############################
test:
docker-compose run --rm bot make test
docker-compose up -d duckling
docker-compose run --name bot --rm bot make test

test-actions:
docker-compose run --rm bot make test-actions
docker-compose run --rm bot make test-actions -e JWT_SECRET=testing_secret_value

run-test-nlu:
docker-compose run --rm bot make test-nlu
Expand Down
2 changes: 1 addition & 1 deletion bot/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ train:

# TESTS
test:
rasa test --out results/
rasa test --out results/ --fail-on-prediction-errors

test-actions:
python -m pytest
Expand Down
29 changes: 20 additions & 9 deletions bot/actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,43 @@
# https://rasa.com/docs/rasa/custom-actions

from typing import Text, List, Any, Dict
import json

#
from rasa_sdk import Action, Tracker, FormValidationAction
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.events import SlotSet, FollowupAction, EventType
from rasa_sdk.types import DomainDict

from .ej_connector import API

#
#


class ActionSetupConversation(Action):
def name(self):
return "action_setup_conversation"

def run(self, dispatcher, tracker, domain):
# TODO: get values from EJ server
conversation_id = 1
user_email = tracker.get_slot("email")
if user_email:
user = API.create_user(tracker.sender_id, user_email, user_email)
else:
user = API.create_user(tracker.sender_id)
# TODO: get values from EJ server
number_comments = 2
number_voted_comments = 1
first_comment = "Comment text here"
comment_id = 53
first_comment = API.get_next_comment(conversation_id, user.token)
return [
SlotSet("number_voted_comments", number_voted_comments),
SlotSet("conversation_id", conversation_id),
SlotSet("number_comments", number_comments),
SlotSet("comment_text", first_comment),
SlotSet("current_comment_id", comment_id),
SlotSet("comment_text", first_comment["content"]),
SlotSet("current_comment_id", first_comment["id"]),
SlotSet("change_comment", False),
SlotSet("ej_user_token", user.token),
FollowupAction("vote_form"),
]

Expand All @@ -52,14 +62,15 @@ def run(
if tracker.get_slot("change_comment"):
# TODO: get values from EJ server
# next_comment = get_random_comment()
new_comment = "novo comentário com outro id"
conversation_id = tracker.get_slot("conversation_id")
token = tracker.get_slot("ej_user_token")
new_comment = API.get_next_comment(conversation_id, token)
dispatcher.utter_message(text=new_comment, buttons=buttons)
number_voted_comments = tracker.get_slot("number_comments") + 1
comment_id = 22
return [
SlotSet("number_voted_comments", number_voted_comments),
SlotSet("comment_text", new_comment),
SlotSet("current_comment_id", comment_id),
SlotSet("comment_text", new_comment["content"]),
SlotSet("current_comment_id", new_comment["id"]),
]
else:
dispatcher.utter_message(
Expand Down
2 changes: 2 additions & 0 deletions bot/actions/ej_connector/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .api import API
from .user import User
58 changes: 58 additions & 0 deletions bot/actions/ej_connector/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os
import requests
from .user import User

HEADERS = {
"Content-Type": "application/json",
}
VOTE_CHOICES = {"Pular": 0, "Concordar": 1, "Discordar": -1}
HOST = os.getenv("EJ_HOST")
API_URL = f"{HOST}/api/v1"
REGISTRATION_URL = f"{HOST}/rest-auth/registration/"
VOTES_URL = f"{API_URL}/votes/"


def conversation_url(conversation_id):
return f"{API_URL}/conversations/{conversation_id}/"


def conversation_random_comment_url(conversation_id):
return f"{conversation_url(conversation_id)}random-comment/"


def user_statistics_url(conversation_id):
return f"{conversation_url(conversation_id)}user-statistics/"


def user_comments_route(conversation_id):
return f"{conversation_url(conversation_id)}user-comments/"


def user_pending_comments_route(conversation_id):
return f"{conversation_url(conversation_id)}user-pending-comments/"


def auth_headers(token):
headers = HEADERS
headers["Authorization"] = f"Token {token}"
return headers


class API:
def create_user(sender_id, name="Participante anônimo", email=""):
user = User(sender_id, name, email)
response = requests.post(
REGISTRATION_URL,
data=user.serialize(),
headers=HEADERS,
)
user.token = response.json()["key"]
return user

def get_next_comment(conversation_id, token):
url = conversation_random_comment_url(conversation_id)
response = requests.get(url, headers=auth_headers(token))
comment = response.json()
comment_url_as_list = comment["links"]["self"].split("/")
comment["id"] = comment_url_as_list[len(comment_url_as_list) - 2]
return comment
23 changes: 23 additions & 0 deletions bot/actions/ej_connector/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
import json
import jwt


class User(object):
def __init__(self, rasa_id, name="", email=""):
self.name = name
self.display_name = ""
self.stats = {}
if email:
self.email = email
self.password = self.email
self.password_confirm = self.email
else:
secret = os.getenv("JWT_SECRET")
encoded_id = jwt.encode({"rasa_id": rasa_id}, secret, algorithm="HS256")
self.email = f"{encoded_id}-rasa@mail.com"
self.password = f"{encoded_id}-rasa"
self.password_confirm = f"{encoded_id}-rasa"

def serialize(self):
return json.dumps(self.__dict__)
4 changes: 3 additions & 1 deletion bot/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pipeline:
epochs: 55
- name: EntitySynonymMapper
- name: ResponseSelector

- name: "DucklingHTTPExtractor"
url: "http://duck:8000"
dimensions: ["email"]
policies:
- name: TEDPolicy
max_history: 10
Expand Down
27 changes: 27 additions & 0 deletions bot/data/nlu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ nlu:
- correto
- afirmativo
- concordar
- uhum
- com certeza
- claro
- intent: disagree
examples: |
Expand All @@ -35,6 +38,7 @@ nlu:
- discordo
- discordar
- não concordo
- não quero
- intent: pass
examples: |
Expand All @@ -44,6 +48,28 @@ nlu:
- passa
- próxima pergunta
- intent: email
examples: |
- meu email é nome@dominio.com
- email@nome.com
- email: esteemail@outroservidor.com
- meu email: email@email.com.br
- intent: invalid_email
examples: |
- @wlor
- @algumacoisa.
- @nome.
- roberto@email
- nome@algo
- roberto@
- maria@
- @joao
- @joao.
- meu email é @nome
- email: @maria
- meu email: roberto@
- intent: out_of_context
examples: |
- 4 + 2 = ?
Expand All @@ -60,3 +86,4 @@ nlu:
- tempo
- stop
- sair
55 changes: 47 additions & 8 deletions bot/data/stories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,51 @@ stories:
- intent: disagree
- action: utter_goodbye

- story: Activate vote form
- story: User wants to participate
steps:
- intent: agree
- action: action_setup_conversation
- action: vote_form
- active_loop: vote_form
- active_loop: null
- slot_was_set:
- vote: null
- intent: start
- action: utter_start
- intent: agree
- action: utter_ask_email

- story: Activate vote form with user email
steps:
- intent: agree
- action: utter_ask_email
- intent: email
entities:
- email
- action: action_setup_conversation
- action: vote_form
- active_loop: vote_form
- active_loop: null
- slot_was_set:
- vote: null

- story: Activate vote form without user email
steps:
- intent: agree
- action: utter_ask_email
- intent: disagree
- action: action_setup_conversation
- action: vote_form
- active_loop: vote_form
- active_loop: null
- slot_was_set:
- vote: null

- story: User provides invalid email
steps:
- intent: agree
- action: utter_ask_email
- intent: invalid_email
- action: utter_invalid_email
- intent: email
entities:
- email
- action: action_setup_conversation
- action: vote_form
- active_loop: vote_form
- active_loop: null
- slot_was_set:
- vote: null
26 changes: 22 additions & 4 deletions bot/domain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ intents:
use_entities: true
- start:
use_entities: true
- email:
use_entities: true
- out_of_context:
use_entities: true
entities: []
- invalid_email:
use_entities: true
entities:
- email
slots:
change_comment:
type: rasa.shared.core.slots.BooleanSlot
Expand All @@ -27,6 +32,15 @@ slots:
initial_value: null
auto_fill: true
influence_conversation: false
ej_user_token:
type: rasa.shared.core.slots.TextSlot
initial_value: null
auto_fill: true
influence_conversation: false
email:
type: rasa.shared.core.slots.TextSlot
initial_value: null
auto_fill: true
conversation_id:
type: rasa.shared.core.slots.FloatSlot
initial_value: null
Expand Down Expand Up @@ -61,12 +75,16 @@ slots:
auto_fill: true
influence_conversation: false
values:
- Concordar
- Discordar
- Pular
- concordar
- discordar
- pular
responses:
utter_start:
- text: Olá, estou coletando opiniões, gostaria de participar?
utter_ask_email:
- text: Legal, você gostaria de se identificar pelo seu email? Se sim, já pode enviar que anoto aqui.
utter_invalid_email:
- text: Não entendi, esse email não parece válido, pode tentar enviar de novo?
utter_goodbye:
- text: Agradeço pela participação! Até logo
utter_vote_received:
Expand Down
Loading

0 comments on commit 70a94c8

Please sign in to comment.