-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiot_button_handler.rb
More file actions
117 lines (97 loc) · 2.87 KB
/
iot_button_handler.rb
File metadata and controls
117 lines (97 loc) · 2.87 KB
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
require 'uri'
require 'aws-record'
require 'twilio-ruby'
module LambdaFunctions
class Handler
MESSAGE = ENV['MESSAGE']
def self.process(event:, context:)
puts 'ios_button_handler process started.'
puts event
puts context
click_type = event.dig('deviceEvent', 'buttonClicked', 'clickType')
device_id = event.dig('deviceInfo', 'deviceId')
phone_call_handler = PhoneCallHandler.new(device_id)
# Ckeck click type and select actions.
case click_type
when 'SINGLE'
phone_call_handler.try_to_start_phone_call(MESSAGE)
when 'DOUBLE'
phone_call_handler.cancel_current_phone_call
when 'LONG'
phone_call_handler.toggle_phone_call_mute
else
raise 'Unknown click type.'
end
puts 'ios_button_handler process finished.'
rescue => e
puts "Error: #{e.message}"
raise e
end
end
class PhoneCallHandler
TWILIO_ACCOUNT = ENV['TWILIO_ACCOUNT']
TWILIO_TOKEN = ENV['TWILIO_TOKEN']
TWILIO_TEL = ENV['TWILIO_TEL']
TARGET_TEL = ENV['TARGET_TEL']
def initialize(device_id)
@device_id = device_id
@twilio = TwilioClient.new(TWILIO_ACCOUNT, TWILIO_TOKEN)
end
def try_to_start_phone_call(message)
state = PhoneCallState.find_or_initialize(@device_id)
if state.mute
puts 'Do not start calling since mute mode is ON.'
return
end
call = @twilio.start_phone_call(TWILIO_TEL, TARGET_TEL, message)
state.sid = call.sid
state.tel = TARGET_TEL
state.save
end
def cancel_current_phone_call
state = PhoneCallState.find_or_initialize(@device_id)
if not state.sid
puts 'Do not cancel call since there are no calling now.'
return
end
@twilio.terminate_phone_call(state.sid)
state.sid = nil
state.tel = nil
state.save
end
def toggle_phone_call_mute
state = PhoneCallState.find_or_initialize(@device_id)
state.mute = !state.mute
state.save
end
end
class TwilioClient
def initialize(account_sid, auth_token)
@client = Twilio::REST::Client.new account_sid, auth_token
end
def start_phone_call(from, to, message)
@client.api.account.calls.create(
from: from,
to: to,
url: "http://twimlets.com/message?Message[0]=#{URI::encode(message)}",
)
end
def terminate_phone_call(sid)
@client.calls(sid)
.update(status: :completed)
end
end
class PhoneCallState
include Aws::Record
set_table_name ENV['TABLE_NAME']
string_attr :device_id, hash_key: true
string_attr :sid
string_attr :tel
boolean_attr :mute, default_value: false
def self.find_or_initialize(device_id)
state = self.find(device_id: device_id)
state = self.new(device_id: device_id) if state.nil?
state
end
end
end