A more mature way would be for "first offenders" to politely reply hi and help them with whatever they need and then when everything is done politely point out that asking directly would be better next time, perhaps with a link to some version of nohello.net. With "repeat offenders" sure, go ahead with what you are doing.