Messenger Micro AI: move duplicate code into a function