Created
July 21, 2023 21:04
-
-
Save aldodelgado/a819eb52461b44a95e25ccaec5bf03c1 to your computer and use it in GitHub Desktop.
SMS NYC subway train times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'twilio-ruby' | |
| require 'net/http' | |
| require 'json' | |
| # Replace these variables with your actual Twilio credentials and phone numbers | |
| TWILIO_ACCOUNT_SID = 'your_twilio_account_sid' | |
| TWILIO_AUTH_TOKEN = 'your_twilio_auth_token' | |
| TWILIO_PHONE_NUMBER = 'your_twilio_phone_number' | |
| YOUR_PHONE_NUMBER = 'your_phone_number_to_receive_sms' | |
| def get_nyc_subway_train_times | |
| # Replace with your MTA API URL to get subway train times | |
| mta_api_url = 'https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-l' | |
| mta_api_key = 'your_mta_api_key' # Get your MTA API key from MTA Developer Portal | |
| uri = URI(mta_api_url) | |
| headers = { | |
| 'x-api-key' => mta_api_key, | |
| 'Content-Type' => 'application/json' | |
| } | |
| response = Net::HTTP.get(uri, headers) | |
| data = JSON.parse(response) | |
| # Assuming data is in a format where you can access the train times | |
| # Adjust this part based on the actual API response structure | |
| train_times = data['trains'].map { |train| train['time'] } | |
| train_times.join(', ') | |
| end | |
| # Send SMS using Twilio | |
| def send_sms(body) | |
| client = Twilio::REST::Client.new(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN) | |
| client.messages.create( | |
| from: TWILIO_PHONE_NUMBER, | |
| to: YOUR_PHONE_NUMBER, | |
| body: body | |
| ) | |
| end | |
| # Main execution | |
| begin | |
| train_times = get_nyc_subway_train_times | |
| message = "NYC Subway Train Times: #{train_times}" | |
| send_sms(message) | |
| puts 'SMS sent successfully!' | |
| rescue StandardError => e | |
| puts "Error: #{e.message}" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment