How can I automatically move a robot's body to the correct height based on its leg(s) rotation?
You can parent the leg to the body so it moves automatically when the body moves, then with the leg selected, in the Numbers Panel right-click on the X rotation, Copy as New Driver, select the body, right-click on the Z location, Paste Driver. The body will now drop down to Z = 0, because that's the current rotation of the leg (at least in my case). Now you may notice the body moves based on the leg's rotation, but not yet correctly:

To properly convert the angle to a vertical offset caused by it, trigonometry has to be used:

In my example the length of the leg is not exactly 1, so I need to multiply that length (2) by the cosine, and the formula, in my case, becomes: 2 * cos(rotation_euler).
With the body selected, right-click on the Z location again, and choose Edit Driver. Change its type from Average Value to Scripted Expression, and update the formula to correctly base the body's height on the leg's rotation:

Effect:

There's some additional problems to be dealt with:

position of the leg lags 1 frame behind, because while it's based (due to parenting) on the cube, the cube is also based on it (due to the driver); the solution is to clear the parenting and copy-paste the driver from the cube's position also to the leg's position;
the formula assumes the leg has 0 thickness; the simplest solution is to put a sphere on the bottom of it and add an additional offset in the formula equal to the radius of the the sphere;
the body can fall through the floor; the solution is to either use a constraint (and because the leg is not parented that means another constraint for it as well), or use max() in the driver to clamp the minimum value, like so: max(1, 2 * cos(rotation_euler))