Skip to content

Pinpoint RSVP System

0

Team -- I am looking to create an SMS based RSVP system that should --

a) greet the user based on 'OriginationNumber' .. for instance, if the text message is coming from +18001234567, the response back should be a greeting with the user's name based on a details in the file imported as part of a segment

b) present a small menu the user can interact with and confirm their attendance

c) save all responses to a csv or text file

Question I have is on a). Once Pinpoint receives a SMS from an 'OriginationNumber', how do I automatically greet the user tied to the 'OriginationNumber'? I suspect I have to import a file with all phone numbers and run a lambda function to compare the 'OriginationNumber' with the file and return the associated name?

Any help is greatly appreciated for this new Pinpoint user.

asked 4 years ago52 views
1 Answer
0

`import boto3 import csv import os

Initialize AWS services

pinpoint_client = boto3.client('pinpoint') dynamodb = boto3.resource('dynamodb')

DynamoDB table name

TABLE_NAME = "UserDetails"

Pinpoint configuration

PINPOINT_APP_ID = "your-pinpoint-app-id" ORIGINATION_NUMBER = "your-origination-number" # E.g., +18001234567

def lambda_handler(event, context): # Extract message details message = event['Records'][0]['body'] origination_number = event['Records'][0]['originationNumber']

# Lookup user details in DynamoDB
table = dynamodb.Table(TABLE_NAME)
response = table.get_item(Key={'PhoneNumber': origination_number})

if 'Item' in response:
    user_name = response['Item']['Name']
else:
    user_name = "Guest"

# Create a greeting message
greeting_message = f"Hello {user_name}, thank you for RSVPing. Reply YES to confirm or NO to decline."

# Send the greeting message
pinpoint_client.send_messages(
    ApplicationId=PINPOINT_APP_ID,
    MessageRequest={
        'Addresses': {
            origination_number: {'ChannelType': 'SMS'}
        },
        'MessageConfiguration': {
            'SMSMessage': {
                'Body': greeting_message,
                'MessageType': 'TRANSACTIONAL',
                'OriginationNumber': ORIGINATION_NUMBER
            }
        }
    }
)

# Log response (you can also log to DynamoDB or S3)
log_response(origination_number, message)

return {"statusCode": 200, "body": "Message sent successfully"}

def log_response(phone_number, response_message): # Log responses to a CSV in S3 or local file system csv_file = "/tmp/responses.csv" fieldnames = ["PhoneNumber", "Response"]

# Check if file exists, create with header if not
write_header = not os.path.exists(csv_file)

with open(csv_file, mode='a', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=fieldnames)
    if write_header:
        writer.writeheader()
    writer.writerow({"PhoneNumber": phone_number, "Response": response_message})

# Upload to S3 (optional)
s3 = boto3.client('s3')
s3.upload_file(csv_file, 'your-s3-bucket-name', 'responses/responses.csv')

`

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.