async def get_data_importer_logs(current_user: User = Depends( auth.get_current_user_and_bot)): """ Get data importer event logs. """ logs = list(DataImporterLogProcessor.get_logs(current_user.get_bot())) return Response(data=logs)
async def list_actions(current_user: User = Depends( auth.get_current_user_and_bot)): """ Returns list of actions for bot. """ actions = mongo_processor.list_actions(bot=current_user.bot) return Response(data=actions)
async def get_integrations(current_user: User = Security( Authentication.get_current_user_and_bot, scopes=TESTER_ACCESS), ): """ List available integrations. """ return Response(data=list( IntegrationProcessor.get_integrations(current_user.get_bot())))
async def allow_bot_for_user( allow_bot: BotAccessRequest, background_tasks: BackgroundTasks, bot: str = Path(default=None, description="bot id", example="613f63e87a1d435607c3c183"), current_user: User = Security(Authentication.get_current_user_and_bot, scopes=ADMIN_ACCESS)): """ Allows user to access a bot. """ bot_name, url = AccountProcessor.allow_bot_and_generate_invite_url( bot, allow_bot.email, current_user.get_user(), current_user.account, allow_bot.role) if Utility.email_conf["email"]["enable"]: background_tasks.add_task( Utility.format_and_send_mail, mail_type='add_member', email=allow_bot.email, url=url, first_name=f'{current_user.first_name} {current_user.last_name}', bot_name=bot_name, role=allow_bot.role) return Response(message='An invitation has been sent to the user') else: return {"message": "User added"}
async def unsuccessful_sessions(request: HistoryQuery = HistoryQuery(), collection: str = Depends(Authentication.authenticate_and_get_collection)): """Fetches the count of sessions that encountered a fallback for a particular user.""" user_list, message = HistoryProcessor.unsuccessful_session( collection, request.month, request.action_fallback, request.nlu_fallback ) return Response(data=user_list, message=message)
async def get_delete_history_logs(current_user: User = Security( Authentication.get_current_user_and_bot, scopes=ADMIN_ACCESS)): """ Get history deletion event logs. """ logs = list(HistoryDeletionLogProcessor.get_logs(current_user.get_bot())) return Response(data=logs)
async def get_intents_with_training_examples(current_user: User = Depends( auth.get_current_user)): """ Fetches list of existing intents and associated training examples for particular bot """ return Response(data=mongo_processor.get_intents_and_training_examples( current_user.get_bot())).dict()
async def get_intents(current_user: User = Depends( auth.get_current_user_and_bot)): """ Fetches list of existing intents for particular bot """ return Response( data=mongo_processor.get_intents(current_user.get_bot())).dict()
async def total_sessions(request: HistoryQuery = HistoryQuery(), collection: str = Depends(Authentication.authenticate_and_get_collection)): """Fetches the total session count for users for the past months.""" user_list, message = HistoryProcessor.session_count( collection, request.month ) return Response(data=user_list, message=message)
async def http_exception_handler(request, exc): """ This function logs the HTTP error detected and returns the appropriate message and details of the error """ logger.debug(exc) return JSONResponse( Response(success=False, error_code=exc.status_code, message=str(exc.detail)).dict())
async def list_http_actions(current_user: User = Depends( auth.get_current_user)): """ Returns list of http actions for bot and user. """ actions = mongo_processor.list_http_actions(user=current_user.get_user(), bot=current_user.bot) return Response(data=actions)
async def add_http_action(request_data: HttpActionConfigRequest, current_user: User = Depends(auth.get_current_user)): """ Stores the http action config and story event """ http_config_id = mongo_processor.add_http_action_with_story(request_data, current_user.get_user(), current_user.get_bot()) response = {"http_config_id": http_config_id} message = "Http action added!" return Response(data=response, message=message)
async def mongoengine_notregistered_exception_handler(request, exc): """ Error handler for NotRegistered errors. Logs the NotRegistered error detected and returns the appropriate message and details of the error. """ logger.debug(exc) return JSONResponse( Response(success=False, error_code=422, message=str(exc)).dict())
async def validation_exception_handler(request, exc: RequestValidationError): """ Error handler for RequestValidationError. Logs the RequestValidationError detected and returns the appropriate message and details of the error. """ logger.debug(exc) return JSONResponse( Response(success=False, error_code=422, message=exc.errors()).dict())
async def assertion_error_handler(request, exc): """ Error handler for AssertionError. This function logs the Assertion error detected and returns the appropriate message and details of the error. """ logger.debug(exc) return JSONResponse( Response(success=False, error_code=422, message=str(exc)).dict())
async def mongoengine_invalid_query_exception_handler(request, exc): """ Error handler for InvalidQueryError. Logs the InvalidQueryError detected and returns the appropriate message and details of the error. """ logger.debug(exc) return JSONResponse( Response(success=False, error_code=422, message=str(exc)).dict())
async def get_http_action(action: str = Path(default=None, description="action name", example="http_action"), current_user: User = Depends(auth.get_current_user)): """ Returns configuration set for the HTTP action """ http_action_config = mongo_processor.get_http_action_config(action_name=action, user=current_user.get_user(), bot=current_user.bot) action_config = Utility.build_http_response_object(http_action_config, current_user.get_user(), current_user.bot) return Response(data=action_config)
async def list_users_for_bot( bot: str = Path(default=None, description="bot id", example="613f63e87a1d435607c3c183"), current_user: User = Security(Authentication.get_current_user_and_bot, scopes=TESTER_ACCESS)): """ Lists active/inactive/invited users of a bot. """ return Response(data=list(AccountProcessor.list_bot_accessors(bot)))
async def list_active_bot_invites(current_user: User = Security( Authentication.get_current_user)): """ Lists active bot invites. """ return Response( data={ 'active_invites': list(AccountProcessor.list_active_invites(current_user.get_user())) })
async def app_exception_handler(request, exc): """ Error handler for AppException errors. Logs the AppException error detected and returns the appropriate message and details of the error. """ logger.debug(exc) return JSONResponse( Response(success=False, error_code=422, message=str(exc)).dict())
async def search_user(request_data: TextData, current_user: User = Security( Authentication.get_current_user)): """ Lists active bot invites. """ return Response(data={ 'matching_users': list(AccountProcessor.search_user(request_data.data)) })
async def mongoengine_multiple_objects_exception_handler(request, exc): """ Error handler for MultipleObjectsReturned. Logs the MultipleObjectsReturned error detected and returns the appropriate message and details of the error. """ logger.debug(exc) return JSONResponse( Response(success=False, error_code=422, message=str(exc)).dict())
async def update_http_action(request_data: HttpActionConfigRequest, current_user: User = Depends(auth.get_current_user)): """ Updates the http action config and related story event """ http_config_id = mongo_processor.update_http_config(request_data=request_data, user=current_user.get_user(), bot=current_user.get_bot()) response = {"http_config_id": http_config_id} message = "Http action updated!" return Response(data=response, message=message)
async def list_bots(current_user: User = Depends( Authentication.get_current_user)): """ List bots for account. """ bots = AccountProcessor.get_accessible_bot_details(current_user.account, current_user.email) if current_user.is_integration_user: bots = Utility.filter_bot_details_for_integration_user( current_user.get_bot(), bots) return Response(data=bots)
async def user_input_unique( month: int = Query(default=1, ge=1, le=6), current_user: User = Security(Authentication.get_current_user_and_bot, scopes=TESTER_ACCESS), ): """ Returns the list of user inputs that are not included as part of training examples """ queries_not_present = ChatHistoryUtils.unique_user_input( month, current_user.get_bot()) return Response(data=queries_not_present)
async def get_action_server_logs(start_idx: int = 0, page_size: int = 10, current_user: User = Depends( auth.get_current_user)): """ Retrieves action server logs for the bot. """ logs = list( mongo_processor.get_action_server_logs(current_user.get_bot(), start_idx, page_size)) row_cnt = mongo_processor.get_row_count(HttpActionLog, current_user.get_bot()) data = {"logs": logs, "total": row_cnt} return Response(data=data)
async def remove_member_from_bot( user: str = Path(default=None, description="user mail id", example="*****@*****.**"), bot: str = Path(default=None, description="bot id", example="613f63e87a1d435607c3c183"), current_user: User = Security(Authentication.get_current_user_and_bot, scopes=ADMIN_ACCESS)): """ Removes user from accessing the bot. """ AccountProcessor.remove_member(bot, accessor_email=user) return Response(message='User removed')
async def update_bot_access_for_user( allow_bot: BotAccessRequest, bot: str = Path(default=None, description="bot id", example="613f63e87a1d435607c3c183"), current_user: User = Security(Authentication.get_current_user_and_bot, scopes=ADMIN_ACCESS)): """ Updates user's role or status. """ AccountProcessor.update_bot_access(bot, allow_bot.email, current_user.get_user(), allow_bot.role, allow_bot.activity_status) return Response(message='User access updated')
async def delete_http_action(action: str = Path(default=None, description="action name", example="http_action"), current_user: User = Depends(auth.get_current_user)): """ Deletes the http action config and story event """ try: mongo_processor.delete_http_action_config(action, user=current_user.get_user(), bot=current_user.bot) mongo_processor.delete_story(story=action, user=current_user.get_user(), bot=current_user.bot) except Exception as e: raise AppException(e) message = "HTTP action deleted" return Response(message=message)
async def transfer_ownership( request_data: TextData, bot: str = Path(default=None, description="bot id", example="613f63e87a1d435607c3c183"), current_user: User = Security(Authentication.get_current_user_and_bot, scopes=OWNER_ACCESS)): """ Transfers ownership to provided user. """ AccountProcessor.transfer_ownership(current_user.account, bot, current_user.get_user(), request_data.data) return Response(message='Ownership transferred')