Skip to content

Commit cb47975

Browse files
committed
Added Call Model, Outbound Calls Controller and View, and Outbound Calls routes
1 parent e0267a4 commit cb47975

File tree

9 files changed

+191
-3
lines changed

9 files changed

+191
-3
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* Outbound Calls form styling - extends SMS styling */
2+
.calls-form {
3+
max-width: 600px;
4+
margin: 20px;
5+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
6+
}
7+
8+
.calls-form h1 {
9+
font-size: 24px;
10+
color: #000;
11+
margin-bottom: 20px;
12+
}
13+
14+
.calls-form .field {
15+
margin-bottom: 15px;
16+
}
17+
18+
.calls-form .field label {
19+
display: block;
20+
margin-bottom: 5px;
21+
color: #333;
22+
font-size: 14px;
23+
}
24+
25+
.calls-form .field input[type="text"],
26+
.calls-form .field textarea {
27+
width: 100%;
28+
padding: 8px;
29+
border: 1px solid #ccc;
30+
border-radius: 4px;
31+
font-size: 14px;
32+
}
33+
34+
.calls-form .field textarea {
35+
height: 120px;
36+
resize: vertical;
37+
}
38+
39+
.calls-form .actions {
40+
margin-top: 20px;
41+
}
42+
43+
.calls-form .actions input[type="submit"] {
44+
background-color: #9941FF;
45+
color: white;
46+
padding: 8px 16px;
47+
border: none;
48+
border-radius: 4px;
49+
cursor: pointer;
50+
font-size: 14px;
51+
}
52+
53+
.calls-form .actions input[type="submit"]:hover {
54+
background-color: #9941FF;
55+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class OutboundCallsController < ApplicationController
2+
def new
3+
@call = Call.new
4+
end
5+
6+
def create
7+
@call = Call.new(safe_params)
8+
9+
if @call.save
10+
make_call(@call)
11+
redirect_to :outbound_calls, notice: 'Call initiated'
12+
else
13+
flash[:alert] = 'Something went wrong'
14+
render :new
15+
end
16+
end
17+
18+
def show
19+
call = Call.find(params[:id])
20+
render json: [
21+
{
22+
"action": "talk",
23+
"text": call.text,
24+
"language": "en-AU",
25+
"style": 3
26+
}
27+
]
28+
end
29+
30+
private
31+
32+
def safe_params
33+
params.require(:call).permit(:to, :from, :text)
34+
end
35+
36+
def vonage
37+
Vonage::Client.new(
38+
application_id: ENV["VONAGE_APPLICATION_ID"],
39+
private_key: ENV["VONAGE_PRIVATE_KEY"]
40+
)
41+
end
42+
43+
def make_call(call)
44+
options = {
45+
to: [{ type: 'phone', number: call.to }],
46+
from: { type: 'phone', number: call.from },
47+
answer_url: ["https://#{ENV['VONAGE_SERVER_HOSTNAME']}/outbound_calls/#{call.id}"]
48+
}
49+
50+
response = vonage.voice.create(options)
51+
52+
call.update(
53+
uuid: response['uuid'],
54+
status: response['status']
55+
) if response['status'] && response['uuid']
56+
end
57+
58+
59+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module OutboundCallsHelper
2+
end

app/models/call.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Call < ApplicationRecord
2+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<div class="calls-form">
2+
<h1>Make a Call</h1>
3+
4+
<%= form_with model: @call, url: outbound_calls_path, local: true do |f| %>
5+
<div class="field">
6+
<%= f.label :from, "Your phone number to make the call from" %>
7+
<%= f.text_field :from, placeholder: '+12223334555' %>
8+
</div>
9+
10+
<div class="field">
11+
<%= f.label :to, "Recipient's phone number" %>
12+
<%= f.text_field :to, placeholder: '+12223334555' %>
13+
</div>
14+
15+
<div class="field">
16+
<%= f.label :text, "Voice message" %>
17+
<%= f.text_area :text %>
18+
</div>
19+
20+
<div class="actions">
21+
<%= f.submit "Call" %>
22+
</div>
23+
<% end %>
24+
</div>

config/environments/development.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
require "active_support/core_ext/integer/time"
22

33
Rails.application.configure do
4+
5+
# Replace with your NGROK URL
6+
config.hosts << ENV['VONAGE_SERVER_HOSTNAME']
47

5-
config.hosts << "1d3a-77-137-44-66.ngrok-free.app"
68
# Settings specified here will take precedence over those in config/application.rb.
79

810
# Make code changes take effect immediately without server restart.

config/routes.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Rails.application.routes.draw do
2-
get "inbound_sms/create"
2+
get "outbound_calls/new"
3+
get "outbound_calls/create"
4+
get "outbound_calls/show"
35
# For OutboundSms controller, new & create
46
get '/outbound_sms/new', to: 'outbound_sms#new', as: :new_outbound_sms
57
post '/outbound_sms', to: 'outbound_sms#create', as: :outbound_sms
@@ -9,4 +11,7 @@
911

1012
# For InboundSms controller, create
1113
post '/inbound_sms', to: 'inbound_sms#create', as: :inbound_sms
14+
15+
# For OutboundCall controller, new & create & show
16+
resources :outbound_calls, only: [:new, :create, :show]
1217
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class CreateCalls < ActiveRecord::Migration[8.0]
2+
def change
3+
create_table :calls do |t|
4+
t.string :to
5+
t.string :from
6+
t.text :text
7+
t.string :uuid
8+
t.string :status
9+
t.boolean :is_inboud
10+
t.string :conversation_uuid
11+
12+
t.timestamps
13+
end
14+
end
15+
end

db/schema.rb

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)