Skip to content

Commit

Permalink
Merge pull request #183 from guilherme1guy/devel
Browse files Browse the repository at this point in the history
Add rocketchat to boilerplate
  • Loading branch information
arthurTemporim authored Oct 28, 2020
2 parents 84560ab + 8bbb2ea commit 1a531da
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 78 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ run-notebooks:
$(info Acesse o KIBANA em: http://localhost:8888)
$(info )

run-rocket:
docker-compose up -d rocketchat bot-rocket

train:
mkdir -p bot/models
docker-compose up --build coach
Expand Down
5 changes: 5 additions & 0 deletions bot/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ webchat:
api:
rasa run -m models/ -vv --endpoints endpoints.yml --enable-api

rocket:
python /modules/rocketchat/bot_config.py
rasa run -m models/ -vv --endpoints endpoints.yml --credentials credentials.yml --port 5005 --cors '*'


# ACTIONS
actions:
rasa run actions --actions actions -vv
6 changes: 6 additions & 0 deletions bot/credentials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ socketio:
bot_message_evt: bot_uttered
session_persistence: true


rocketchat:
user: "bot"
password: "bot"
server_url: "http://rocketchat:3000"

rest:
69 changes: 69 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ services:
- actions
command: sh -c "make webchat"

# ============================ WebChat Bot =================================
# Specific Rasa bot integrated with WebChat.
bot-rocket:
build:
context: .
dockerfile: ./docker/bot.Dockerfile
volumes:
- ./bot/:/bot/
ports:
- 5005:5005
depends_on:
- actions
command: sh -c "make rocket"

# =============================== Analytics =================================
# Analitics ElasticSearch Stack.
elasticsearch:
Expand Down Expand Up @@ -165,10 +179,65 @@ services:
ports:
- 8888:8888

# =============================== Rocket.Chat =================================
# Rocket.Chat instance and database
rocketchat:
image: rocketchat/rocket.chat:3.7.1
command: >
bash -c
"for i in `seq 1 30`; do
node main.js &&
s=$$? && break || s=$$?;
echo \"Tried $$i times. Waiting 5 secs...\";
sleep 5;
done; (exit $$s)"
restart: unless-stopped
volumes:
- rocket_uploads:/app/uploads
environment:
- PORT=3000
- ROOT_URL=http://localhost:3000
- MONGO_URL=mongodb://mongo:27017/rocketchat
- MONGO_OPLOG_URL=mongodb://mongo:27017/local
- ADMIN_USERNAME=admin
- ADMIN_PASS=admin
- ADMIN_EMAIL=mail@mail.com
depends_on:
- mongo
- mongo-init-replica
ports:
- 3000:3000

mongo:
image: mongo:4.0
restart: unless-stopped
volumes:
- mongo_data:/data/db
command: mongod --smallfiles --oplogSize 128 --replSet rs0 --storageEngine=mmapv1

# this container's job is just run the command to initialize the replica set.
# it will run the command and remove himself (it will not stay running)
mongo-init-replica:
image: mongo:4.0
command: >
bash -c
"for i in `seq 1 30`; do
mongo mongo/rocketchat --eval \"
rs.initiate({
_id: 'rs0',
members: [ { _id: 0, host: 'localhost:27017' } ]})\" &&
s=$$? && break || s=$$?;
echo \"Tried $$i times. Waiting 5 secs...\";
sleep 5;
done; (exit $$s)"
depends_on:
- mongo

volumes:
notebook_models:
mongo_data:
rabbit_data:
esbackup:
esdata:
driver: local
rocket_uploads:
94 changes: 16 additions & 78 deletions modules/rocketchat/bot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# == Log Config ==

logger = logging.getLogger("Bot Config")
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

ch = logging.StreamHandler()
Expand Down Expand Up @@ -44,7 +44,7 @@
admin_name = os.getenv("ROCKETCHAT_ADMIN_USERNAME", "admin")
admin_password = os.getenv("ROCKETCHAT_ADMIN_PASSWORD", "admin")

rasa_url = os.getenv("RASA_URL", "http://bot:5005/webhooks/rocketchat/webhook")
rasa_url = os.getenv("RASA_URL", "http://bot-rocket:5005/webhooks/rocketchat/webhook")
user_header = None


Expand Down Expand Up @@ -121,40 +121,6 @@ def create_bot_user():
)


def create_livechat_agent():
response = api_post("livechat/users/agent", {"username": bot["username"]})
return response["user"]["_id"]


def configure_livechat():
# Enable Livechat
api_post("settings/Livechat_enabled", {"value": True})

# Disable show pre-registration form
api_post("settings/Livechat_registration_form", {"value": False})

# Change Livechat Color
api_post("settings/Livechat_title_color", {"value": "#039046", "editor": "color"})

# Change Livechat Title
api_post("settings/Livechat_title", {"value": bot["name"]})

# Disable Livechat Email display
api_post("settings/Livechat_show_agent_email", {"value": False})

# Disable file upload
api_post("settings/Livechat_fileupload_enabled", {"value": False})

# Change Livechat Webhook URL
api_post("settings/Livechat_webhookUrl", {"value": rasa_url})

# Activate Livechat Webhook Send Request on Visitor Message
api_post("settings/Livechat_webhook_on_visitor_message", {"value": True})

# Activate Livechat Webhook Send Request on Agent Messages
api_post("settings/Livechat_webhook_on_agent_message", {"value": True})


def configure_webhooks():
webooks = api_get("integrations.list")

Expand Down Expand Up @@ -192,61 +158,33 @@ def configure_rocketchat():
api_post("settings/API_Enable_CORS", {"value": True})


def create_department(bot_agent_id):
get_departments_url = host + "/api/v1/livechat/department"

get_departments_response = requests.get(get_departments_url, headers=user_header)

number_of_departments = len(get_departments_response.json()["departments"])

if number_of_departments == 0:
api_post(
"livechat/department",
{
"department": {
"enabled": True,
"showOnRegistration": True,
"name": "department",
"description": "default department",
},
"agents": [
{
"agentId": bot_agent_id,
"username": bot["username"],
"count": 0,
"order": 0,
}
],
},
)


if __name__ == "__main__":
logger.info("===== Automatic env configuration =====")

try:
user_header = get_authentication_token()
except Exception:
print("\n\n --------- Rocket Chat Unavailable! --------\n\n")
rocket_available = False

while not rocket_available:
try:
user_header = get_authentication_token()
rocket_available = True
except Exception:
import time
logger.info("\n\n --------- Rocket Chat Unavailable! --------\n\n")
logger.info(">> Waiting for 3 seconds...")
time.sleep(3)

logger.info(">> Connected to Rocket Instance")

if user_header:
logger.info(">> Create user")
create_bot_user()

logger.info(">> Create livechat agent")
bot_agent_id = create_livechat_agent()

logger.info(">> Configure livechat")
configure_livechat()

logger.info(">> Configure Rocketchat")
configure_rocketchat()

logger.info(">> Configure Webhooks")
configure_webhooks()

logger.info(">> Create livechat department")
create_department(bot_agent_id)

else:
logger.error("Login Failed")
logger.error(">> Login Failed")

0 comments on commit 1a531da

Please sign in to comment.