From 8bbb2eaf47dab94b529b60b4b2a69108a6ab289e Mon Sep 17 00:00:00 2001 From: Guilherme Guy Date: Tue, 27 Oct 2020 10:04:24 -0300 Subject: [PATCH] Add rocketchat to boilerplate --- Makefile | 3 + bot/Makefile | 5 ++ bot/credentials.yml | 6 ++ docker-compose.yml | 69 +++++++++++++++++++++++ modules/rocketchat/bot_config.py | 94 ++++++-------------------------- 5 files changed, 99 insertions(+), 78 deletions(-) diff --git a/Makefile b/Makefile index 07dc24e0..a70abc65 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/bot/Makefile b/bot/Makefile index cf1e5dfe..da89cccc 100644 --- a/bot/Makefile +++ b/bot/Makefile @@ -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 diff --git a/bot/credentials.yml b/bot/credentials.yml index 923995a4..827f866d 100644 --- a/bot/credentials.yml +++ b/bot/credentials.yml @@ -13,4 +13,10 @@ socketio: bot_message_evt: bot_uttered session_persistence: true + +rocketchat: + user: "bot" + password: "bot" + server_url: "http://rocketchat:3000" + rest: diff --git a/docker-compose.yml b/docker-compose.yml index e71b7d04..0083915b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: @@ -165,6 +179,60 @@ 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: @@ -172,3 +240,4 @@ volumes: esbackup: esdata: driver: local + rocket_uploads: diff --git a/modules/rocketchat/bot_config.py b/modules/rocketchat/bot_config.py index c86b4bdd..17760152 100755 --- a/modules/rocketchat/bot_config.py +++ b/modules/rocketchat/bot_config.py @@ -7,7 +7,7 @@ # == Log Config == -logger = logging.getLogger("Bot Config") +logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() @@ -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 @@ -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") @@ -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")