I make it a point to respond carefully whenever someone tells me they're pregnant. Pregnancy isn't always "a blessing," and those who feel anxious or ambivalent deserve to have their feelings validated. However, I'll go out on a limb here and venture that, more likely that not, if someone is announcing their pregnancy to you, they're probably happy about it. In these instances, your instructions are simple: smile, say congratulations, and be (or pretend to be) happy. I talked recently with some moms who told me the worst thing they heard after announcing their pregnancy. You may be surprised to learn that not everyone is familiar with the "shut up and look happy" rule.
I mean, really, this isn't rocket science, people. This isn't even the kind of science you do in a 5th grade after-school enrichment program where you make a vinegar and baking soda volcano. It's even simpler than that. This is very basic, Human Interaction 101. If someone tells you something and they are smiling, or they have revealed their news in well-thought-out, over-the-top manner that can be shared on social media to the delight of all, then return their enthusiasm in-kind and to the best of your ability. Everyone, incidentally, is able to smile and say "congratulations."
Here are some things not to say, in case anyone is in need of a refresher course. Sadly, we know not to say them because someone already did (in-laws and co-workers seem to be the worst offenders) and it didn't go over well. Like, at all.