def get_user_id_from_event(tracker: Tracker) -> Text:
    """Pulls "session_started" event, if available, and 
       returns the userId from the channel's metadata.
       Anonymous user profile ID is returned if channel 
       metadata is not available
    """
    event = tracker.get_last_event_for("session_started")
    if event is not None:
        # Read the channel's metadata.
        metadata = event.get("metadata", {})
        # If "usedId" key is missing, return anonymous ID.
        return metadata.get("userId", anonymous_profile.get("id"))

    return anonymous_profile.get("id")
示例#2
0
    async def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[EventType]:
        """Look up all incidents associated with email address
           and return status of each"""

        user_profile = tracker.get_slot("user_profile")

        # Handle anonymous profile. No need to call Snow API.
        if user_profile.get("id") == anonymous_profile.get("id"):
            message = "Since you are anonymous, I can't really tell your incident status :)"
        else:
            incident_states = snow.states_db()
            incidents_result = await snow.retrieve_incidents(user_profile)
            incidents = incidents_result.get("incidents")
            if incidents:
                message = "\n".join(
                    [
                        f'Incident {i.get("number")}: '
                        f'"{i.get("short_description")}", '
                        f'opened on {i.get("opened_at")} '
                        f'{incident_states.get(i.get("incident_state"))}'
                        for i in incidents
                    ]
                )
            else:
                message = f"{incidents_result.get('error')}"

        dispatcher.utter_message(message)
        return []
    async def fetch_slots(tracker: Tracker) -> List[EventType]:
        """Add user profile to the slots if it is not set."""

        slots = []

        # Start by copying all the existing slots
        for key in tracker.current_slot_values().keys():
            slots.append(SlotSet(key=key, value=tracker.get_slot(key)))

        user_profile = tracker.get_slot("user_profile")
        user_name = tracker.get_slot("user_name")

        if user_profile is None:
            id = get_user_id_from_event(tracker)
            if id == anonymous_profile.get("id"):
                user_profile = anonymous_profile
            else:
                # Make an actual call to Snow API.
                user_profile = await snow.get_user_profile(id)

            slots.append(SlotSet(key="user_profile", value=user_profile))

        if user_name is None:
            slots.append(
                SlotSet(key="user_name", value=user_profile.get("name")))

        return slots
示例#4
0
    async def submit(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[Dict]:
        """Create an incident and return the details"""

        user_profile = tracker.get_slot("user_profile")
        confirm = tracker.get_slot("confirm")

        if not confirm:
            dispatcher.utter_message(
                template="utter_incident_creation_canceled"
            )
            # Early exit.
            return self.build_slot_sets(user_profile)

        # Handle anonymous profile. No need to call Snow API.
        if user_profile.get("id") == anonymous_profile.get("id"):
            message = (
                "Nice try anonymous. But I can't actually create a "
                "ticket for you. Appreciate your enthusiasm though :)"
            )
        else:
            result = await snow.create_incident(
                user_profile.get("id"),
                tracker.get_slot("incident_title"),
                tracker.get_slot("problem_description"),
                tracker.get_slot("priority")
            )
            incident_number = result.get("number")
            if incident_number:
                message = (
                    f"Incident {incident_number} has been opened for you. "
                    f"A support specialist will reach out to you soon."
                )
            else:
                message = (
                    f"Something went wrong while opening an incident for you. "
                    f"{result.get('error')}"
                )

        dispatcher.utter_message(message)
        return self.build_slot_sets(user_profile)
示例#5
0
    async def fetch_slots(
            tracker: Tracker,
            dispatcher: CollectingDispatcher) -> List[EventType]:
        """
        Add user profile to the slots if it is not set.
        """

        slots = []

        # Start by copying all the existing slots
        for key in tracker.current_slot_values().keys():
            slots.append(SlotSet(key=key, value=tracker.get_slot(key)))

        user_profile = tracker.get_slot("user_profile")
        user_name = tracker.get_slot("user_name")
        user_email = tracker.get_slot("user_email")

        if user_profile is None:
            # id = get_user_id_from_event(tracker)

            # Injecting sys_id so that I can work in Rasa shell
            # id = "a8f98bb0eb32010045e1a5115206fe3a"  # Abraham Lincoln
            id = "0a826bf03710200044e0bfc8bcbe5d7a"  # Adela Cervantsz

            if id == anonymous_profile.get("id"):
                user_profile = anonymous_profile
            else:
                # Make an actual call to Snow API.
                user_profile = await snow.get_user_profile(id)
                if user_profile is None:

                    dispatcher.utter_message(template="utter_no_connection")
                    user_profile = anonymous_profile

            slots.append(SlotSet(key="user_profile", value=user_profile))

        if user_name is None:
            slots.append(SlotSet(key="user_name", value=user_profile.get("name")))

        if user_email is None:
            slots.append(SlotSet(key="user_email", value=user_profile.get("email")))

        return slots
    async def fetch_slots(dispatcher: CollectingDispatcher,
                          tracker: Tracker) -> List[EventType]:
        """Add user profile to the slots if it is not set."""

        slots = []

        # Start by copying all the existing slots
        for key in tracker.current_slot_values().keys():
            slots.append(SlotSet(key=key, value=tracker.get_slot(key)))

        user_profile = tracker.get_slot("user_profile")
        user_name = tracker.get_slot("user_name")

        if user_profile is None:
            id = get_user_id_from_event(tracker)

            # injecting sys_id so that I can work in rasa shell
            connection_status = await snow.connection_status()
            if connection_status == True:
                id = "62826bf03710200044e0bfc8bcbe5df1"

            if id == anonymous_profile.get("id"):
                user_profile = anonymous_profile
            else:
                # Make an actual call to Snow API.
                user_profile = await snow.get_user_profile(id)

            slots.append(SlotSet(key="user_profile", value=user_profile))

        if user_name is None:
            slots.append(
                SlotSet(key="user_name", value=user_profile.get("name")))
            slots.append(
                SlotSet(key="user_email", value=user_profile.get("email")))

        # valideting connect with snow server
        connection_status = await snow.connection_status()
        if connection_status == False:
            dispatcher.utter_message(
                "Connection Error: There is no connection with ServiceNow server. So, you will be treated as anonymous"
            )

        return slots