~singpolyma/sgx-jmp

ref: sip-outbound sgx-jmp/web.rb -rw-r--r-- 3.4 KiB
a318c83fStephen Paul Weber WIP 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true

require "roda"
require "thin"
require "sentry-ruby"
require "bandwidth"

Faraday.default_adapter = :em_synchrony

require_relative "lib/cdr"
require_relative "lib/roda_em_promise"
require_relative "lib/rack_fiber"

BANDWIDTH_VOICE = Bandwidth::Client.new(
	voice_basic_auth_user_name: CONFIG[:creds][:username],
	voice_basic_auth_password: CONFIG[:creds][:password],
).voice_client.client

class Web < Roda
	use Rack::Fiber # Must go first!
	use Sentry::Rack::CaptureExceptions
	plugin :json_parser
	plugin :render, engine: "slim"
	plugin RodaEMPromise # Must go last!

	class << self
		attr_reader :customer_repo, :log
	end

	def customer_repo
		Web.customer_repo
	end

	def log
		Web.log
	end

	def params
		request.params
	end

	def self.run(log, customer_repo)
		plugin :common_logger, log, method: :info
		@log = log
		@customer_repo = customer_repo
		Thin::Logging.logger = log
		Thin::Server.start(
			"::1",
			ENV.fetch("PORT", 8080),
			freeze.app,
			signals: false
		)
	end

BOOP = {}
BOOP2 = {}

	route do |r|
		r.on "inbound" do

				r.post "fallback" do
"<Response><Redirect redirectUrl=\"https://62f3-2607-fea8-1c80-2d-f22f-74ff-fed2-3e37.ngrok.io/inbound/calls/#{params['callId']}/poll\" /></Response>"
				end
			r.on "calls" do
				r.post "status" do
					log.info(params)
"OK"
				end


				r.on :call_id do |call_id|
					r.post "poll" do
						EM.promise_timer(4).then do
"<Response><Redirect redirectUrl=\"https://62f3-2607-fea8-1c80-2d-f22f-74ff-fed2-3e37.ngrok.io/inbound/calls/#{params['callId']}/poll\" /></Response>"
						end
					end

					r.post do
true_call_id = BOOP2.fetch(call_id, call_id)
						render :bridge, locals: { call_id: true_call_id }
					end
				end

				r.post do
begin
if BOOP["#{params['from']},#{params['to']}"]
	BOOP2[BOOP["#{params['from']},#{params['to']}"]] = params["callId"]
	return "<Response><Pause duration=\"300\" /></Response>"
end
					body = Bandwidth::ApiCreateCallRequest.new.tap do |cc|
						#cc.to = "sip:singpolyma%5C40singpolyma.net%40cheogram.com@sip.cheogram.com"
						cc.to = "+12263393063"
						cc.from = params["from"]
						cc.application_id = params["applicationId"]
						cc.call_timeout = 300
						cc.answer_url = "https://62f3-2607-fea8-1c80-2d-f22f-74ff-fed2-3e37.ngrok.io/inbound/calls/#{params['callId']}"
					end
					outbound_leg = BANDWIDTH_VOICE.create_call(CONFIG[:creds][:account], body: body).data.call_id
BOOP["#{params['from']},#{params['to']}"] = params['callId']
"<Response><Ring duration=\"300\" /></Response>"
"<Response><Redirect redirectUrl=\"https://62f3-2607-fea8-1c80-2d-f22f-74ff-fed2-3e37.ngrok.io/inbound/calls/#{params['callId']}/poll\" /></Response>"
"<Response><Hangup/></Response>"
"<Response><Pause duration=\"300\" /></Response>"
rescue
	log.error($!)
exit!
end
				end
			end
		end

		r.on "outbound" do
			r.on "calls" do
				r.post "status" do
					loggable = params.dup.tap { |p| p.delete("to") }
					log.info "#{params['eventType']} #{params['callId']}", loggable
					if params["eventType"] == "disconnect"
						CDR.for_disconnect(params).save.catch do |e|
							log.error("Error raised during /outbound/calls/status", e, loggable)
							Sentry.capture_exception(e)
						end
					end
					"OK"
				end

				r.post do
					customer_id = params["from"].sub(/^\+/, "")
					customer_repo.find(customer_id).then(:registered?).then do |reg|
						render :forward, locals: {
							from: reg.phone,
							to: params["to"]
						}
					end
				end
			end
		end
	end
end