From e2783ed7546ae8ce9851a17f40d3366eda85aacd Mon Sep 17 00:00:00 2001 From: Chris Gaffney Date: Thu, 16 Jan 2025 13:31:03 -0500 Subject: [PATCH] Add email notification for SMS messages --- emailer.rb | 15 +++++++++++++++ no_phone.rb | 8 +++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/emailer.rb b/emailer.rb index 481cf7a..005167e 100644 --- a/emailer.rb +++ b/emailer.rb @@ -18,4 +18,19 @@ def voicemail_notification(url, duration, phone_number) response = @sendgrid.client.mail._("send").post(request_body: message.to_json) raise "#{response.status_code}: #{response.body}" if response.status_code != "202" end + + def sms_notification(phone_number, body) + message = SendGrid::Mail.new + message.from = SendGrid::Email.new(email: ENV["EMAIL_FROM"]) + message.subject = "New SMS from #{phone_number}" + + personalization = SendGrid::Personalization.new + personalization.add_to(SendGrid::Email.new(email: ENV["EMAIL_TO"])) + message.add_personalization(personalization) + + message.add_content(SendGrid::Content.new(type: "text/plain", value: body)) + + response = @sendgrid.client.mail._("send").post(request_body: message.to_json) + raise "#{response.status_code}: #{response.body}" if response.status_code != "202" + end end diff --git a/no_phone.rb b/no_phone.rb index e50a131..4dac753 100644 --- a/no_phone.rb +++ b/no_phone.rb @@ -35,9 +35,11 @@ def sound_url(sound) post "/sms" do content_type "text/xml" - Twilio::TwiML::MessagingResponse.new do |r| - r.message body: "Thanks for contacting [i] Collective Idea. Please visit http://collectiveidea.com" - end.to_s + Emailer.new.tap do |email| + email.sms_notification(params["From"], params["Body"]) + end + + Twilio::TwiML::MessagingResponse.new.to_s end # Caller selected a menu item