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
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') conversation_username = os.environ.get("CONVERSATION_USERNAME") conversation_password = os.environ.get("CONVERSATION_PASSWORD") cloudant_username = os.environ.get("CLOUDANT_USERNAME") cloudant_password = os.environ.get("CLOUDANT_PASSWORD") cloudant_url = os.environ.get("CLOUDANT_URL") cloudant_db_name = os.environ.get("CLOUDANT_DB_NAME") discovery_username = os.environ.get('DISCOVERY_USERNAME') discovery_password = os.environ.get('DISCOVERY_PASSWORD') if not all((conversation_username, conversation_password, cloudant_username, cloudant_password, cloudant_url, discovery_username, discovery_password)): # If some of the service env vars are not set get them from VCAP vcap_env = None vcap_services = os.environ.get("VCAP_SERVICES") if vcap_services: vcap_env = json.loads(vcap_services) if vcap_env: conversation_creds = WatsonEnv.get_vcap_credentials( vcap_env, 'conversation') conversation_username = \ conversation_username or conversation_creds['username'] conversation_password = \ conversation_password or conversation_creds['password'] cloudant_creds = WatsonEnv.get_vcap_credentials( vcap_env, 'cloudantNoSQLDB') cloudant_username = \ cloudant_username or cloudant_creds['username'] cloudant_password = \ cloudant_password or cloudant_creds['password'] cloudant_url = cloudant_url or cloudant_creds['url'] discovery_creds = WatsonEnv.get_vcap_credentials( vcap_env, 'discovery') discovery_username = \ discovery_username or discovery_creds['username'] discovery_password = \ discovery_password or discovery_creds['password'] # If we still don't have all the above plus a few, then no WOS. if not all( (slack_bot_token, conversation_username, conversation_password, cloudant_username, cloudant_password, cloudant_url, cloudant_db_name, discovery_username, discovery_password)): print(MISSING_ENV_VARS) return None # Instantiate Watson Conversation client. conversation_client = ConversationV1(username=conversation_username, password=conversation_password, version='2016-07-11') # Instantiate Cloudant DB. cloudant_online_store = CloudantOnlineStore( Cloudant(cloudant_username, cloudant_password, url=cloudant_url, connect=True), cloudant_db_name) # Instantiate Watson Discovery client. discovery_client = DiscoveryV1(version='2017-07-19', username=discovery_username, password=discovery_password) # Instantiate Slack chatbot. if 'placeholder' in slack_bot_token: raise Exception("SLACK_BOT_TOKEN needs to be set correctly. " "It is currently set to 'placeholder'.") slack_client = SlackClient(slack_bot_token) # If BOT_ID wasn't set, we can get it using SlackClient and user ID. 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, conversation_client, discovery_client, cloudant_online_store) return watsononlinestore
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') conversation_username = os.environ.get("CONVERSATION_USERNAME") conversation_password = os.environ.get("CONVERSATION_PASSWORD") conversation_url = os.environ.get("CONVERSATION_URL") cloudant_username = os.environ.get("CLOUDANT_USERNAME") cloudant_password = os.environ.get("CLOUDANT_PASSWORD") cloudant_url = os.environ.get("CLOUDANT_URL") cloudant_db_name = os.environ.get( "CLOUDANT_DB_NAME") or 'watson_online_store' discovery_username = os.environ.get('DISCOVERY_USERNAME') discovery_password = os.environ.get('DISCOVERY_PASSWORD') discovery_url = os.environ.get('DISCOVERY_URL') # If the CLOUDANT_USERNAME env var was not set then use # VCAP_SERVICES like a WatsonService would. if not cloudant_username: 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: cloudant_url = cloudant_creds['url'] # overrides default if 'username' in cloudant_creds: cloudant_username = cloudant_creds['username'] if 'password' in cloudant_creds: cloudant_password = cloudant_creds['password'] # Instantiate Watson Assistant client. # - only give a url if we have one (don't override the default) conversation_kwargs = { 'version': '2017-05-26', 'username': conversation_username, 'password': conversation_password } if conversation_url: conversation_kwargs['url'] = conversation_url conversation_client = ConversationV1(**conversation_kwargs) # Instantiate Cloudant DB. cloudant_online_store = CloudantOnlineStore( Cloudant( cloudant_username, cloudant_password, url=CloudantOnlineStore.optimize_cloudant_url(cloudant_url), connect=True ), cloudant_db_name ) # Instantiate Watson Discovery client. # - only give a url if we have one (don't override the default) discovery_kwargs = { 'version': '2017-09-01', 'username': discovery_username, 'password': discovery_password } if discovery_url: discovery_kwargs['url'] = discovery_url discovery_client = DiscoveryV1(**discovery_kwargs) # 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, conversation_client, discovery_client, cloudant_online_store) return watsononlinestore
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') assistant_iam_apikey = os.environ.get("ASSISTANT_IAM_APIKEY") assistant_url = os.environ.get("ASSISTANT_URL") if not assistant_url: # Direct access to VCAP to workaround SDK problems vcap_services = os.environ.get("VCAP_SERVICES") vcap_env = json.loads(vcap_services) if vcap_services else None if vcap_env: assistant_creds = WatsonEnv.get_vcap_credentials( vcap_env, 'conversation') if assistant_creds: assistant_url = assistant_creds['url'] # overrides default assistant_iam_apikey = (assistant_iam_apikey or assistant_creds.get('apikey')) 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' discovery_url = os.environ.get('DISCOVERY_URL') discovery_iam_apikey = os.environ.get("DISCOVERY_IAM_APIKEY") # 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_kwargs = { 'version': '2019-02-28', 'iam_apikey': assistant_iam_apikey } if assistant_url: assistant_kwargs['url'] = assistant_url assistant_client = AssistantV1(**assistant_kwargs) # 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_kwargs = { 'version': '2019-04-30', 'iam_apikey': discovery_iam_apikey } if discovery_url: discovery_kwargs['url'] = discovery_url discovery_client = DiscoveryV1(**discovery_kwargs) # 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
from database.cloudant_online_store import CloudantOnlineStore if __name__ == "__main__": load_dotenv(os.path.join(os.path.dirname(__file__), ".env")) bot_id = os.environ.get("BOT_ID") slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN')) conversation_client = ConversationV1( username=os.environ.get("CONVERSATION_USERNAME"), password=os.environ.get("CONVERSATION_PASSWORD"), version='2016-07-11') cloudant_online_store = CloudantOnlineStore( Cloudant( os.environ.get("CLOUDANT_USERNAME"), os.environ.get("CLOUDANT_PASSWORD"), url=os.environ.get("CLOUDANT_URL"), connect=True ), os.environ.get("CLOUDANT_DB_NAME") ) watsononlinestore = WatsonOnlineStore(bot_id, slack_client, conversation_client, cloudant_online_store) watsononlinestore.run()