This is a serious concern that scientists and politicians have been mulling over in Real Life for decades now:
There's a specially designed test called "the Turing Test", which is meant to find if an artificial intelligence is genuinely sentient. So far no robots or computers have been able to pass it, and it's usefulness is a major point of contention for scientists (not helped by the fact that some humans somehow manage to fail the test). However quite a few are sure that, eventually, there will be a machine that will pass the test.
The ethics and rights of robots have been debated about for ages. Currently the argument seems to be less whether robots should have rights and more which rights robots should have and when.
This article discusses and explains in detail how governments are preparing in case of contact with sentient aliens and whether they would be treated as legal citizens. It seems that in the current state of such laws, there's a metric assload of bureaucratic snafus and loopholes that would have to be dealt with for aliens to have any real rights while on Earth. It's most likely that if we actually did meet aliens, there would be some hasty reworking of laws and a new legal classification created so as to avoid First Contact becoming a political shitstorm.