Any way to create User @mentions using API?

Hello Asanas,

I am trying to find a not hacky way to @mention someone using the API. As I know the id in tag is not similar to the user id but redirects to the user list of tasks and this id is not available in the user record.

Any fancy ideas are always welcome :slight_smile:

Cheers,
D.

Any thoughts @Matt_Bramlage or @Jeff_Schneider?

When you have User object, it has an id, see Build an app with Asana. Just put URL in form of

https://app.asana.com/0/${user.id}/list

in Task.notes or Story.text and it’ll appear as @user mention.

Is URL format mentioned above the thing that you were searching for?

Thank you for your reply @Myroslav_Opyr.

Your solution is not working. The problem is that the user id is not the same with the id at the URL for the user’s task list. I don’t know why Asana has impement it that way but there is no way to know the user’s task list id and use it to mention someone in a task or comment.

Very true :frowning:

Mine task list ID is my id+6.

I was able to get a list of all my team IDs from terminal calls.

Then, I had to do a lookup every time I wanted to mention that user or assign them as a collaborator.

1 Like

Hey @Noel_Howell1,

You can assign a task to a user or added him as a collaborator using his id but you cannot mention him using this id.

What is the url format you used to mention a user in a task description or in a comment?

It’s pretty straightforward actually
I reverse engineered it, grabbed a a story I created manually via the API

All you have to do is;
https://app.asana.com/0/"user_gid" Good Morning

will return

@user good morning

This doesn’t work for me

Hi @Korey_Perkins,

Since this thread was last discussed, Asana added formal support for @mentoning users via the API.

The format to use is:

<a data-asana-gid="user's gid goes here" data-asana-type="user" />

For more, see Rich text

1 Like

For those who interested hot to get it to work, what have Phil mentioned:

html_text = '<body><a data-asana-gid="user's gid goes here"/>\nGood morning</body>'
                        stories_api_instance.create_story_for_task(body, data['gid'], {'html_text': f'{html_text}'})

Previously I faced the problem with parameters of .create_story_for_task comand, make sure there is ‘html_text’ , not ‘text’ in the second line of code.

1 Like

Thank you for sharing @anon36729299. It looks like you are using our new Python client library v5.X.X to make this request.

Here is a more detailed code sample for folks who want to do this (python-asana v5.0.3):

import asana
from asana.rest import ApiException
from pprint import pprint

configuration = asana.Configuration()
configuration.access_token = '<YOUR_ACCESS_TOKEN>'
api_client = asana.ApiClient(configuration)

# create an instance of the API class
stories_api_instance = asana.StoriesApi(api_client)
body = {
    "data": {
        "html_text": "<body><a data-asana-gid=\"<USER_GID>\" data-asana-accessible=\"true\" data-asana-type=\"user\" data-asana-dynamic=\"true\">@<NAME_OF_USER></a>\nGood morning</body>",
    }
}
task_gid = "<TASK_GID>"
opts = {
    'opt_fields': "html_text"
}

try:
    # Create a story on a task
    api_response = stories_api_instance.create_story_for_task(body, task_gid, opts)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling StoriesApi->create_story_for_task: %s\n" % e)

Replace in the above:

  • <YOUR_ACCESS_TOKEN>
  • <USER_GID>
  • <NAME_OF_USER>
  • <TASK_GID>

with your own information

IMPORTANT: @mentions in our rich text does not automatically add the @mentioned user to the resource. So the above code will show the user as @Mentioned in the comment of a task but they will not be added as a collaborator to that task. This means that new events on that task will not notify that @mentioned user. If you want that user to be notified of events on that task in their Asana inbox please make a follow up call to our Add followers to a task endpoint to add that user as a collaborator on the task.

EX: Using python-asana v5.0.3

# create an instance of the API class
tasks_api_instance = asana.TasksApi(api_client)
body = {
    "data": {
        "followers": ["<USER_GID>"],
    }
}
task_gid = "<TASK_GID>"
opts = {}

try:
    # Add followers to a task
    api_response = tasks_api_instance.add_followers_for_task(body, task_gid, opts)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling TasksApi->add_followers_for_task: %s\n" % e)
1 Like