-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
653279f
commit 6b56af2
Showing
8 changed files
with
73 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class SendCustomMessageJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(user, body) | ||
return unless body.present? | ||
|
||
message = Message.create(user:, body:) | ||
|
||
Twilio::Client.new.send_message(message) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,19 @@ | ||
require "twilio-ruby" | ||
|
||
class SendMessageJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(user:, body: "", group: "") | ||
return unless group.present? || body.present? | ||
def perform(user, group) | ||
return unless group.present? | ||
|
||
content = group.present? ? user.next_content(group)&.body : body | ||
content = user.next_content(group)&.body | ||
|
||
return unless content.present? | ||
|
||
@client = Twilio::REST::Client.new(ENV.fetch("TWILIO_ACCOUNT_SID"), ENV.fetch("TWILIO_AUTH_TOKEN")) | ||
|
||
message = @client | ||
.messages | ||
.create( | ||
body: content, | ||
from: ENV.fetch("TWILIO_PHONE_NUMBER"), | ||
to: user.phone_number, | ||
status_callback: "#{ENV.fetch("CALLBACK_URL")}/messages/status" | ||
) | ||
|
||
Message.create( | ||
message = Message.create( | ||
user:, | ||
body: message.body, | ||
message_sid: message.sid, | ||
status: message.status, | ||
content: user.next_content(group) || nil | ||
body: user.next_content(group).body, | ||
content: user.next_content(group) | ||
) | ||
|
||
Twilio::Client.new.send_message(message) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require "twilio-ruby" | ||
|
||
module Twilio | ||
class Client | ||
def initialize | ||
@client = Twilio::REST::Client.new(ENV.fetch("TWILIO_ACCOUNT_SID"), ENV.fetch("TWILIO_AUTH_TOKEN")) | ||
end | ||
|
||
def send_message(message) | ||
sms = @client | ||
.messages | ||
.create( | ||
body: message.body, | ||
from: ENV.fetch("TWILIO_PHONE_NUMBER"), | ||
to: message.user.phone_number, | ||
status_callback: "#{ENV.fetch("CALLBACK_URL")}/messages/status" | ||
) | ||
|
||
message.update(status: sms.status, message_sid: sms.sid) | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require "test_helper" | ||
|
||
class SendCustomMessageJobTest < ActiveSupport::TestCase | ||
include ActiveJob::TestHelper | ||
|
||
test "#perform sends messages" do | ||
user = create(:user) | ||
|
||
stub_successful_twilio_call("Custom Body", user) | ||
|
||
SendCustomMessageJob.perform_now(user, "Custom Body") | ||
|
||
assert_equal 1, Message.count | ||
assert_equal "Custom Body", Message.last.body | ||
end | ||
|
||
test "#perform doesn't send message with no content" do | ||
user = create(:user) | ||
|
||
SendCustomMessageJob.perform_now(user, "") | ||
|
||
assert_equal 0, Message.count | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Twilio::Client < ActiveSupport::TestCase | ||
test "#send_message" do | ||
end | ||
end |