Sending messages
1. Sending messages
OK! In the last two lessons, we learned about creating topics and subscriptions. Now it's time to actually start publishing. In other words, let's send some texts and emails. Ready? Game on!2. Publishing to a Topic
By publishing a message to a topic, all of our subscribers will receive it. To publish to a topic, call the publish method with TopicArn, message, and subject as arguments. This will broadcast the message to every subscriber of the topic. The e-mail subscribers will receive this message in their inbox. The SMS subscribers will receive it on their phone.3. Publishing to a Topic
The message argument contains the body of the message - this goes into the e-mail body and the SMS message.4. Publishing to a Topic
The subject argument gets shown in the subject line of the e-mail and is not visible for those receiving text messages.5. Sending custom messages
We obviously want to send custom messages. In order to do this, we can use Python's nifty string format method to replace text with a variable from our data. This lets us generate custom messages as part of our code.6. Sending a single SMS
Alternatively, we can send a single SMS message. If we use this method, we don't have to worry about having a topic or subscribers to it. We simply send a text message to a phone number. To send a single SMS, we also use the boto3 SNS client's publish method. Since SMS messages don't have subjects, we pass a phone number and the message body. We don't pass a TopicARN because we have no topic to publish to. The person on the other end of the line will get an SMS on her phone.7. Not a good long term practice
This can be good for one-off text messages, but quickly gets very unwieldy for data pipelines. One of the tasks of a data engineer is to balance getting stuff done with being able to maintain it. Sending one-off texts lives in the realm of getting stuff done. Setting up topics and publishing to them lives in the realm of being able to maintain it.8. Publish to Topic vs Single SMS
There are two ways to send a message. We could publish a message to a topic. This assumes we have created a topic and have subscriptions that are listening to it. This is better when there are multiple subscribers, and we need to manage a list of them. Alternatively, we could send a one-off SMS. It's great if we just need to send one message to a phone number - keeping it simple. This is great if we don't have to manage a list, don't need to send via e-mail, and don't want to have a topic or subscriptions.9. Review
In this lesson, we learned how to actually publish our message! We learned the difference between publishing to a topic so that all of our subscribers get it, as opposed to sending single SMS messages. And we learned how to make custom messages!10. Let's practice!
Now that we have the power to alert, let's practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.