-
Notifications
You must be signed in to change notification settings - Fork 222
/
run.py
executable file
·148 lines (126 loc) · 5.66 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
import os
from cloudant.client import Cloudant
from dotenv import load_dotenv
from slackclient import SlackClient
from ibm_watson import AssistantV1
from ibm_watson import DiscoveryV1
from ibm_cloud_sdk_core import get_authenticator_from_environment
from watsononlinestore.database.cloudant_online_store import \
CloudantOnlineStore
from watsononlinestore.watson_online_store import WatsonOnlineStore
class WatsonEnv:
def __init__(self):
pass
@staticmethod
def get_vcap_credentials(vcap_env, service):
if service in vcap_env:
vcap_instance_list = vcap_env[service]
if isinstance(vcap_instance_list, list):
first = vcap_instance_list[0]
if 'credentials' in first:
return first['credentials']
@staticmethod
def get_slack_user_id(slack_client):
"""Get slack bot user ID from SLACK_BOT_USER or BOT_ID env vars.
Use the original BOT_ID if found, but now we can instead take the
SLACK_BOT_USER (familiar bot name) and look-up the ID.
This should be called after env is loaded when using dotenv.
"""
slack_bot_user = os.environ.get('SLACK_BOT_USER')
print("Looking up BOT_ID for '%s'" % slack_bot_user)
api_call = slack_client.api_call("users.list")
if api_call.get('ok'):
# retrieve all users so we can find our bot
users = api_call.get('members')
for user in users:
if 'name' in user and user.get('name') == slack_bot_user:
bot_id = user.get('id')
print("Found BOT_ID=" + bot_id)
return bot_id
else:
print("could not find user with the name " + slack_bot_user)
else:
print("could not find user because api_call did not return 'ok'")
return None
@staticmethod
def get_watson_online_store():
load_dotenv(os.path.join(os.path.dirname(__file__), ".env"))
# Use these env vars first if set
bot_id = os.environ.get("BOT_ID")
slack_bot_token = os.environ.get('SLACK_BOT_TOKEN')
authenticator = (get_authenticator_from_environment('assistant') or
get_authenticator_from_environment('conversation'))
cloudant_account = os.environ.get("CLOUDANT_USERNAME")
cloudant_iam_apikey = os.environ.get("CLOUDANT_IAM_APIKEY")
cloudant_db_name = os.environ.get(
"CLOUDANT_DB_NAME") or 'watson_online_store'
# If the CLOUDANT_USERNAME env var was not set then use
# VCAP_SERVICES like a WatsonService would.
if not cloudant_iam_apikey:
vcap_services = os.environ.get("VCAP_SERVICES")
vcap_env = json.loads(vcap_services) if vcap_services else None
if vcap_env:
cloudant_creds = WatsonEnv.get_vcap_credentials(
vcap_env, 'cloudantNoSQLDB')
if cloudant_creds:
if 'apikey' in cloudant_creds:
cloudant_iam_apikey = cloudant_creds['apikey']
if 'username' in cloudant_creds:
cloudant_account = cloudant_creds['username']
# Instantiate Watson Assistant client.
# - only give a url if we have one (don't override the default)
assistant_client = AssistantV1(
version='2018-09-20',
authenticator=authenticator)
# Instantiate Cloudant DB.
cloudant_online_store = CloudantOnlineStore(
Cloudant.iam(
cloudant_account,
cloudant_iam_apikey,
connect=True
),
cloudant_db_name
)
# Instantiate Watson Discovery client.
# - only give a url if we have one (don't override the default)
discovery_client = DiscoveryV1(
version='2019-11-22',
)
# Instantiate Slack chatbot.
if not slack_bot_token or 'placeholder' in slack_bot_token:
print("SLACK_BOT_TOKEN needs to be set correctly. "
"It is currently set to '%s'." % slack_bot_token)
print("Only the web UI will be available.")
slack_client = None
else:
slack_client = SlackClient(slack_bot_token)
# If BOT_ID wasn't set, we can get it using SLACK_BOT_USER.
if not bot_id:
bot_id = WatsonEnv.get_slack_user_id(slack_client)
if not bot_id:
print("Error: Missing BOT_ID or invalid SLACK_BOT_USER.")
return None
# Start Watson Online Store app.
watsononlinestore = WatsonOnlineStore(bot_id,
slack_client,
assistant_client,
discovery_client,
cloudant_online_store)
return watsononlinestore
if __name__ == "__main__":
watsononlinestore = WatsonEnv.get_watson_online_store()
if watsononlinestore:
watsononlinestore.run()