php – How to programmatically add Gallery Block to Existing Post Content

I need to insert a set of images already in the media library to the end of the content of each new post.

Instead of inserting the html directly is there a way to add a Gallery block by using the image IDs ?

I’ve found this piece of code without much explanation for it

    var content = "Test content";
var el = wp.element.createElement;
var name = 'core/paragraph';
// var name = 'core/html';
insertedBlock = wp.blocks.createBlock(name, {
    content: content,
});
wp.data.dispatch('core/editor').insertBlocks(insertedBlock);

and am wondering if someone can point to some material showing how to do this correctly if it is at all possible to do.

Examples, articles etc – what I’ve come across has not been helpful. Mostly appending the html to the end of the content.