Skip to main content

How to Setup AWS Pinpoint SMS Two Way Communication (Part 2)?

In this article, we will discuss "How to Setup AWS Pinpoint SMS Two Way Communication?". As previously, I already explained the Pinpoint project setup. So I recommend going through the previous article for a better understanding.





Enable Two-Way Communication





You have to enable two-way communication for each of the numbers separately. I explained in my previous article about requesting a Long Code, Toll-Free, or 10DLC numbers. So go to the "Pinpoint -> SMS and voice -> Phone numbers" screen.









Click on the specific number.









Scroll down and enable the two-way SMS.









After enabling the two-way SMS, you have to add the SNS topic where the user replies received.









Create SNS Topic





Go to AWS SNS (Simple Notification Service) Console.









No need to apply any change in other settings. Add the description if needed then click on the "Create Topic" button.









After successfully creating the SNS topic, you are redirected to the next screen.









Still, we cannot read the user replies using this SNS topic. We have to add the subscription where those replies can be handled. In this tutorial, I'm using the Lambda function for handling our two-way communication. Let me create a basic Lambda function, later on updating this function for further handling.





I already explained about AWS Lambda function in my previous articles. If you are not familiar with Lambda then click here to read more.









Let's update the SNS in our Toll-Free number.









Create SNS Subscription





Go to the SNS console and open the topic "Two_Way_SMS". Click on the "Create subscription" button.









In the "Create subscription" form, you have to select AWS Lambda as a protocol and add the function ARN in the endpoint. Then click on the "Create subscription" button.









Handling Two Way SMS Communication





Open the newly created Lambda function "two_way_sms_handling", and update the following code snippet.





import json

def lambda_handler(event, context):
# Get patient response and send to api for further appointment process
resp = json.loads(event['Records'][0]['Sns']['Message'])
print(resp)




Here, we have to add a trigger to user replies sent to this function.









Choose the SNS from the dropdown and add select the SNS topic.









After adding the trigger, you are redirected to the Lambda function screen.









Whenever users replied back to our SMS then we receive the same reply with some of the system default parameters in the response. For example:





'originationNumber': '+18126664275', 'destinationNumber': '+1XXXXXXXXXX', 'messageKeyword': 'KEYWORD_393113800179', 'messageBody': '3', 'previousPublishedMessageId': 'm2tfblreu543o8ucciuevg***************', 'inboundMessageId': 'a40961af-31a3-4808-****-***********'




As per the response:





  • originationNumber = User mobile number.
  • destinationNumber = Our AWS acquired toll-free number.
  • messageBody = This is a user replied message body.
  • previousPublishedMessageId = This is the previous message-id, you can use this to make the relation for the replied message sent against which message.
  • inboundMessageId = Unique message id for further references.




You can apply changes in your Lambda function as per your needs. For this example, I'm printing the response in cloud watch for demonstration only.





Conclusion





In this article, we are discussing "How to Setup AWS Pinpoint SMS Two Way Communication (Part 2)?" Hope you like this article, I will explain how to manage SMS delivery reports using AWS Kinesis in my next article. Please feel free to add comments if any queries or suggestions.





Keep learning & stay safe ;)

Comments

Popular posts from this blog

Laravel Logging Guzzle Requests in File

In this article, we will discuss “Laravel Logging Guzzle Requests in File”. In the multiple scenarios, we have to use third-party API’s in our project and test those API using postman or any other tool. But also required to maintain a log for each request, such as what we send (request)? and what we receive (response)? Today, […] Read out the full post at here

How to Use SSH with AWS EC2 Instance?

In this article, we will discuss "How to Use SSH with AWS EC2 Instance?" . As I already explain the setup of EC2 Instance and the use of an Elastic IP in my previous article. Today, we will learn how to connect an EC2 instance using SSH. If still, you have not read my previous articles then I recommend checking them once for a better understanding. Prerequisites A running EC2 Instance . Elastic IP (Optional for this article) ".pem" file, which is downloaded when setup the EC2 Instance. If not having the ".pem" file, then you have to create new key/value pairs. Connect via Terminal or WSL(Window Subsystem for Linux) Open your terminal and go to the directory where you downloaded your ".pem" file. Use the following command to connect with the server. ssh -i "****.pem" username@<publicDNS> or <IP Address> The same terminal command can be used in the windows Linux terminal. I'm using ubuntu on my windows machine...

How to Setup and Install Next.js App?

In this article, we will discuss "How to Setup and Install Next.js App" from scratch. What is React.js and Next.js? "React.js" is a JavaScript library for building user interfaces (elements that users see and interacting on-screen). Basically, React provide us some helpful functions to build UI, and leaves it on us where to use those functions in the application. "Next.js" is a React framework. It is maintained by "Vercel" . Next.js features to solve common application requirements such as routing, data fetching, integrations - all while improving the developer and end-user experience. Why we use Next.js ? Next.js comes with the some additional features to solve come application requirements such as: We can build SSG (Static Site Generation), SSR (Server-Side Rendering) and SPA (Single Page Application) apps. Hot code Reloading: Reload the page when it detects any change saved. Routing: No need to configure any route. Files put in the pages fol...