Your App in 15 Minutes
Your app keeps its own help desk. It tells ReplyBrain when a customer writes (a signed event), answers questions about that customer (facts), and receives the drafted reply as a draft in its own desk, where your teammate sends it. This page uses the Ruby gem replybrain-client, mounted in a Rails app. Any stack can implement the same HTTP contract: see Connection events and Capabilities.
Step 1. Ask for the connection (2 minutes)
Connections are set up by ReplyBrain for you today. Write to [email protected] with your app's public HTTPS base URL (https://app.example.com). You receive four values once:
| Value | What it is |
|---|---|
connection_id |
Your app's connection. Every signature is bound to it. |
key_id and secret (rbsec_…) |
The secret both sides sign with: ReplyBrain its calls to your app, your app its events. |
API key (rb_sk_live_…) |
A secret key bounded to this connection, scope connections.read. |
They are shown once. Put them straight into your encrypted credentials; never in a repository, a log or a chat.
Step 2. Install the gem (2 minutes)
The gem is not on rubygems.org. Today it is vendored: copy the replybrain-client source you received into your app, then:
# Gemfile
gem "replybrain-client", path: "vendor/replybrain-client"
bundle install
It has no runtime dependencies; Rails is optional but this page assumes it.
Step 3. Configure and mount (3 minutes)
# config/initializers/replybrain.rb
ReplyBrain::Client.configure do |c|
creds = Rails.application.credentials.replybrain
c.api_key = creds[:api_key]
c.connection_id = creds[:connection_id]
c.base_url = "https://replybrain.com/v1"
c.signing_secrets = { creds[:key_id] => creds[:secret] } # current first; add the previous one while rotating
c.effects = ReplyBrain::Client::Effects::ActiveRecordStore.new
c.account_topic = "user" # the facts topic for a customer's whole account
c.record_topics = %w[order] # records that topic lists, which a message may be about
end
# config/routes.rb
mount ReplyBrain::Client::Engine => "/replybrain"
The effects store needs one table:
create_table :replybrain_effects, id: false do |t|
t.string :key, null: false, primary_key: true
t.text :value, null: false
t.timestamps
end
ReplyBrain calls your app at https://app.example.com/replybrain/…, always a signed POST. Anything unsigned is refused with 401.
Step 4. Serve facts (2 minutes)
Tell ReplyBrain what a customer's account and records look like. Only what your block returns leaves your app.
class User < ApplicationRecord
replybrain_facts(:user, account: :id) do |user|
{
plan: user.plan_name,
plan_status_label: user.plan_status.humanize, # a label people read, not a code
orders: { recent: user.orders.recent.limit(5).map { |o| { id: o.id, reference: o.number, status_label: o.status.humanize } } }
}
end
end
class Order < ApplicationRecord
replybrain_facts(:order, account: :user_id) { |order| { reference: order.number, status_label: order.status.humanize, eta: order.eta } }
end
account: names who owns the record; a request for another account's record answers exactly like a missing one. See Facts.
Step 5. Post events (3 minutes)
When a message is added to a conversation in your desk (by the customer or by your team), post an event. Do it from a job enqueued after the database commits, never inline:
class ReplyBrainEventJob < ApplicationJob
def perform(message)
ticket = message.ticket
ReplyBrain::Client.connection.post_event(
source_event_id: "message:#{message.id}",
source_object: { type: "ticket", id: ticket.reference },
occurred_at: message.created_at.utc.iso8601,
kind: "conversation.message_added",
message: {
id: message.id.to_s, body_text: message.body,
visibility: "customer", channel: "in_app",
direction: message.from_customer? ? "inbound" : "outbound",
author: message.from_customer? ? "requester" : "agent",
author_name: (message.author.first_name unless message.from_customer?)
},
requester: { ref: "user:#{ticket.user.id}", account_scope: ticket.user.id.to_s, name: ticket.user.first_name, email: ticket.user.email },
assignee: ticket.assignee ? { name: ticket.assignee.name, email: ticket.assignee.email } : {}
)
end
end
Always send message.channel and message.visibility. Without channel the conversation is filed under api, and drafts are refused there until a disclosure decision exists for api. Without visibility: "customer" the message counts as internal and is never answered. See Events.
Step 6. Receive drafts, and let them be taken back (2 minutes)
Two pieces in app/capabilities/ (the engine loads that folder):
# app/capabilities/help_desk_source.rb: drafts arrive here
class HelpDeskSource < ReplyBrain::Client::Source
publish do |message|
next refused(:not_draft_only) unless message.draft? # draft-only: never send
ticket = Ticket.find_by!(reference: message.conversation_ref.delete_prefix("ticket:"))
draft = ticket.drafts.find_or_create_by!(effect_key: message.effect_key) { |d| d.body = message.body }
drafted(draft.id)
end
# A publish that timed out: does a draft exist for this key? state is pending, sent,
# withdrawn, rejected, superseded or expired; message_id once a person sent it.
lookup do |effect_key|
Draft.find_by(effect_key:)&.then { |d| { draft_id: d.id.to_s, state: d.state, message_id: d.message_id&.to_s }.compact }
end
end
# app/capabilities/message_withdraw.rb: without it, your app is sent no draft at all
class MessageWithdraw < ReplyBrain::Client::Capability
manifest "message.withdraw", version: 1, effect: :communication
argument :publication_id
argument :reason
perform do |draft_id: nil, effect_key:|
draft = Draft.find_by(id: draft_id)
next succeeded(outcome: "not_pending") unless draft
next succeeded(outcome: "already_sent", message_id: draft.message_id.to_s) if draft.sent?
draft.withdraw!(idempotency_key: effect_key)
succeeded(outcome: "withdrawn")
end
reconcile { |draft_id: nil| Draft.find_by(id: draft_id)&.withdrawn? ? effected(outcome: "withdrawn") : not_effected }
verifier do |draft_id: nil|
draft = Draft.find_by(id: draft_id) or next pending
observe(source: "draft:#{draft.id}", revision: draft.updated_at) { draft.withdrawn? }
end
end
conversation_ref is "<source_object.type>:<source_object.id>" of your events. The full contract, and the optional support.open_tickets, identity.lookup and conversation.close, are in Capabilities.
Step 7. Register and go draft-only (1 minute)
Deploy, then tell ReplyBrain it can:
- read your app's manifest (your fact topics and capabilities);
- register
message.withdraw(and any of the optional operations you serve); - record the draft-only decision for the channel your events carry (
in_appabove): disclosurenone, the assistant drafts, the owner sends.
Then follow Check it works.