Skip to content

Instantly share code, notes, and snippets.

@AaronC81
Created March 25, 2019 19:36
Show Gist options
  • Save AaronC81/ddeeb2f5456b939f12482d8e2abdcb4e to your computer and use it in GitHub Desktop.
Save AaronC81/ddeeb2f5456b939f12482d8e2abdcb4e to your computer and use it in GitHub Desktop.
Three credit checker
require 'capybara'
require 'headless'
require 'twilio-ruby'
THREE_USERNAME = ENV['THREE_USERNAME']
THREE_PASSWORD = ENV['THREE_PASSWORD']
TWILIO_SID = ENV['TWILIO_SID']
TWILIO_AUTH = ENV['TWILIO_AUTH']
PHONE = ENV['PHONE']
THRESHOLD = 9.99
@twilio = Twilio::REST::Client.new(TWILIO_SID, TWILIO_AUTH)
def get_credit
Headless.ly do
session = Capybara::Session.new(:selenium)
# Sign into My3
session.visit('https://sso.three.co.uk/mylogin/?service=https%3A%2F%2Fwww.three.co.uk%2FThreePortal%2Fappmanager%2FThree%2FSelfcareUk%3F_pageLabel%3DP22200581501288714397101%26_nfpb%3Dtrue%26&resource=portlet')
session.find('#username').set(THREE_USERNAME)
session.find("#j_password").set(THREE_PASSWORD)
session.find(".buttonInner14").click
# Navigate to credit page
session.find(:xpath, './/a[contains(., "Check your balance.")]').click
# Read credit
return session.find("#balanceBody td.alignRight").text.gsub('£', '').to_f
end
end
def send_text(number, message)
@twilio.api.account.messages.create(
from: '+447446382408',
to: number,
body: message
)
end
unless THREE_PASSWORD && THREE_PASSWORD && TWILIO_AUTH && TWILIO_SID && PHONE
puts "missing ENVs"
exit
end
credit = get_credit
if credit <= THRESHOLD
msg = "You have £%5.2f credit, which is below your set threshold of £%5.2f." % [credit, THRESHOLD]
send_text(PHONE, msg)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment