forked from soitun/twilio_endpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio_endpoint.rb
55 lines (35 loc) · 1.97 KB
/
twilio_endpoint.rb
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
require 'uri'
Dir['./lib/**/*.rb'].each &method(:require)
class TwilioEndpoint < EndpointBase
set :logging, true
post '/sms_order' do
order = Order.new(@config, @message['payload']['order'])
client = Twilio::REST::Client.new(@config['twilio.account_sid'], @config['twilio.auth_token'])
body = "Hey #{order.customer_name}! Your order #{order.number} has been received."
message = Message.new(@config, body, order.customer_phone)
message.deliver
process_result 200, { message_id: @message[:message_id],
notifications: [{ level: 'info', subject: "SMS confirmation sent to #{order.customer_phone}", description: body }] }
end
post '/sms_ship' do
shipment = @message['payload']['shipment']['number']
order = @message['payload']['shipment']['order_number']
name = @message['payload']['shipment']['shipping_address']['firstname']
phone = @message['payload']['shipment']['shipping_address']['phone']
client = Twilio::REST::Client.new(@config['twilio.account_sid'], @config['twilio.auth_token'])
body = "Hey #{name}! Your shipment \##{shipment} for order \##{order} has shipped."
message = Message.new(@config, body, phone)
message.deliver
process_result 200, { message_id: @message[:message_id],
notifications: [{ level: 'info', subject: "SMS confirmation sent to #{phone}", description: body }] }
end
post '/sms_cancel' do
order = Order.new(@config, @message['payload']['order'])
client = Twilio::REST::Client.new(@config['twilio.account_sid'], @config['twilio.auth_token'])
body = "Hey #{order.customer_name}! Your order #{order.number} has been canceled."
message = Message.new(@config, body, order.customer_phone)
message.deliver
process_result 200, { message_id: @message[:message_id],
notifications: [{ level: 'info', subject: "SMS confirmation sent to #{order.customer_phone}", description: body }] }
end
end