In this exercise you will be tasked with implementing the Optimizely Ruby SDK, and experimenting on your own SMS bot with Twilio.
This sample application is setup to make things as easy as possible. All you'll need to do is implement Optimizely, and connect the application to Twilio.
For access to Twilio, see the email this was sent in, or contact @andreas.
Instructions are given as comments in the server.rb
file, but repeated here with additional context and initial setup steps.
When followed closely, this project should take no more than 1-2 hours.
Need some help? Like any good developer, when in doubt, Google it out. If that doesn't help, here are a few resources that might help. If none of these help, contact Andreas :)
Happy coding!
Language = Ruby
- ngrok (tunnels a local server to a public facing endpoint) Libraries
- Sinatra (framework for creating endpoints)
- Twilio (for sending SMS messages)
- HTTParty (for making HTTP requests)
- securerandom (generates a random UUID)
-
Fork this repository, and clone it
click fork
>click clone
>copy the key to clone the repo
in your terminal rungit clone [the text you just copied]
-
cd se-ruby-fullstack-exercise
-
Install ngrok using terminal - I prefer doing this via npm, but there are alternative ways to install ngrok
sudo npm install -g ngrok
If the above command fails, trysudo npm i -g ngrok --unsafe-perm=true --allow-root
-
Log into Twilio via the credentials shared via email
-
In the Twilio console, click the "#" in the lefthand nav, then click "+"
-
Search for and buy a phone number, this will be the number for your sms bot (important! make sure that the number you buy is compatible with SMS) (while phone numbers are cheap, we will turn these off after a month)
-
Open
server.rb
in your prefered text editor -
In
server.rb
replace the TWILIO_NUMBER value with the number you purchased in step 5 (note: the number should be a string beginning with a '+' followed by the country code + the number - no spaces, or other characters) -
Return to the Twilio tab, click to view the console dashboard and copy the Account SID and Auth Token
-
In
server.rb
replace theTWILIO_ACCOUNT_SID
andTWILIO_AUTH_TOKEN
with the SID and token found in step 8. -
Create an Optimizely Full Stack project and replace the
DATAFILE_URL
inserver.rb
, with your own projects datafile
Now that we've done the initial setup we can start implementing the SDK and coding our experiment!
Here are the steps you'll need to follow to get started: For help with implementing Optimizely, use the developer docs
- Add the Optimizely Ruby gem to your Gemfile
- In your terminal tab open on the projects directory ("se-ruby-fullstack-exercise"), run
bundle install
in the terminal - Require the Optimizely SDK in
server.rb
- Use HTTParty to access the json of the datafile - note use the URI variable in the sample code with HTTParty (of course you are welcome to use an alternative library)
- Initialize the Optimizely SDK (for help reference the Optimizely Dev Docs)
- Create an experiment in your Optimizely project either as a code experiment or feature experiment, and implement it within the
get /sms
function
- In your terminal, run
ruby server.rb
(verify your server is running by going to http:localhost:4567) - Open a separate terminal window (while the ruby server is running in the other), and run
ngrok http 4567
- This will create a public url to access your local server shown to you in the output of the command
- In the terminal window where you ran
ngrok http 4567
copy the https url shown - Paste the url in a browser tab and verify you're able to access the same page as in step 1
- Return to Twilio
- Locate the number you purchased from the previous section and click to manage the number
- Scroll down and find the messaging section
- In the "A message comes in" section, select "Webhook" from the drop down and paste the URL copied in step 3 above and past it in the webhook form field with "/sms" included as a path at the end of the URL - for example "https://47707590.ngrok.io/sms".
- In the drop down menu next to the form field, select "HTTP GET" and click save
- Now with everything running, using your mobile phone, send a text message to the Twilio number you bought!
BONUS
For bonus points you can try the following:
- Create another endpoint and implement Optimizely Webhooks, so that anytime your datafile changes you re-initialize Optimizely in your app
- Add feature variables and play around with a feature rollout
- Add some conditional logic to respond in different ways depending on the content of the message received
When your application is working, follow the steps below to create a branch and push your changes to that branch
** Submit your app **
- IMPORTANT in
server.rb
remove the lines containing the account SID and auth token!!! - In your app directory from the terminal enter
git add .
git checkout -b [your branch name]
NOTE: branch names may not contain any spaces ex "andreas/my_branch-name" is a valid namegit commit -m [give your commit a name]
commit messages may contain spacesgit push origin [the branch name you created in step 2]
This will push all of your changes to the master branch of your forked repository.
That's it! By now you should have:
- Forked this repository
- Setup your application and implemented Full Stack
- Made your application public via ngrok
- Connected your application to Twilio
- Pushed your code back to Github
To submit your exercise email a link with Github repo to Zach, Joe or
Questions? Reach out to Andreas!