How do I get the ID of other bricks touching the current brick?
%brick.getNumUpBricks() will tell you how many are connected on top, %brick.getUpBrick(%i) returns the i-th connected brick. So you'll need a loop to get all of them.
You can also call %brick.dump() to see all the variables and methods for it, for similar problems in the future.