Created
February 21, 2024 13:41
-
-
Save HooFoo/79067ab94a4ccc9cf638e53814588355 to your computer and use it in GitHub Desktop.
Ruby SMS implementation for gatewayapi.com
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
class Sms | |
URL = 'https://gatewayapi.com/rest/mtsms' | |
def self.send(number, text) | |
if Rails.env.production? || Rails.env.staging? #TODO: feature flags | |
send_sms(number, text) | |
else | |
write_to_file(number, text) # sms text will be available at your.domain/public/pwd.txt | |
end | |
end | |
def self.write_to_file(number, text) | |
file = File.new(Rails.public_path + 'pwd.txt', 'w') | |
file.syswrite(text) | |
file.close | |
end | |
def self.send_sms(number, text) | |
begin | |
headers = {'Authorization' => auth_header, 'Content-Type' => 'application/json', 'Accept' => 'application/json' } | |
body = { | |
sender: 'SMSInfo', | |
message: text, | |
recipients: [ | |
{ | |
msisdn: number.to_i | |
}, | |
]} | |
Rails.logger.debug body | |
Rails.logger.debug headers | |
response = HTTParty.post URL, body: body.to_json, headers: headers | |
Rails.logger.debug response.inspect | |
rescue StandardError => ex | |
Rails.logger.error ex.message | |
end | |
end | |
private | |
# set up with: export GATEWAY_AUTH=xxxx | |
def self.auth_header | |
"Basic #{Base64.strict_encode64("#{ENV['GATEWAY_AUTH']}:")}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FUKKEN SAVED