MCPcopy Create free account
hub / github.com/awsdocs/aws-doc-sdk-examples / usage_demo

Function usage_demo

python/example_code/sns/sns_basics.py:282–380  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

280
281
282def usage_demo():
283 print("-" * 88)
284 print("Welcome to the Amazon Simple Notification Service (Amazon SNS) demo!")
285 print("-" * 88)
286
287 logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
288
289 sns_wrapper = SnsWrapper(boto3.resource("sns"))
290 topic_name = f"demo-basics-topic-{time.time_ns()}"
291
292 print(f"Creating topic {topic_name}.")
293 topic = sns_wrapper.create_topic(topic_name)
294
295 phone_number = input(
296 "Enter a phone number (in E.164 format) that can receive SMS messages: "
297 )
298 if phone_number != "":
299 print(f"Sending an SMS message directly from SNS to {phone_number}.")
300 sns_wrapper.publish_text_message(phone_number, "Hello from the SNS demo!")
301
302 email = input(
303 f"Enter an email address to subscribe to {topic_name} and receive "
304 f"a message: "
305 )
306
307 if email != "":
308 print(f"Subscribing {email} to {topic_name}.")
309 email_sub = sns_wrapper.subscribe(topic, "email", email)
310 answer = input(
311 f"Confirmation email sent to {email}. To receive SNS messages, "
312 f"follow the instructions in the email. When confirmed, press "
313 f"Enter to continue."
314 )
315 while (
316 email_sub.attributes["PendingConfirmation"] == "true"
317 and answer.lower() != "s"
318 ):
319 answer = input(
320 f"Email address {email} is not confirmed. Follow the "
321 f"instructions in the email to confirm and receive SNS messages. "
322 f"Press Enter when confirmed or enter 's' to skip. "
323 )
324 email_sub.reload()
325
326 phone_sub = None
327 if phone_number != "":
328 print(
329 f"Subscribing {phone_number} to {topic_name}. Phone numbers do not "
330 f"require confirmation."
331 )
332 phone_sub = sns_wrapper.subscribe(topic, "sms", phone_number)
333
334 if phone_number != "" or email != "":
335 print(
336 f"Publishing a multi-format message to {topic_name}. Multi-format "
337 f"messages contain different messages for different kinds of endpoints."
338 )
339 sns_wrapper.publish_multi_message(

Callers 1

sns_basics.pyFile · 0.70

Calls 10

create_topicMethod · 0.95
publish_text_messageMethod · 0.95
subscribeMethod · 0.95
publish_multi_messageMethod · 0.95
publish_messageMethod · 0.95
list_subscriptionsMethod · 0.95
delete_subscriptionMethod · 0.95
delete_topicMethod · 0.95
SnsWrapperClass · 0.70

Tested by

no test coverage detected