Jump to content

send custom notifications via ftrack_api


Remus Avram

Recommended Posts

Hi all,

is it possible to send custom notifications via ftrack_api?

We are automating a lot with actions. But this creates also a lot of confusion. We need to communicate better to the users. We are using the pop up messages, but these are displayed only for 5 seconds. Sometimes the user is missing them, sometimes the message is going away before he/she finishes reading it.

Sending notifications to the user will help a lot in understanding what is happening.

Link to comment
Share on other sites

Hi Remus, this could be done in two other ways at the moment:

  • Trigger an action dialog for the user with the information. You can just use label with markdown to get the message through.
  • Create a job to show the user progress.

As for the notification system we have ideas of revamping the notifications and at the same time give the ability to add your own. Note that this is just internal discussions at the moment.

Link to comment
Share on other sites

Hey Mattias,

Tried getting the examples running, but couldn't so wanted to see if you could correct my example code?

import getpass

import ftrack_api


def callback(event):
    """Status change updates."""

    session = ftrack_api.Session()

    for entity_data in event["data"].get("entities", []):

        if entity_data["action"] != "update":
            continue

        if "statusid" not in entity_data["keys"]:
            continue

        new_status = session.get(
            "Status", entity_data["changes"]["statusid"]["new"]
        )

        # Task changes
        if entity_data["entityType"] == "task":
            if new_status["name"] == "In Progress":
                event = ftrack_api.event.base.Event(
                    topic='ftrack.action.trigger-user-interface',
                    data={
                        'type': 'form',
                        'items': [
                            {
                                'label': 'My Boolean',
                                'name': 'my_boolean',
                                'value': True,
                                'type': 'boolean'
                            }
                        ],
                        'title': 'Foobar'
                    },
                    target=(
                        'applicationId=ftrack.client.web and '
                        'user.id={0}'.format(
                            event['source']['user']['id']
                        )
                    )
                )
                session.event_hub.publish(event)

    session.commit()


def register(session, **kw):
    """Register event listener."""

    # Validate that session is an instance of ftrack_api.Session. If not,
    # assume that register is being called from an incompatible API
    # and return without doing anything.
    if not isinstance(session, ftrack_api.Session):
        # Exit to avoid registering this plugin again.
        return

    # Register the event handler
    subscription = (
        "topic=ftrack.update and source.applicationId=ftrack.client.web and "
        "source.user.username={0}".format(getpass.getuser())
    )
    session.event_hub.subscribe(subscription, callback)

When a task's status is set to "In Progress" this code should show a form for the user to interact with.

Link to comment
Share on other sites

Should have investigated it deeper. The event from hitting "Submit" on the dialog has a topic of "ftrack.action.launch" with the values in it.

Might be nice to update the documentation with this information.

 

import getpass

import ftrack_api
from ftrack_connect.session import get_shared_session


def callback(event):
    """Status change updates."""

    session = get_shared_session()

    for entity_data in event["data"].get("entities", []):

        if entity_data["action"] != "update":
            continue

        if "statusid" not in entity_data["keys"]:
            continue

        new_status = session.get(
            "Status", entity_data["changes"]["statusid"]["new"]
        )

        # Task changes
        if entity_data["entityType"] == "task":
            if new_status["name"] == "In Progress":
                event = ftrack_api.event.base.Event(
                    topic='ftrack.action.trigger-user-interface',
                    data={
                        'type': 'form',
                        'items': [
                            {
                                'label': 'My Boolean',
                                'name': 'my_boolean',
                                'value': True,
                                'type': 'boolean'
                            }
                        ],
                        'title': 'Foobar'
                    },
                    target=(
                        'applicationId=ftrack.client.web and '
                        'user.id={0}'.format(
                            event['source']['user']['id']
                        )
                    )
                )
                session.event_hub.publish(event)

    session.commit()


def action_launch(event):

    print event


def register(session, **kw):
    """Register event listener."""

    # Validate that session is an instance of ftrack_api.Session. If not,
    # assume that register is being called from an incompatible API
    # and return without doing anything.
    if not isinstance(session, ftrack_api.Session):
        # Exit to avoid registering this plugin again.
        return

    # Register the event handler
    subscription = (
        "topic=ftrack.update and source.applicationId=ftrack.client.web and "
        "source.user.username={0}".format(getpass.getuser())
    )
    session.event_hub.subscribe(subscription, callback)

    subscription = (
        "topic=ftrack.action.launch and "
        "source.user.username={0}".format(getpass.getuser())
    )
    session.event_hub.subscribe(subscription, action_launch)

 

Link to comment
Share on other sites

  • 1 year later...

No updates as of now - as an additional workaround, have you looked into writing a message (note without parent) to the user? This should trigger a notification

note = session.create('Note', { 'content': 'hey', 'user_id': author_user_id})
sesssion.create('Recipient', { 'note_id': note['id'], 'resource_id': target_user_id })
session.commit()

 

Link to comment
Share on other sites

3 hours ago, Mattias Lagergren said:

No updates as of now - as an additional workaround, have you looked into writing a message (note without parent) to the user? This should trigger a notification


note = session.create('Note', { 'content': 'hey', 'user_id': author_user_id})
sesssion.create('Recipient', { 'note_id': note['id'], 'resource_id': target_user_id })
session.commit()

 

I think this is what exactly we are looking for :) 

we will give it a try and let you know if works for us

Link to comment
Share on other sites

  • 1 year later...

I'm coming in 2 years late, but I was attempting to do this myself and found some info which might help others:

  • If api_user == target_user, then target_user will never receive any notification. It can only be found in the sent items of author_user.
  • The browser notification will always be from api_user
  • The note itself will correctly be from author_user.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...