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.
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.
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.
# 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)