move message handling code to a separate function so the restapi can call it. This might not work