Use a reed switch as a magnet-operated contact, adding a pull-up, debounce, and suitable mechanical placement.