Nazywam się Katarzyna, mam 48 lat i stoję przed wyborem, który rozdziera mi serce. W naszym spokojnym miasteczku nad Wisłą mój syn Marek oznajmił, że zamierza ożenić się ze swoją dziewczyną Zosią. Pełni nadziei marzą, by zamieszkać w naszym wynajmowanym mieszkaniu. Stanowczo się temu sprzeciwiam, a powód, który mnie dręczy, jest głęboki. Ta decyzja może na zawsze zmienić naszą relację z synem, ale nie mogę postąpić inaczej, boję się o swoją przyszłość i powtórzenia cudzych błędów.
Marek i Zosia błagają nas z mężem, by pozwolić im zamieszkać w naszym kawalerce. Obecnie mieszkamy razem z Markiem w dwupokojowym mieszkaniu, a kawalerkę kupiliśmy kilka lat temu, biorąc kredyt, który dopiero co spłaciliśmy. To mieszkanie to nasz plan na emeryturę. Wynajmujemy je, by oszczędzać i godnie żyć, gdy przyjdzie czas na odpoczynek. Teraz te pieniądze nie są niezbędne, ale za kilka lat mogą być jedynym zabezpieczeniem. Bez nich staniemy się ubodzy, a nie chcę sp# Ruby, Sinatra, Heroku, and the Twilio API
Oh man, Twilio is dope. Right? So cool. And if you want to build something with it, they’ve got Ruby and Sinatra support. But if you want to do something quickly, that’s small, that you don’t want to have to deploy a full Rails app somewhere, or if you just want to *get to it*, the Heroku + Twilio + Ruby/Sinatra process is your best friend.
So here’s how to get a Sinatra app rolling with Twilio, and deploy it to Heroku.
First things first. Install the Twilio Ruby library:
`gem install twilio-ruby`
Alright.
## How to build a Sinatra app that responds to Twilio
You’ll want to respond to an http POST request with TwiML (Twilio Markup Language). Here’s what that looks like in Sinatra:
“`ruby
require ‘sinatra’
require ‘twilio-ruby’
post ‘/sms’ do
Twilio::TwiML::Response.new do |r|
r.Message “Hey Mon. It’s me again.”
end.text
end
“`
So what’s that doing?
### Breaking it down
1. When someone sends a text message to your Twilio number, it’ll get send to the `/sms` route as a POST request. So when that happens, the URL you configured in Twilio will trigger a POST to that route.
2. You’ve got to respond with TwiML, so instead of serving up html, like you normally might with Sinatra, you’ll respond with XML.
3. Twilio’s Ruby gem will handle the TwiML generation for you in this case, so you just use the `r.Message` method to define what you want to send back.
4. In this example, we’re sending back “Hey Mon. It’s me again.”
5. Translate all that to XML and send it back. That’s what the `.text` at the end is for.
## Fun with parameters
Now, you probably want to do something more fun than that. So Twilio sends you a bunch of parameters with the text message. Here’s a few:
* `From` – the phone number the text was sent from
* `Body` – the content of the text message
* `To` – your Twilio number
So to get the phone number that sent the text message, you’d use `params[‘From’]`. Similarly, to get the content of the message, you’d use `params[‘Body’]`.
Here’s an example:
“`ruby
post ‘/sms’ do
Twilio::TwiML::Response.new do |r|
r.Message “You said: #{params[‘Body’]}”
end.text
end
“`
## Getting it up on Heroku
Alright, so you’ve got a really basic Sinatra app. Here’s how to set it up so you can deploy it to Heroku.
### Set up the app to run
First, you’ll need to tell Heroku what exactly to do with your app when it’s deployed. You’ll do that with a `config.ru` file. So create a `config.ru` in the root directory of your application (same place as you’d put the `app.rb` file or whatever you named your Sinatra app file).
Inside the `config.ru` add:
“`ruby
require ‘./app’
run Sinatra::Application
“`
This assumes you’ve named your app file `app.rb`. If you’ve named it something else, like `server.rb` for example, then you’d change `./app` to `./server`
### A procfile for Heroku
Create a `Procfile` in your app’s root directory. This tells Heroku how to run your app.
If you’ve got a simple sinatra app, it’d look like this:
`web: bundle exec ruby app.rb -p $PORT`
### Set up a Gemfile
Tell Heroku about your dependencies with a `Gemfile` so it can install them when you deploy.
Here’s what you’d put into your Gemfile. Create it in the root directory of your app.
“`ruby
source ‘https://rubygems.org’
gem ‘sinatra’
gem ‘twilio-ruby’
“`
### Install Bundler
If you don’t have bundler, you should.
`gem install bundler`
And then have it install all the dependencies listed in your Gemfile.
`bundle install`
Now you’re ready to get this thing up to Heroku.
### Deploying to Heroku
1. Create an account on [Heroku.com](http://www.heroku.com)
2. Once you’ve created the account, install the [Heroku toolbelt](https://devcenter.heroku.com/articles/getting-started-with-ruby#set-up)
3. Once you’ve got the toolbelt installed, run `heroku login` and enter your login details.
4. In your app’s directory, run `git init` if you haven’t already
5. Then run `heroku create`
6. Now add all your files to git, commit them, and push to heroku:
7. `git add .`
8. `git commit -m ‘a message here’`
9. `git push heroku master`
Now your app is live.
### Getting your URL
So your app is deployed now. You’ll need to get its URL from Heroku to tell Twilio where to send your messages.
To get the URL, run:
`heroku apps:info`
It’ll spit out a ton of info, but the important thing is the Web URL at the bottom, which’ll look something like `http://agile-refuge-1345.herokuapp.com/`
Now you’ll need to configure Twilio to use that URL.
### Configure your Twilio Number
1. Log in to your Twilio account
2. Go to Numbers
3. Click on your phone number
4. Under Messaging, in the A Message Comes In section, select Webhook
5. Paste your heroku url in the blank, and make sure `/sms` is at the end of it. So like `http://agile-refuge-1345.herokuapp.com/sms`
6. Select `HTTP Post`
7. Click save
### Try it out
Text your Twilio number. It should reply!
If you get an error, check the logs to see what’s happening. You can view the heroku logs in the terminal:
`heroku logs`
## A more complex example
Alright, so here’s a little more complex example, where we’re going to do something with the text message we get sent.
Let’s write a little app that listens for a text with a zip code, and returns the current weather conditions for that zip code.
Here’s the code:
“`ruby
require ‘sinatra’
require ‘twilio-ruby’
require ‘httparty’
def weather(zip)
response = HTTParty.get(“http://api.openweathermap.org/data/2.5/weather?zip=#{zip},us”)
body = JSON.parse(response.body)
temp_k = body[‘main’][‘temp’]
temp_f = (temp_k * 9/5 – 459.67).round
weather = body[‘weather’][0][‘description’]
“It’s #{temp_f} degrees and #{weather} in #{zip}”
end
post ‘/sms’ do
zip = params[‘Body’]
Twilio::TwiML::Response.new do |r|
r.Message weather(zip)
end.text
end
“`
And here’s what’s going on:
1. We’re adding the `httparty` gem to make HTTP requests. So now, in addition to the `twilio-ruby` gem and Sinatra, we’ll need to add `httparty` to the Gemfile. So:
“`ruby
source ‘https://rubygems.org’
gem ‘sinatra’
gem ‘twilio-ruby’
gem ‘Czuję, jak ciężar tej decyzji przygniata mnie każdego dnia, ale w głębi duszy wiem, że muszę być silna, choćby miał to być koniec naszych dawnych, beztroskich relacji.



