Use machine learning to understand social media user sentiment and create marketing campaigns to engage promoters and detractors.
Imagine you are a brand with a large web and mobile presence. You want to monitor the conversation around your brand on social media and message your promoters/influencers and detractors across multiple channels. AWS and Pinpoint provide a great foundation for doing so easily and at massive scale.
You need the following:
- A mobile app that uses Twitter's APIs and/or SDK for authentication and tweet ingestion
- A macOS-based computer and physical iOS device (the Simulator that's included with Xcode isn't sufficient for testing this solution).
- Xcode, Node.js, npm and CocoaPods installed on your macOS-based computer.
- To download Xcode, see https://developer.apple.com/download/.
- To download Node.js and npm, see https://nodejs.org/en/. Download the latest Long-Term Support (LTS) version for macOS.
- Install CocoaPods via the Mac terminal with
sudo gem install cocoapods
- The AWS Command Line Interface (AWS CLI) installed and configured on your macOS-based computer. For information about installing the AWS CLI, see Installing the AWS Command Line Interface. For information about setting up the AWS CLI, see Configuring the AWS CLI.
- An AWS account with sufficient permissions to create the resources shown in the diagram in the earlier section. For more information about creating an AWS account, see How do I create and activate a new Amazon Web Services account?.
- An Amazon EC2 key pair. You need this to log into the EC2 instance if you want to modify the Twitter handle that you're monitoring. For more information, see Creating a Key Pair Using Amazon EC2.
- An Apple Developer account. Note that the approach that we cover in this post focuses exclusively on iOS devices. You can implement this solution on Android devices.
The first step in this process is to create a Twitter app, which gives you access to the Twitter API. This solution uses the Twitter API to collect tweets in real-time.
To create a Twitter application:
- Log into your Twitter account. If you don't already have a Twitter account, create one at https://twitter.com/signup.
- Go to https://apps.twitter.com/app/new, and then choose Create a new application.
- Under Application Details, complete the following sections:
- For Name, type the name of your app.
- For Description, type a description of your app.
- For the Website and Callback URL fields, type any fully qualified URL (such as https://www.example.com). You'll change these values in a later step, so the values you enter at this point aren't important.
- Choose Create your Twitter application.
- Under Your access token, choose Create your access token.
- Under Application type, choose Read Only.
- Under Oauth settings, note the values next to Consumer key and Consumer secret. Then, under Your access token, note the values next to Access key and Secret access key. You'll need all of these values in later steps.
This solution requires you to download and set up some files from a GitHub repository.
To configure the AWS Mobile SDK in your app:
- Open Terminal.app. On the command line, navigate to the directory where you want to create your project.
- On the command line, type the following command to clone this repository:
git clone https://github.com/aws-samples/amazon-pinpoint-social-sentiment
- Type the following command to change into the directory that contains the installation files:
cd amazon-pinpoint-social-sentiment/mobile
- Type the following command to download the dependencies for this solution:
npm install
- Type the following command to link the dependencies in the project:
react-native link
- Change into the ios directory:
cd ios
- Retrieve and install CocoaPods into your project specified in the Podfile:
pod install
To configure your app:
- From the
/mobile
directory, type the following command to create a backend project for your app and pull the service configuration (aws-exports.js
file) into your project:awsmobile init
Press Enter at each prompt to accept the default response, as shown in the following example.
Please tell us about your project:
? Where is your project's source directory: /
? Where is your project's distribution directory that stores build artifacts: /
? What is your project's build command: npm run-script build
? What is your project's start command for local test run: npm run-script start
? What awsmobile project name would you like to use: mobile-2018-06-20-03-16-39
- Open the file
aws-exports.js
. This file contains information about the backend configuration of your AWS Mobile Hub project. Take note of theaws_mobile_analytics_app_id
key—you'll use this value in a later step. - In a text editor, open the file
amazon-pinpoint-social-sentiment/mobile/App.js
. UnderTwitterAuth.init
, next to twitter_key, replace<your key here>
with the consumer key that you received when you created your Twitter app in Step 1. Then, next totwitter_secret
, replace<your secret here>
with the Consumer Secret you received when you created your Twitter app. When you finish, save the file. - In a text editor, open the file
amazon-pinpoint-social-sentiment/mobile/ios/MobileCon/AppDelegate.m
. Search for the following section:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *jsCodeLocation;
[[Twitter sharedInstance] startWithConsumerKey:@"<your-consumer-key>" consumerSecret:@"<your-consumer-secret>"];
In this section, replace <your-consumer-key>
with your Twitter consumer key, and replace <your-consumer-secret>
with your Twitter consumer secret.
- In a text editor, open the file
amazon-pinpoint-social-sentiment/mobile/ios/MobileCon/Info.plist
. Search for the following section:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>twitterkit-<your-API-key></string>
</array>
</dict>
</array>
Replace <your-API-key>
with your Twitter Consumer Secret.
Now you're ready to set up your app to send push notifications. A recent Medium post from AWS Developer Advocate Nader Dabit outlines this process nicely. Start at the Apple Developer Configuration section, and complete the remaining steps. After you complete these steps, your app is ready to send push notifications.
While your app is building, you can launch the AWS CloudFormation template that sets up the backend components that power this solution.
To launch the AWS CloudFormation template:
- Sign in to the AWS Management Console, and then open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation/home?region=us-east-1.
- Use the region selector in the upper right to ensure that US East (N. Virginia) is the selected region.
- Choose Create new stack.
- Next to Choose a template, choose Specify an Amazon S3 template URL, and then paste the following URL: https://s3.amazonaws.com/mattd-customer-share/twitterdemo.template.yaml. Choose Next.
- Under Specify Details, for Stack Name, type a name for the CloudFormation stack.
- Under Parameters, do the following:
- For AccessToken, type your Twitter access token.
- For SecretAccessToken, type your Twitter access token secret.
- For AppId, type the app ID that you obtained in Part 3.
- For ConsumerKey, type your Twitter consumer key.
- For ConsumerSecret, type your Twitter consumer secret.
- Choose Next.
- On the next page, review your settings, and then choose Next again. On the final page, select the box to indicate that you understand that AWS CloudFormation will create IAM resources, and then choose Create.
When you choose Create, AWS CloudFormation creates the all of the backend components for the application. These include an EC2 instance, a Kinesis data stream, a Kinesis Firehose delivery stream, an S3 bucket, an Elasticsearch cluster, and a Lambda function. This process takes about 10 minutes to complete.
Now you're ready to test the solution to make sure that all of the components work as expected.
Start by logging in to your Twitter account. Send a tweet to @awsformobile
. Your tweet should contain language that has a strongly positive sentiment.
Your EC2 instance, which monitors the Twitter streaming API, captures this tweet. When this happens, the EC2 instance uses the Kinesis data stream to send the tweet to an Amazon S3 bucket for long-term storage. It also sends the tweet to AWS Lambda, which uses Amazon Comprehend to assign a sentiment score to the tweet. If the message is positive, Amazon Pinpoint sends a push notification to the Twitter handle that sent the message.
You can monitor the execution of the Lambda function by using CloudWatch Logs. You can access CloudWatch Logs at https://console.aws.amazon.com/cloudwatch/home?region=us-east-1#logs:. The log should contain an entry that resembles the following example: In the above screen shot, you can see the sentiment score that Amazon Comprehend provides, in this case an 87.1% positive tweet.
On the Amazon Elasticsearch Service (Amazon ES) console, you can watch as Amazon ES catalogs incoming tweets. You can access this console at https://console.aws.amazon.com/es/home?region=us-east-1. In the Amazon ES domain called “tweets”, choose the Kibana URL. You can use Kibana to easily search your incoming tweets, as shown in the following image: Finally, you can go to your Amazon S3 bucket to view an archive of the tweets that were addressed to you. This bucket is useful for simple archiving, additional analysis, visualization, or even machine learning. You can access the Amazon S3 console at https://s3.console.aws.amazon.com/s3/home?region=us-east-1#.
In the real world, you probably don't want to send messages to users immediately after they send tweets to your Twitter handle—if you did, you might seem too aggressive, and your customers might hesitate to engage with your brand in the future.
Fortunately, you can use the campaign scheduling tools in Amazon Pinpoint to create a recurring campaign. When you create a recurring campaign, Amazon Pinpoint sends messages only to customers who meet certain criteria at campaign execution. Additionally, you can schedule your messages to be sent at a specific time in each recipient's time zone.
Important: You have to obtain consent from your customers before you send messages to them.
-
Log in to the AWS console, and then open the Amazon Pinpoint console at https://console.aws.amazon.com/pinpoint/home/?region=us-east-1.
-
On the Projects page, choose your app.
-
In the navigation pane, choose Campaigns, and then choose New Campaign.
-
For Campaign name, type a name for the campaign, and then choose Next step.
-
On the Segment page, do the following
-
On the Message page, type the message that you want to send, and then choose Next step. To learn more about writing mobile push messages, see Writing a Mobile Push Message in the Amazon Pinpoint User Guide.
-
On the Schedule page, choose the date and time when the message will be sent. You can also schedule the campaign to run on a recurring basis, such as every week. To learn more about scheduling campaigns, see Set the Campaign Schedule in the Amazon Pinpoint User Guide.
- Push notifications aren't sending.
- Did you allow permissions for push notifications on the first app launch?
- Did the Kinesis producer script crash? Log into EC2 instance and view logs to troubleshoot.
- Sometimes, the Twitter Stream API decides not to monitor certain Twitter accounts. Twitter does not publish their criteria for which accounts are unmonitored and why. Create a new Twitter account and its tweets should show up on the stream.
- Did you have poor connectivity when you launched the app for the first time? Try force backgrounding/force quitting the app and restarting a few times.
- If all else fails, consult Apple's push notification troubleshooting guide here.
- I'm getting more than one push!
- This can happen if the Lambda fails unexpectedly. Exceptions should be handled apppropriately but troubleshooting for this can be done within provided Lambda funciton.
This sample code is made available under a modified MIT license. See the LICENSE file.