-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
executable file
·169 lines (138 loc) · 4.05 KB
/
app.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
require 'sinatra'
require 'json'
require 'skuby'
configure do
enable :logging
# get username and password from Environment variables
username = ENV['SKEBBY_USERNAME']
password = ENV['SKEBBY_PASSWORD']
if (username.nil? || password.nil?)
status_message = "error: verify Skubby credentials (environment vars unset)"
else
status_message = "up and running!"
end
set status: \
{ "about" => "SMS Echo Server (using Skebby)",
"version" => "0.3.3",
"home page" => "https://github.com/solyaris/SMS-Echo-Server",
"e-mail" => "giorgio.robino@gmail.com",
"status" => status_message
}
# Initialize Skuby
Skuby.setup do |config|
config.method = 'send_sms_classic'
config.username = username
config.password = password
config.sender_string = 'ECHO-SERVER'
#config.sender_number = 'xxxxxxxxxxxx'
config.charset = 'UTF-8'
end
# in case you are testing with scenario: *SHARED NUMBER + KEYWORD*
enable :contain_keyword
set echo_keyword: "ECHO"
end
before do
content_type :json
logger.level = Logger::INFO
# logger.level = 0 # set log level to debug
# logger.datetime_format = "%Y/%m/%d @ %H:%M:%S "
end
helpers do
#
# dump a Ruby hash object in a pretty printed JSON
#
def to_json( dataset, pretty_generate=true )
if !dataset #.empty?
return no_data!
end
if pretty_generate
JSON.pretty_generate(JSON.parse(dataset.to_json)) + "\n"
else
dataset.to_json
end
end
def no_data!
status 204
# to_json message: "no data"
end
#
# echo_text
#
# If message is in format: <KEYWORD><separator_char><text>
# return a new message that substitute KEYWORD in the original message
# with <ECHO> <text>
#
# By example with a message containing a <KEYWORD>:
#
# echo_message "TEST123 Hello World! I'm poor!"
# # => "ECHO Hello World! I'm poor!"
#
# By example with a message without keyword:
#
# echo_message "Hello World! I'm rich!"
# # => "ECHO Hello World! I'm rich!"
#
def echo_text (text)
if settings.contain_keyword
# substitute <KEYWORD> with <ECHO>
separator_index = text.index(/\s/)
lenght = text.length
cut_text = text[separator_index+1, lenght-separator_index]
"#{settings.echo_keyword} #{cut_text}"
else
"#{settings.echo_keyword} #{text}"
end
end
def echo_sms (request, params)
# debug info
logger.debug "request header:"
logger.debug request.inspect
logger.debug "request body:"
logger.debug request.body.read.inspect
# log received SMS message
sms_params = "SMS RECEIVED: #{params.to_s}"
logger.info sms_params
# Send back to the sender an SMS echo message!
text = echo_text params[:text]
receiver = params[:sender]
# Send SMS via Skuby
sms = Skuby::Gateway.send_sms text, receiver
if sms.success?
response = { status: sms.status,
text: text,
receiver: receiver,
remaining_sms: sms.remaining_sms
}
response.merge! sms_id: sms.sms_id if sms.sms_id?
logger.info "SMS SENT: #{response.to_s}"
else
response = { status: sms.status,
error_code: sms.error_code,
error_message: sms.error_message,
text: text,
receiver: receiver
}
response.merge! sms_id: sms.sms_id if sms.sms_id?
logger.error "SMS SENT: #{response.to_s}"
end
# JSON response (for debug purposes)
to_json ( { "SMS RECEIVED".to_sym => params, "SMS SENT".to_sym => response } )
end
end
get "/" do
to_json settings.status
end
get "/echoserver/skebby" do
to_json message: 'sorry, to be done, use POST.'
end
post "/echoserver/skebby" do
# received SMS: elaborate ECHO logic
# send an echo SMS with Skuby::Gateway.send_sms
echo_sms request, params
end
not_found do
to_json message: 'This is nowhere to be found.'
end
error do
to_json message: 'Sorry there was a nasty error - ' + env['sinatra.error'].name
end