While Stealth V2 was a standalone Ruby application, Stealth V3 is a mounted engine within an existing Ruby on Rails application.
In your Rails app, add the stealth
and stealth-bandwidth
gems:
gem 'stealth', git: 'https://github.com/hellostealth/stealth.git', branch: '3.0-mountable'
gem 'stealth-bandwidth'
Create a Bandwidth Messaging App, and set up the webhook URL to point to your ngrok that forwards requests to your Rails app. You must append /stealth/<service>
.
For example:
https://abc1234.ngrok.io/stealth/bandwidth
Inline replies can be created by calling the say
method within Stealth.flow
Stealth.flow :hello do
state :say_hello do
say "Hello world!"
end
end
You can use the send_replies
method within Stealth.flow
to define the reply in Stealth.reply
stealth/flows/hello_flow.rb
Stealth.flow :hello do
state :say_hello do
send_replies
end
end
stealth/replies/hello/say_hello.rb
Stealth.reply :hello do
state :say_hello do
say "Hello world!"
end
end
☝️ We recommend using the send_replies
method, when replies are more complex and that you want to keep your Stealth.flow
dry.
Note: To follow naming conventions, in the stealth/replies directory, you must create a subdirectory that matches the flow name, and inside it, a file named after the state name.
Though suggestions are not a reply type on their own, they enhance the visual presentation of SMS replies by adding line breaks and formatting options as separate choices.
Stealth.flow :animal do
state :ask_if_like_dogs do
say("Do you like dogs?", suggestions: ["Yes", "No"])
update_session_to state: :get_if_like_dogs
end
state :get_if_like_dogs do
handle_message(
yes: proc { say("Woof") },
no: proc { say("Too bad...") }
)
step_to state: :ask_favorite_animal
end
state :ask_favorite_animal do
say("What's your favorite animal?", suggestions: ["Cat", "Dog", "Sloth"])
update_session_to state: :get_favorite_animal
end
state :get_favorite_animal do
translated_msg = get_match(
['Cat', 'Dog', 'Sloth'], raise_on_mismatch: true
)
say("I like #{translated_msg} too!")
end
end
Delays introduce pauses between text replies, improving user experience.
Stealth.reply do
say "Hello"
say(reply_type: "delay", duration: "dynamic")
say "How are you?"
say(reply_type: "delay", duration: 2)
end
The duration can be a floating point value (in seconds) or dynamic, where the system automatically determines the delay.
To enable automatic delays globally:
Stealth.config.auto_insert_delays
Bandwidth supports various MMS file types. For more details, refer to: Bandwidth Supported MMS File Types
You must use a valid URL where the file is hosted.
Stealth.reply do
say(
"Here's an image.",
reply_type: "image",
image_url: "https://example.org/image.png"
)
end
Stealth.reply do
say(
"Here's a video.",
reply_type: "video",
video_url: "https://example.org/cool_video.mp4"
)
end
Stealth.reply do
say(
"Here's an audio.",
reply_type: "audio",
audio_url: "https://example.org/podcast.mp3"
)
end
Stealth.reply do
say(
"Here's a PDF file.",
reply_type: "file",
file_url: "https://example.org/some.pdf"
)
end