Manipulating the avatar via automation script?

Hi there. I’m trying to generate avatars for my TODO items in fibery using an image generator AI, but I can’t seem to access the Avatar field in an automation script.

When I console.log(entity['Avatar']); in the script, it prints undefined even though that specific entity has an avatar (that I uploaded manually). If I try to fetch the field via the API with const entityWithExtraFields = await fibery.getEntityById(entity.type, entity.id, ['Avatar']);, then triggering the automation displays a “script failed” popup.

Printing out all the keys with console.log(Object.keys(entity)); shows a list of keys without 'Avatar'.

Is this not supported for now? Doesn’t seem like a stretch to support it since a 'Files' field can be manipulated in a script.

2 Likes

Hi @Sandi
For historical reasons Avatar is only label shown on UI, however it is implemented as files collection with name ‘avatar/avatars’.

You can access it via

const fibery = context.getService('fibery');


for (const entity of args.currentEntities) {
    const entityWithExtraFields = await fibery.getEntityById(entity.type, entity.id, ['avatars']);

    console.log(entityWithExtraFields);
}
2 Likes

Haven’t tried this yet, but thanks!

Is it possible to create a script that would copy the avatar from one entity to another one that has the same name?

(Quite newbie in programming here…)

It is possible, but not a simple thing to do.

Given that you have a ‘source’ entity (with the avatar you want to copy) and a ‘destination’ entity (where you want to add the avatar), your automation needs to know which these two entities are.

If you have an automation running in the source, there needs to be a way for you to tell it the destination, or vice versa.

Assuming you can identify them, your script would look something like this:

// automation running in the destination
const fibery = context.getService('fibery');
const https = context.getService('http');

// some code to get the file secret of the avatar in the source
// ...
//

// this function uploads another copy of the avatar file (using the file secret from the source)
const response = await https.postAsync('https://WORKSPACE_NAME.fibery.io/api/files/from-url', {
    'headers': {
        'Authorization': 'Token YOUR_API_TOKEN',
    },
    'body': {
         "url": "https://WORKSPACE_NAME.fibery/io/api/files/FILE_SECRET",
         method: "GET",
         name: "img.jpg",
         headers: {
           'Authorization': 'Token YOUR_API_TOKEN',
         }
    } 
});

// add the uploaded file id to the avatar collection for this entity
await fibery.addCollectionItem(entity.type, entity.id, 'avatars', response['fibery/id'])
1 Like