Skip to content

Commit 1a531da

Browse files
Merge pull request #183 from guilherme1guy/devel
Add rocketchat to boilerplate
2 parents 84560ab + 8bbb2ea commit 1a531da

File tree

5 files changed

+99
-78
lines changed

5 files changed

+99
-78
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ run-notebooks:
7676
$(info Acesse o KIBANA em: http://localhost:8888)
7777
$(info )
7878

79+
run-rocket:
80+
docker-compose up -d rocketchat bot-rocket
81+
7982
train:
8083
mkdir -p bot/models
8184
docker-compose up --build coach

bot/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ webchat:
4848
api:
4949
rasa run -m models/ -vv --endpoints endpoints.yml --enable-api
5050

51+
rocket:
52+
python /modules/rocketchat/bot_config.py
53+
rasa run -m models/ -vv --endpoints endpoints.yml --credentials credentials.yml --port 5005 --cors '*'
54+
55+
5156
# ACTIONS
5257
actions:
5358
rasa run actions --actions actions -vv

bot/credentials.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ socketio:
1313
bot_message_evt: bot_uttered
1414
session_persistence: true
1515

16+
17+
rocketchat:
18+
user: "bot"
19+
password: "bot"
20+
server_url: "http://rocketchat:3000"
21+
1622
rest:

docker-compose.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ services:
6767
- actions
6868
command: sh -c "make webchat"
6969

70+
# ============================ WebChat Bot =================================
71+
# Specific Rasa bot integrated with WebChat.
72+
bot-rocket:
73+
build:
74+
context: .
75+
dockerfile: ./docker/bot.Dockerfile
76+
volumes:
77+
- ./bot/:/bot/
78+
ports:
79+
- 5005:5005
80+
depends_on:
81+
- actions
82+
command: sh -c "make rocket"
83+
7084
# =============================== Analytics =================================
7185
# Analitics ElasticSearch Stack.
7286
elasticsearch:
@@ -165,10 +179,65 @@ services:
165179
ports:
166180
- 8888:8888
167181

182+
# =============================== Rocket.Chat =================================
183+
# Rocket.Chat instance and database
184+
rocketchat:
185+
image: rocketchat/rocket.chat:3.7.1
186+
command: >
187+
bash -c
188+
"for i in `seq 1 30`; do
189+
node main.js &&
190+
s=$$? && break || s=$$?;
191+
echo \"Tried $$i times. Waiting 5 secs...\";
192+
sleep 5;
193+
done; (exit $$s)"
194+
restart: unless-stopped
195+
volumes:
196+
- rocket_uploads:/app/uploads
197+
environment:
198+
- PORT=3000
199+
- ROOT_URL=http://localhost:3000
200+
- MONGO_URL=mongodb://mongo:27017/rocketchat
201+
- MONGO_OPLOG_URL=mongodb://mongo:27017/local
202+
- ADMIN_USERNAME=admin
203+
- ADMIN_PASS=admin
204+
205+
depends_on:
206+
- mongo
207+
- mongo-init-replica
208+
ports:
209+
- 3000:3000
210+
211+
mongo:
212+
image: mongo:4.0
213+
restart: unless-stopped
214+
volumes:
215+
- mongo_data:/data/db
216+
command: mongod --smallfiles --oplogSize 128 --replSet rs0 --storageEngine=mmapv1
217+
218+
# this container's job is just run the command to initialize the replica set.
219+
# it will run the command and remove himself (it will not stay running)
220+
mongo-init-replica:
221+
image: mongo:4.0
222+
command: >
223+
bash -c
224+
"for i in `seq 1 30`; do
225+
mongo mongo/rocketchat --eval \"
226+
rs.initiate({
227+
_id: 'rs0',
228+
members: [ { _id: 0, host: 'localhost:27017' } ]})\" &&
229+
s=$$? && break || s=$$?;
230+
echo \"Tried $$i times. Waiting 5 secs...\";
231+
sleep 5;
232+
done; (exit $$s)"
233+
depends_on:
234+
- mongo
235+
168236
volumes:
169237
notebook_models:
170238
mongo_data:
171239
rabbit_data:
172240
esbackup:
173241
esdata:
174242
driver: local
243+
rocket_uploads:

modules/rocketchat/bot_config.py

Lines changed: 16 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# == Log Config ==
99

10-
logger = logging.getLogger("Bot Config")
10+
logger = logging.getLogger(__name__)
1111
logger.setLevel(logging.DEBUG)
1212

1313
ch = logging.StreamHandler()
@@ -44,7 +44,7 @@
4444
admin_name = os.getenv("ROCKETCHAT_ADMIN_USERNAME", "admin")
4545
admin_password = os.getenv("ROCKETCHAT_ADMIN_PASSWORD", "admin")
4646

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

5050

@@ -121,40 +121,6 @@ def create_bot_user():
121121
)
122122

123123

124-
def create_livechat_agent():
125-
response = api_post("livechat/users/agent", {"username": bot["username"]})
126-
return response["user"]["_id"]
127-
128-
129-
def configure_livechat():
130-
# Enable Livechat
131-
api_post("settings/Livechat_enabled", {"value": True})
132-
133-
# Disable show pre-registration form
134-
api_post("settings/Livechat_registration_form", {"value": False})
135-
136-
# Change Livechat Color
137-
api_post("settings/Livechat_title_color", {"value": "#039046", "editor": "color"})
138-
139-
# Change Livechat Title
140-
api_post("settings/Livechat_title", {"value": bot["name"]})
141-
142-
# Disable Livechat Email display
143-
api_post("settings/Livechat_show_agent_email", {"value": False})
144-
145-
# Disable file upload
146-
api_post("settings/Livechat_fileupload_enabled", {"value": False})
147-
148-
# Change Livechat Webhook URL
149-
api_post("settings/Livechat_webhookUrl", {"value": rasa_url})
150-
151-
# Activate Livechat Webhook Send Request on Visitor Message
152-
api_post("settings/Livechat_webhook_on_visitor_message", {"value": True})
153-
154-
# Activate Livechat Webhook Send Request on Agent Messages
155-
api_post("settings/Livechat_webhook_on_agent_message", {"value": True})
156-
157-
158124
def configure_webhooks():
159125
webooks = api_get("integrations.list")
160126

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

194160

195-
def create_department(bot_agent_id):
196-
get_departments_url = host + "/api/v1/livechat/department"
197-
198-
get_departments_response = requests.get(get_departments_url, headers=user_header)
199-
200-
number_of_departments = len(get_departments_response.json()["departments"])
201-
202-
if number_of_departments == 0:
203-
api_post(
204-
"livechat/department",
205-
{
206-
"department": {
207-
"enabled": True,
208-
"showOnRegistration": True,
209-
"name": "department",
210-
"description": "default department",
211-
},
212-
"agents": [
213-
{
214-
"agentId": bot_agent_id,
215-
"username": bot["username"],
216-
"count": 0,
217-
"order": 0,
218-
}
219-
],
220-
},
221-
)
222-
223-
224161
if __name__ == "__main__":
225162
logger.info("===== Automatic env configuration =====")
226163

227-
try:
228-
user_header = get_authentication_token()
229-
except Exception:
230-
print("\n\n --------- Rocket Chat Unavailable! --------\n\n")
164+
rocket_available = False
165+
166+
while not rocket_available:
167+
try:
168+
user_header = get_authentication_token()
169+
rocket_available = True
170+
except Exception:
171+
import time
172+
logger.info("\n\n --------- Rocket Chat Unavailable! --------\n\n")
173+
logger.info(">> Waiting for 3 seconds...")
174+
time.sleep(3)
175+
176+
logger.info(">> Connected to Rocket Instance")
231177

232178
if user_header:
233179
logger.info(">> Create user")
234180
create_bot_user()
235181

236-
logger.info(">> Create livechat agent")
237-
bot_agent_id = create_livechat_agent()
238-
239-
logger.info(">> Configure livechat")
240-
configure_livechat()
241-
242182
logger.info(">> Configure Rocketchat")
243183
configure_rocketchat()
244184

245185
logger.info(">> Configure Webhooks")
246186
configure_webhooks()
247187

248-
logger.info(">> Create livechat department")
249-
create_department(bot_agent_id)
250188

251189
else:
252-
logger.error("Login Failed")
190+
logger.error(">> Login Failed")

0 commit comments

Comments
 (0)